2011-10-25 26 views
0

實際的問題是我輸入的是一個XML文件,只是樣品輸入這個樣子如何產生的哈希值存儲到新文件

<student> 
    <number>24</number> 
    <education>bachelors</education> 
    <specialization>computers </specialization> 
    <address> 
     <name="michel"/> 
    <house_number="128"/> 
    <town=proddutoor/> 
    </address> 
    </student> 
    <student> 
    <number>23</number> 
    <education>ph.d.</education> 
    <specialization>physics </specialization> 
<address> 
    <name="clar"/> 
    <house_number="12"/> 
    <town=kadapa/> 
    </address> 
    </student> 

我需要提取信息像這樣

$var1={ 
     student=>[ 
       { 
       'number'=>'24', 
       'education'=>'bachelors', 
       'specilization'=>'computers', 
        'address'=>'name=michel' 
           'house_number=128' 
           'town=proddutoor', 
        } 
        { 
        'number'=>'23', 
       'education'=>'ph.d.', 
       'specilization'=>'physics', 
        'address'=>'name=clar' 
           'house_number=12' 
           'town=kadapa', 
        } 

我需要以這種方式打印我的散列並將此散列存儲在新文件中。我在解壓縮地址信息時遇到了問題。我沒有獲取像這樣的地址標記元素。

+0

那麼你有什麼嘗試,你會得到什麼結果? SO不是我們只爲你寫程序的地方。 – cjm

+0

沒關係,但是你[pavani](http://stackoverflow.com/users/992215/pavani)?你正在使用他的[數據](http://stackoverflow.com/questions/7776629/how-can-i-produce-a-pdf-file-using-data-in-an-xml-file-in-perl/ 7785246#7785246)。如果是這樣,請不要創建多個用戶名。如果沒有,那就忽略這個。 –

回答

0

我建議你使用XML::Simple庫進行簡單的XML操作(請記住,這取決於處理和輸出XML文件的確切需求)。

+0

我試過,但它給出了相同的錯誤,如「反斜槓找到操作符預期atc:\ libxml.pl行44附近」store_fd \「。(你需要predelre Store_fd?) – michel

+0

我的意思是你不需要存儲,如果你使用XML :: Simple。 – m0skit0

0

您正在使用文件引用,如商店句子中的文件名稱。 Acording的文檔,你可以通過名字:

store \%nums,'result.xml'; 

或:

store_fd \%nums, $file1; 
+0

我試過了,但是它給出了同樣的錯誤,例如「在store_fd \」附近找到了反向操作符預期atc:\ libxml.pl行44的反斜槓。 (你需要預先聲明Store_fd嗎?) – michel

+0

是的,你需要這樣做:使用Storable qw(store_fd); –

1

Backslash found where operator expected ... near "store_fd \"是錯誤,如果你有行

store_fd \%hash, $file1; 

你會得到你有未定義功能store_fdStorable默認情況下不會導出store_fd;你必須要求它。這聽起來像你沒有。

從您的某些評論中,也可能是您拼錯了函數名稱。 Perl是區分大小寫的; Store_fd是與store_fd完全分離的功能。

這裏有一個完整的例子:

#!/usr/bin/perl 
use strict; 
use warnings; 

use Storable qw(store_fd); 

my %hash = (key => 'value'); 

my $file1; 
open $file1, '>', "result.stored" or die $!; 

store_fd \%hash, $file1; 

注意,你不應該寫一個使用名爲可保存文件result.xml,因爲可保存不寫XML。它寫入自己的格式。如果你想寫XML,你可以使用不同的模塊,如XML::SimpleXML::Writer。但是在給你一個體面的例子之前,你必須提供更多關於你想要你的XML的細節。

+0

嗨我張貼我的問題在實際問題。@ cjm – michel