Connecting Tech Pros Worldwide Forums | Help | Site Map

GetElementById PHP5 DOM Problem

Skeets
Guest
 
Posts: n/a
#1: Feb 22 '06
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...


Martin Honnen
Guest
 
Posts: n/a
#2: Feb 22 '06

re: GetElementById PHP5 DOM Problem




Skeets wrote:
[color=blue]
> 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?[/color]

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";
}



--

Martin Honnen
http://JavaScript.FAQTs.com/
Skeets
Guest
 
Posts: n/a
#3: Feb 23 '06

re: GetElementById PHP5 DOM Problem


Thanks Martin.

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

i used the example here:

http://us2.php.net/manual/en/functio...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

Martin Honnen
Guest
 
Posts: n/a
#4: Feb 23 '06

re: GetElementById PHP5 DOM Problem




Skeets wrote:

[color=blue]
> i'm receiving an error when tryiong to use xpath.[/color]

I think you said you use PHP 5. My earlier post contains an example that
works with PHP 5.
[color=blue]
> i used the example here:
>
> http://us2.php.net/manual/en/functio...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[/color]

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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Skeets
Guest
 
Posts: n/a
#5: Feb 23 '06

re: GetElementById PHP5 DOM Problem


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...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.

Closed Thread