Something like this should help you.
It's not clear from your question whether <outline>
is the root element of the data, or if it is buried somewhere in a bigger document. It's also unclear how general you want the solution to be - e.g. do you want the entire document dumped in this manner?
Anyway, this program generates the output you requested from the given XML input in a fairly concise manner.
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>
output
outline
node1=text1
node1 attribute1=value1
node1 attribute2=value2
my @attrs = $e->attributes
, which returns a list of all attribute nodes, but an element node object also behaves as a tied hash reference, andkeys %$e
will return all of the attribute names while$e->{attr_name}
will return the value of attributeattr_name
. – Retention