GetElementById PHP5 DOM Problem

S

Skeets

When the data structure looks like this:

<page>
<point id="1">
<premise> 1+1=2 </premise>
</point>
<point id="2">
<premise> round is better than square </premise>
</point>
<thought id="1">
<note> I like Fridays </note>
</thought>
<thought id="2">
<note> rsaturday's rock </note>
</thought>
</page>

how do i use GetElementById() to get thought id 2's note?

i saw some examples where the id was unique within the xml. in my
case, though, the id isn't unique and i couldn't google up a single
example. te syntax isn't intuitive to me so i haven't been able to
work it out on my own, either.

tia...
tia...
 
M

Martin Honnen

Skeets said:
When the data structure looks like this:

<page>
<point id="1">
<premise> 1+1=2 </premise>
</point>
<point id="2">
<premise> round is better than square </premise>
</point>
<thought id="1">
<note> I like Fridays </note>
</thought>
<thought id="2">
<note> rsaturday's rock </note>
</thought>
</page>

how do i use GetElementById() to get thought id 2's note?

getElementById on XML is only going to work if there is a DTD that
defines the attributes of type ID. And ID attribute values are required
to be unique in the document. And ID attribute values have to begin with
a letter so your values starting with/consisting of a digit are not
valid ID values.

I guess you could use XPath to look for point elements with @id being 2 e.g.
/page/thought[@id = '2']/note/text()
with PHP 5

$xPathEvaluator = new DOMXPath($xmlDocument);
$nodeList = $xPathEvaluator->query(
"/page/thought[@id = '2']/note/text()", $xmlDocument);
foreach ($nodeList as $node) {
echo $node->data . "<br>\r\n";
}
 
M

Martin Honnen

Skeets wrote:

i'm receiving an error when tryiong to use xpath.

I think you said you use PHP 5. My earlier post contains an example that
works with PHP 5.
i used the example here:

http://us2.php.net/manual/en/function.xpath-eval-expression.php

i cipied and pasted it and it get the following error:

PHP Fatal error: Call to undefined function domxml_open_mem() in
C:\web\html\dmt\_debug_tmp.php on line 497

That stuff is part of a PHP 4 only extension for DOM and XPath. You
can't use that with PHP 5 and you really don't want to.
If you are using PHP 4 now and want to use that code then you need to
make sure that DOM XML extension for PHP 4 is installed.
 
S

Skeets

Martin, for some reason i was having lots of trouble.

i eventually found out how to do everything i needed, and more, in
PHP5's simplexml.

this tutorial is excellent:

http://www.isolated-designs.net/core/article/25/simplexml-and-xpath-in-php-5

the author switched from

simplexml_load_file('applications.xml');

to

$apps = simplexml_load_string($xml);

midstream - with no comment. for the newbs, you only need one or the
other - depending on if you are importing a file or working with a
string (included from another file or actually in the same file).

back to my problem, the solution is as simple as:

<?php
$page = simplexml_load_string($xml_string);
$thought = $page->xpath("//thought[@id = 2]");
echo $thought[0]->note;
?>

this should print the note, although i don't have time to test this
code. i did test the code in the tutorial and it worked liked a charm.

thanks for helping - i'm not sure what i was doing to gaff things, but
this method is the simplest around. being a newb, i like that a lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,255
Members
46,852
Latest member
CarlaDowle

Latest Threads

Top