2012-10-29 45 views
3

輸入XML輸入XML數據doen't匹配輸出XML格式

<?xml version="1.0" encoding="utf-8"?> 
<!--00/00/0000 12:35:25 AM--> 
<Physical xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" > 
    <Pro managementID="22000020"> 
    <Identification Type="XXXXX" >   
     <Address> 
     <Data>test</Data>   
     </Address> 
     <Phone> 
     <Number>0000</Number> 
     </Phone> 
     <Email>[email protected]</Email> 
    </Identification>  
    </Pro> 
</Physical> 

腳本:

我試圖改變變量的值,並打印剩下的一個新的輸出xml文件

use strict; 
use warnings; 
use XML::Simple; 
use Data::Dumper;  

    my $xml = XML::Simple->new(ForceContent => 1,); 
    my $xmlData = $xml->XMLin('input.xml') or die$!;  

    print Dumper (\$xmlData); 

    foreach my $xmlKey (keys %{$xmlData}){ 
    if ($xmlKey =~ m/Pro/){ 
     print ${$xmlData}{$xmlKey}{Identification}{Address}{Data}="hello"; 
    } 
    } 

XMLout(
    $xmlData, 
    KeepRoot => 1, 
    NoAttr => 0, 
    OutputFile => $xml_out, 
); 

Outout XML:

<opt xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <Pro managementID="22000020"> 
    <Identification Type="XXXXX"> 
     <Address Data="hello" /> 
     <Email>[email protected]</Email> 
     <Phone name="Number">0000</Phone> 
    </Identification> 
    </Pro> 
</opt> 

我能夠改變這個值,但是當我試圖將數據寫入輸出時,格式已經改變了。任何一個人都可以指導我得到相同的輸入數據,並將改變後的值作爲輸出。

+2

這就是爲什麼你不使用XML ::簡單的輸出。 – ikegami

回答

2

使用不同的XML處理模塊。例如,該腳本使用XML::XSH2,圍繞XML::LibXML包裝:

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

use XML::XSH2; 

xsh << 'END'; 
    open input.xml ; 
    for //*[xsh:matches(name(),'Pro')]/Identification/Address/Data 
     set . 'hello' ; 
    save :b ; 
END 
4

使用XML ::的libxml這樣:

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

use XML::LibXML; 

my $input; 
while(<>) { 
    $input .= $_; 
} 
my $xml_doc = XML::LibXML->load_xml(string => $input); 
my $xpath_ctx = new XML::LibXML::XPathContext($xml_doc); 
$xpath_ctx->find("/Physical/Pro/Identification/Address/Data")->get_node(0)->firstChild()->setData("hello"); 
my $xml_data = $xpath_ctx->find("/")->get_node(0)->toString(1); 

print $xml_data; 

XML ::的libxml是更快,能夠使用XPath的幫助下,對$xml_doc的操縱要容易得多。

更多相關信息,您可以找到here

+0

爲什麼不使用'load_xml(location =>'input.xml')'? – choroba

+0

@choroba沒有任何理由......你可以使用它,因爲你喜歡 – memosdp

+0

原因是簡單。 – choroba

2

的XML ::嫩枝版本:

#!/usr/bin/perl 

use strict; 
use warnings; 

use XML::Twig; 

XML::Twig->new(twig_roots => { 'Pro/Identification/Address/Data' => sub { $_->set_text('hello'); $_->flush; } }, 
       twig_print_outside_roots => 1, 
      ) 
     ->parsefile('input.xml'); 
1

另一種方式W/XML ::規則

use strict; 
use warnings; 

use XML::Rules; 

my @rules = (
    Data => sub { $_[1]{_content} =~ s/test/hello/; return $_[0] => $_[1] }, 
); 
my $xr = XML::Rules->new(
    rules => \@rules, 
    style => 'filter', 
); 

$xr->filterfile('input.xml'); 
0

你有NoAttr集輸出爲零。

這不就是你想要的嗎?

NoAttr => 1

當與XMLout(使用),將所生成的XML將不包含屬性。 所有散列鍵/值將被表示爲嵌套元素。

NoAttr將停止

<Phone> 
    <Number>0000</Number> 

變成

<Phone name="Number">