473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

Feb 22 '06 #1
4 2004


Skeets wrote:
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";
}

--

Martin Honnen
http://JavaScript.FAQTs.com/
Feb 22 '06 #2
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

Feb 23 '06 #3


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


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/
Feb 23 '06 #4
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.

Feb 23 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Christoph Nothdurfter | last post by:
Hallo! I was wondering if my PHP4-Scripts will run under PHP5 (Haeven't tried the beta yet). Does anybody know? Thank you, -Christoph
6
by: somaBoy MX | last post by:
I'm configuring PHP5 on my WinXP pro machine as an Apache2 handler. However when I start apache I get system errormessages like "PHP Shutdown: Unable to load dynamic library...
1
by: Pedro Fonseca | last post by:
Greetings everyone! I'm porting my applications to PHP5 and I've stumbled on yet another problem. I'll try to simplify things a bit. I have a main script that is being executed (index.php, PHP5...
0
by: Joe | last post by:
Hey everyone I'm trying to use php to extract data from an AS/400 system. The php engine is running locally on my workstation(win2000) and I'm accessing the DB2 database on the AS/400 system via...
4
by: Daniel Andersen | last post by:
Hey, We are in the process of rewriting our internal management system (which was written in PHP4), and figured this would be a good time to migrate to PHP5 to get all the OO goodness it has to...
2
by: Stefan Huber | last post by:
Hi I've got a really strange problem, and can't find out why it's not working as intended. in order to use php4 and 5 together on a webserver and the requirement for running as different...
2
by: mananvyas | last post by:
Hi, I am working on PHP5 and all the xmldom related extensions are installed and on properly. I am trying to execute following code but some how not working. $doc = new DomDocument();...
0
by: cty | last post by:
Title: Problem in session using php5 Good day, I use php5+mySQL4+IIS5.x Previuosly i use php4 and no error occur,
3
by: xhe | last post by:
I have just upgraded my php version form php4 to php5. and I met this problem, and don't know if you know the solution. My site was written in PHP4, and most parts can be running smoothly in PHP5,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.