Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting xml DOMDocument to string

Tags:

ajax

php

xml

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 :-)

like image 362
Mallikarjuna Rao Kolluri Avatar asked Oct 20 '12 12:10

Mallikarjuna Rao Kolluri


People also ask

Which method converts DOMDocument object into String?

DOMDocument implements the DOM API - access nodes via things like getElementById(), getElementsByClassName() , etc, and use saveXML() to write out a string.

How do I convert a file to String in Java?

Document convertStringToDocument(String xmlStr) : This method will take input as String and then convert it to DOM Document and return it. We will use InputSource and StringReader for this conversion. String convertDocumentToString(Document doc) : This method will take input as Document and convert it to String.

How do I read an XML String in Java?

In this article, you will learn three ways to read XML files as String in Java, first by using FileReader and BufferedReader, second by using DOM parser, and third by using open-source XML library jcabi-xml.


2 Answers

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(); 
like image 117
noetix Avatar answered Oct 05 '22 19:10

noetix


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:

  • preserveWhiteSpaces
  • formatOutput
  • encoding
  • ...
like image 24
Vyktor Avatar answered Oct 05 '22 21:10

Vyktor