2016-05-23 186 views
-4
<?xml version="1.0" encoding="UTF-8"?> 
<Document> 
    <DataElement> 
     <Serial_Start>1000</Serial_Start> 
     <Serial_End>2000</Serial_End> 
     <Item value="257896"> 
      <ComItemation> 
       <Price>00</Price> 
       <Sku>20</Sku> 
       <Qcode>27</Qcode> 
      </ComItemation> 
      <ComItemation> 
       <Price>80</Price> 
       <Sku>20</Sku> 
       <Qcode>20</Qcode> 
      </ComItemation> 
     </Item> 
     <Item value="523698"> 
      <ComItemation> 
       <Price>00</Price> 
       <Sku>20</Sku> 
       <Qcode>27</Qcode> 
      </ComItemation> 
      <ComItemation> 
       <Price>80</Price> 
       <Sku>20</Sku> 
       <Qcode>20</Qcode> 
      </ComItemation> 
     </Item> 
     <Item value="856987"> 
      <ComItemation> 
       <Price>00</Price> 
       <Sku>20</Sku> 
       <Qcode>27</Qcode> 
      </ComItemation> 
     </Item> 
    </DataElement> 
    <DataElement> 
     <Serial_Start></Serial_Start> 
     <Serial_End></Serial_End> 
     <Item value="123456"> 
      <ComItemation> 
       <Price>00</Price> 
       <Sku>20</Sku> 
       <Qcode>27</Qcode> 
      </ComItemation> 
      <ComItemation> 
       <Price>80</Price> 
       <Sku>20</Sku> 
       <Qcode>20</Qcode> 
      </ComItemation> 
     </Item> 
     <Item value="123456"> 
      <ComItemation> 
       <Price>00</Price> 
       <Sku>20</Sku> 
       <Qcode>27</Qcode> 
      </ComItemation> 
      <ComItemation> 
       <Price>80</Price> 
       <Sku>20</Sku> 
       <Qcode>20</Qcode> 
      </ComItemation> 
     </Item> 
     <Item value="123456"> 
      <ComItemation> 
       <Price>00</Price> 
       <Sku>20</Sku> 
       <Qcode>27</Qcode> 
      </ComItemation> 
      <ComItemation> 
       <Price>80</Price> 
       <Sku>20</Sku> 
       <Qcode>20</Qcode> 
      </ComItemation> 
     </Item> 
    </DataElement> 
</Document> 

我是PERL的新蜜蜂,試圖解析上述XML文檔。 I 要求以下述格式輸出。解析XML文檔

Serial Start : 1000 
Serial End : 2000 
Item : 257896 
Price : 00 
Sku : 20 
Qcode : 27 
Item : 257896 
Price : 80 
Sku : 20 
Qcode : 20 

...以此類推每個子節點。

樣品至今代碼:

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

use XML::Simple; 
use Data::Dumper; 
my $xml = new XML::Simple; 
my $data = $xml->XMLin("/home/rocky/PERL/doc.xml"); 
print Dumper($data); 

foreach my $imgrec (@{ $data->{DataElement} }) { 
    my $Serial_Start = $imgrec->{Serial_Start}; 
    my $Serial_End = $imgrec->{Serial_End}; 
    foreach my $imgrec1 (@{ $data->{DataElement}->{Item} }) { 
     ## Not sure of this code 
     ## Trying on this part. 
    } 
} 
+0

試試看。如果你使用'XML :: Twig',這並不難。然後問問你有沒有問題。 – Sobrique

+0

' 使用嚴格; 使用警告; 使用XML :: Simple; 使用Data :: Dumper; my $ xml = new XML :: Simple; my $ data = $ xml-> XMLin(「/ home/rocky/PERL/doc.xml」); print Dumper($ data); foreach my $ imgrec(@ {$ data - > {DataElement}}){ my $ Serial_Start = $ imgrec - > {Serial_Start}; my $ Serial_End = $ imgrec - > {Serial_End}; \t我的foreach $ imgrec1(@ {數據 - > {} DataElement - > {項目}}){ \t \t ##不知道這碼 \t \t ##試穿這部分。 \t} }' – vmadhav

+0

[編輯]進入你的問題。 – Sobrique

回答

1

好了,這裏是你的問題:

use XML::Simple; 

不 - 它只會讓你的生活困難。

下面是使用XML::Twig 10首發 - 這不是完全清楚自己在做什麼讓你後輸出,所以:

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

use XML::Twig; 

my $twig = XML::Twig-> new -> parsefile('/home/rocky/PERL/doc.xml') 
foreach my $data_element ($twig->findnodes('//DataElement')) { 
    print "Start:", $data_element->first_child_text('Serial_Start'), "\n"; 
    print "End:", $data_element->first_child_text('Serial_End'), "\n"; 
    foreach my $item ($data_element -> children('Item')){ 
     print "Item: ", $item -> att('value'),"\n"; 
     foreach my $tag (qw (Price Sku Qcode)) { 
      print "$tag: ", $item -> findnodes (".//$tag", 0) -> text,"\n"; 
     } 
    } 
} 

注意 - 這個發現的該第一實例物品下方的特定標籤 - 不是全部。