Bjoern Hoehrmann wrote:
* fr**********@europe.com wrote in comp.text.xml:
I want to parse this XML file:
-----------------
<doc>
<item>
<name>John Morrow</name>
<contact type="email">jo**@john.com</contact>
Blah blah
</item>
</doc>
--------------------
I'm able to get 'John Morrow':
$doc->getDocumentElement()->getElementsByTagName('item')->
getElementsByTagName('name')->item(0)->getFirstChild->getNodeValue;
But I don't know to get 'Blah Blah'. Any suggestion?
Well, it's the getLastChild of the item element, so something like
$doc
->getDocumentElement()
->getElementsByTagName('item')
->item(0)
->getLastChild
->getNodeValue;
should do, depending on whether your implementation supports such a
method. You did not say what you are using so I have to guess... Note
that e.g. XPath might be a better way to query for such information.
This does it, although I'd welcome critiques of the XPath expression:
--------------------- snip -------------------------
use XML::LibXML;
$parser = XML::LibXML->new();
$doc = $parser->parse_file("junkfile.xml");
$root = $doc->documentElement();
($node) = $root->findnodes("//item/child::node()[position()=last()]");
print "\"", $node->nodeValue, "\"\n";
--------------------- snip --------------------------
It prints:
"
Blah blah
"
Paul Allen