Is there any way how to keep tags of nodes from xml code? Explanation of the problem could be like this: I have an xml input, I query the input and I want to get xml output of the queried items.
<pets>
<dog>
<name>Maggie</name>
<dob>12 October 2005</dob>
<price>75</price>
<owner>Rosie</owner>
</dog>
</pets>
When I access //pets/dog
in my Perl program, the output is as follows:
<?xml version='1.0' standalone='yes'?>
<dog>
Maggie
12 October 2005
75
Rosie
</dog>
Is there any way to give the function textContent
some parameter
to keep the tags? This is a block of my cycle code that puts the nodes
into hash, then outputs it:
$parser = XML::LibXML->new();
$data = $parser->load_xml(string => $takeninput);
$xml = new XML::LibXML::XPathContext($data);
$i = 0;
for $node ($xml->findnodes('//pets/dog'))
{
$name = $node->nodeName;
$hash{$name}[$i] = $node->textContent;
$i++;
}
To keep the tags of children? To have the output like this:
<?xml version='1.0' standalone='yes'?>
<dog>
<name>Maggie</name>
<dob>12 October 2005</dob>
<price>75</price>
<owner>Rosie</owner>
</dog>
Thank you for answers.
The library is doing exactly what you asked - it is returning the text content of the node. Markup isn't text content so it doesn't give it to you.
What I think you want is $node->toString
, which will return the dog
node as formatted XML.
For a pretty layout, use $node->toString(1)