Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old February 22nd, 2006, 07:55 PM
Skeets
Guest
 
Posts: n/a
Default GetElementById PHP5 DOM Problem

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

  #2  
Old February 22nd, 2006, 08:15 PM
Martin Honnen
Guest
 
Posts: n/a
Default 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/
  #3  
Old February 23rd, 2006, 07:55 PM
Skeets
Guest
 
Posts: n/a
Default 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

  #4  
Old February 23rd, 2006, 08:15 PM
Martin Honnen
Guest
 
Posts: n/a
Default 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/
  #5  
Old February 23rd, 2006, 11:25 PM
Skeets
Guest
 
Posts: n/a
Default 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.

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles