33

HI i am using ajax for a web site which displays courses available, i am having a problem where i am reading xml(which has courses details) getting node value(course name) comparing to my input(inout course name) if it is equal i am displaying the text(course description). now i am tracing it getting to correct course name(in xml file) but while comparing it to input course name its not at all comparing so.at one point i loaded the xml file and echo it:

$doc->load('data/ICT.xml'); 
echo"$doc";

It gives me an error

Catchable fatal error: Object of class DOMDocument could not be converted to string in /home/students/....../www/htdocs/client/unit_details.php on line 23

so what i understood from this is that xml dom object should be converted to string so that i can get required data and use it, is it true? if so can some one tell me how to do so please(like any functions etc) thanks in advance :-)

1
  • 1
    DOMDocument implements the DOM API - access nodes via things like getElementById(), getElementsByClassName(), etc, and use saveXML() to write out a string. Commented Oct 20, 2012 at 12:25

4 Answers 4

45

The DOMDocument object can't be used as a string.

Here is how you would display the DOMDocument object as an XML string:

echo $doc->saveXML();
Sign up to request clarification or add additional context in comments.

Comments

20

DOMDocument has a method SaveHTML(), it's designed to do exactly what you need.

You may display whole document by:

$doc->saveHTML();

Or just certain node:

$doc->saveHTML( $bodyNode);

DOMDocument also provides few options how to enhance how the XML is print, such as:

Comments

6

Try this:

$xml = $doc->saveXML($doc->documentElement);

Comments

-3

You can use Type casting

$doc->load('data/ICT.xml'); 
$doc2=(string)$doc;
echo $doc2;

Or You can use the PHP function

strval();

This function returns the string values of the parameter passed to it.

1 Comment

You should better read the question before answering it: "Catchable fatal error: Object of class DOMDocument could not be converted to string in /home/students/....../www/htdocs/client/unit_details.php on line 23"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.