...">

How to list XML node attributes using XML :: LibXML?

Given the following XML fragment:

<outline> <node1 attribute1="value1" attribute2="value2"> text1 </node1> </outline> 

How to get this conclusion?

 outline node1=text1 node1 attribute1=value1 node1 attribute2=value2 

I looked at use XML::LibXML::Reader; , but this module only provides access to the value attributes referenced by their names. And how do I get a list of attribute names in the first place?

+5
source share
2 answers

You will find a list of attributes by doing $e->findnodes( "./@*");

Below is a solution with plain XML :: LibXML, not with XML :: LibXML :: Reader that works with your test data. It may be sensitive to extra spaces and mixed content, so before using it, check it on real data.

 #!/usr/bin/perl use strict; use warnings; use XML::LibXML; my $dom= XML::LibXML->load_xml( IO => \*DATA); my $e= $dom->findnodes( "//*"); foreach my $e (@$e) { print $e->nodeName; # text needs to be trimmed or line returns show up in the output my $text= $e->textContent; $text=~s{^\s*}{}; $text=~s{\s*$}{}; if( ! $e->getChildrenByTagName( '*') && $text) { print "=$text"; } print "\n"; my @attrs= $e->findnodes( "./@*"); # or, as suggested by Borodin below, $e->attributes foreach my $attr (@attrs) { print $e->nodeName, " ", $attr->nodeName. "=", $attr->value, "\n"; } } __END__ <outline> <node1 attribute1="value1" attribute2="value2"> text1 </node1> </outline> 
+4
source

Something like this should help you.

It is not clear from your question whether the <outline> root data element, or if it is buried somewhere in the larger document. It is also unclear how usually you want the solution to be - for example, do you want the entire document to be reset in this way?

In any case, this program generates the result that you requested from this XML input, rather concise.

 use strict; use warnings; use 5.014; #' For /r non-destructive substitution mode use XML::LibXML; my $xml = XML::LibXML->load_xml(IO => \*DATA); my ($node) = $xml->findnodes('//outline'); print $node->nodeName, "\n"; for my $child ($node->getChildrenByTagName('*')) { my $name = $child->nodeName; printf "%s=%s\n", $name, $child->textContent =~ s/\A\s+|\s+\z//gr; for my $attr ($child->attributes) { printf "%s %s=%s\n", $name, $attr->getName, $attr->getValue; } } __DATA__ <outline> <node1 attribute1="value1" attribute2="value2"> text1 </node1> </outline> 

Exit

 outline node1=text1 node1 attribute1=value1 node1 attribute2=value2 
+5
source

Source: https://habr.com/ru/post/1206395/


All Articles