xml-twig

XML::Twig and save order of attributes

混江龙づ霸主 提交于 2020-06-27 15:59:05
问题 I am editing some XML files using XML::Twig below are the code : my $twig = XML::Twig->new( pretty_print => 'indented', twig_handlers => { Vendor => sub { $_->set_att( 'ID' => $_->{'att'}->{'att1'} ); $_->set_att( 'ID' => $_->{'att'}->{'att2'} ); $_->set_att( 'ID' => $_->{'att'}->{'att3'} ); $_->set_att( 'ID' => $_->{'att'}->{'att4'} ); }, }, ); $twig->parsefile('myfile'); $twig->flush; The problem is that this code does not save the xml attributes in the same order in the edited file. for

How do I add child elements to XML using Perl's XML::Twig?

拜拜、爱过 提交于 2020-01-23 14:35:08
问题 I have the following XML file: <SOURCE_SERVER> <SERVER HOSTNAME="serv1"> <CIFS_SERVICE NETBIOSNAME="serv1"/> </SERVER> <SERVER HOSTNAME="serv2"> </SERVER> <SOURCE_SERVER> Now, I want to add child <CIFS_SERVICE NETBIOSNAME="serv2"/> to <SERVER HOSTNAME="serv2"> . How do I do this using XML::Twig? 回答1: Here is a solution incorporating what I think are reasonable fixes to your question. The code below is based on the filtering example given in the documentation. #!/usr/bin/perl use strict; use

How do I add child elements to XML using Perl's XML::Twig?

馋奶兔 提交于 2020-01-23 14:32:32
问题 I have the following XML file: <SOURCE_SERVER> <SERVER HOSTNAME="serv1"> <CIFS_SERVICE NETBIOSNAME="serv1"/> </SERVER> <SERVER HOSTNAME="serv2"> </SERVER> <SOURCE_SERVER> Now, I want to add child <CIFS_SERVICE NETBIOSNAME="serv2"/> to <SERVER HOSTNAME="serv2"> . How do I do this using XML::Twig? 回答1: Here is a solution incorporating what I think are reasonable fixes to your question. The code below is based on the filtering example given in the documentation. #!/usr/bin/perl use strict; use

How do I add child elements to XML using Perl's XML::Twig?

只谈情不闲聊 提交于 2020-01-23 14:30:06
问题 I have the following XML file: <SOURCE_SERVER> <SERVER HOSTNAME="serv1"> <CIFS_SERVICE NETBIOSNAME="serv1"/> </SERVER> <SERVER HOSTNAME="serv2"> </SERVER> <SOURCE_SERVER> Now, I want to add child <CIFS_SERVICE NETBIOSNAME="serv2"/> to <SERVER HOSTNAME="serv2"> . How do I do this using XML::Twig? 回答1: Here is a solution incorporating what I think are reasonable fixes to your question. The code below is based on the filtering example given in the documentation. #!/usr/bin/perl use strict; use

How can I add an attribute to a child element using Perl's XML::Twig?

自古美人都是妖i 提交于 2020-01-15 06:08:09
问题 I have an XML string like this: <DATA> <CHILD_DATA ATVAL="value1"/> <CHILD_DATA /> </DATA> The final output I want is: <DATA> <CHILD_DATA ATVAL="value1"/> <CHILD_DATA ATVAL="value2"/> </DATA> My twig $t is at <DATA> . Now I want to add an attribute to the second <CHILD_DATA /> . The attribute is ATVAL="value2" . I tried the following: $t->last_child('CHILD_DATA')->set_att{"ATVAL","value2"}; This didn't work. What's wrong with this code? Is there another way to do this? 回答1: As Jon hinted to

Purge XML Twig inside sub handler

谁说胖子不能爱 提交于 2020-01-03 09:03:19
问题 I am parsing large XML files (60GB+) with XML::Twig and using it in a OO (Moose) script. I am using the twig_handlers option to parse elements as soon as they're read into memory. However, I'm not sure how I can deal with the Element and Twig. Before I used Moose (and OO altogether), my script looked as follows (and worked): my $twig = XML::Twig->new( twig_handlers => { $outer_tag => \&_process_tree, } ); $twig->parsefile($input_file); sub _process_tree { my ($fulltwig, $twig) = @_; $twig-

Purge XML Twig inside sub handler

一曲冷凌霜 提交于 2020-01-03 09:02:30
问题 I am parsing large XML files (60GB+) with XML::Twig and using it in a OO (Moose) script. I am using the twig_handlers option to parse elements as soon as they're read into memory. However, I'm not sure how I can deal with the Element and Twig. Before I used Moose (and OO altogether), my script looked as follows (and worked): my $twig = XML::Twig->new( twig_handlers => { $outer_tag => \&_process_tree, } ); $twig->parsefile($input_file); sub _process_tree { my ($fulltwig, $twig) = @_; $twig-

Perl, XML::Twig, how to reading field with the same tag

扶醉桌前 提交于 2020-01-02 03:39:10
问题 I'm working on processing a XML file I receive from a partner. I do not have any influence on changing the makeup of this xml file. An extract of the XML is: <?xml version="1.0" encoding="UTF-8"?> <objects> <object> <id>VW-XJC9</id> <name>Name</name> <type>House</type> <description> <![CDATA[<p>some descrioption of the house</p>]]> </description> <localcosts> <localcost> <type>mandatory</type> <name>What kind of cost</name> <description> <![CDATA[Some text again, different than the first tag]

Copy XML value using XML twig perl

北城余情 提交于 2019-12-25 01:48:55
问题 I have a nested XML tags and need to have the value of ExternalId in Product XML to a ProductPageURL tag using XML Twig <Products> <Product> <ExternalId>317851</ExternalId> <ProductPageUrl></ProductPageUrl> </Product> <Product> <ExternalId>316232</ExternalId> <ProductPageUrl></ProductPageUrl> </Product> <Product> <ExternalId>13472</ExternalId> <ProductPageUrl></ProductPageUrl> </Product> </Products> Expected result: <Products> <Product> <ExternalId>PF317851</ExternalId> <ProductPageUrl>317851

How can I extract some XML data from a URL using XML::Twig?

女生的网名这么多〃 提交于 2019-12-23 12:42:38
问题 I want to get a specific string, for example 123 in <received>123</received> from some XML that will be retrieved from a URL. I have write a code but stuck with an error message: Attempt to bless into a reference at /usr/share/perl5/XML/Twig.pm line 392. How can I solve it? The code: use XML::Twig; use LWP::Simple; my $url = 'http://192.168.1.205:13000/status.xml'; my $twig = new XML::Twig(TwigRoots => { 'smsc/received' => sub {$author = $_[1]->text; }}); $twig->nparse( $url ); $twig->print;