Yes, I can do this with SimpleXML, but for what I'm ultimately going to try to do getElementById with PHP DOM will work a lot better.
Main Topics
Browse All TopicsI'm trying to use php dom's getElementById, and I'm just doing the basically tutorial, but it doesn't work. I just get the:
The element whose id is books is:
as my output.
My book.xml code:
<books>
TEST BOOK
</books>
and my php code:
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I'm trying to do the tutorial here:
http://theserverpages.com/
but it doesn't say what the code for book.xml should be, and it seems to me like I'm writing it wrong.
Maybe this will be helpful.
http://us3.php.net/manual/
Also, you might want getElementsByTagName instead of looking for the ID.
>> Is there a way to have it return TEST BOOK or TEST BOOK2
Use the "nodeValue" property:
http://php.net/manual/en/
That's working really well. I tried using an .html file, instead of an .xml, and it works fine (I'm swapping xml text in for html text).
The issue I'm having though is if there are any links or inner divs inside, it seems that this won't work if there are any interior <> . Is there a way to to make it work with <>?
The example in the manual does not work for me either. This excerpt from the manual gives a hint, though:
" For this function to work, you will need either to set some ID attributes with DOMElement::setIdAttribute
defines an attribute to be of type ID. In the later case, you will need to validate your document with
DOMDocument::validate or DOMDocument->validateOnPars
http://php.net/manual/en/d
The book.xml used in the manual probably has a DTD, that is why validateOnParse is set to true in the example. Without a DTD, you need to use setIdAttribute() if you are going to use getElementById().
That worked fine. Then I tried to copy and paste the source code from www.w3.org into a file called "test.html", and put it in the same folder as my php code, and I got:
The element whose id is slogan is:
NULL
The element with id=logo is a h1 element, and it contains an img element.
To get the child element of the h1 element, you can use the firstChild property. To extract an element as a string, you can use the saveXML() method of the DOMDocument object. To see the string in a html context, you must use htmlentities().
Try this:
That h1 element contains multiple child elements: A text node with value "This is a ", an anchor element containing another text node with value "link", and finally a text node with value " test".
If you wanted to include the h1 tags and the content in your output, it would be easier, you could just do
$s = $doc->saveXML($logo);
To get just the content of the h1 element, you must loop and output each of the child nodes:
The worked well. I tried to incorporate it into a search and replace, where the header is replaced by text from an .xml file. This works really well, EXCEPT, if there is a link.
So this
<h1 id="logo">This is a link test</h1>
Will get changed, but this won't
<h1 id="logo">This is a <a href="link.html">link</a> test</h1>
Even though they both output with the echo statement fine.
It seems to me the first version was correct... the syntax for str_replace is:
str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
The string to search for comes first, and the string to insert comes second.
If you can't figure it out, please open a new question. This question was about using getElementById(). Thank you! :)
Btw, I said earlier that malformed html would not work well, I forgot about this method:
http://php.net/manual/en/d
Business Accounts
Answer for Membership
by: Ray_PaseurPosted on 2009-05-26 at 10:55:28ID: 24475702
Just curious - have you tried using SimpleXML instead?