By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,836 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,836 IT Pros & Developers. It's quick & easy.

Strange structure to be parsed

P: n/a
Hi.

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?

Thank you very much.

Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* 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.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Jul 20 '05 #2

P: n/a
There are so many tools to do that ... if I gave you a solution you
probably wouldn't be interested in it.

Jul 20 '05 #3

P: n/a
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
Jul 20 '05 #4

P: n/a
On 15 Feb 2005 16:04:54 -0800, fr**********@europe.com wrote:
Any suggestion?


Redesign the schema. That sort of mixed content is always a pain to
work with.

Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.