in reply to Retrieving an XML node value with XML::DOM::Lite and XML::Parser

I would recommend that you switch to XML::LibXML. This is how you would do the same thing using that module. Note that I had to add a Root tag to surround the XML as it will give an error otherwise. Note also the different Xpath expressions used as a result.
use XML::LibXML; my $string = q| <Root> <A > <B>B value</B> <C> <D d_attribute="d_attribute_value">D_Value</D> </C> </A > <A> <B>B value</B> <C> <D d_attribute="d_attribute_value">D_Value</D> </C> </A> </Root> |; my $doc = XML::LibXML->load_xml(string => $string); my @B = $doc->findnodes("//A/B"); if (@B) { for my $node (@B) { my $val = $node->textContent; print qq|Value of B is: $val\n|; } } my @D = $doc->findnodes("//A/C/D"); if (@D) { for my $node (@D) { my $attr = $node->getAttribute("d_attribute"); print qq|Attribute 'd_attribute' of D is: $attr\n|; my $val = $node->textContent; print qq|Value of D is: $val\n|; } }
  • Comment on Re: Retrieving an XML node value with XML::DOM::Lite and XML::Parser
  • Download Code