Java JDOM Document toString() Method



The Java JDOM toString() method of Document class is used to get the string representation of the document.

Syntax

Following is the syntax of the Java JDOM Document toString() method −

Document.toString();

Parameters

The Java setRootElement() method doesn't accept any parameters.

Return Value

The Java setRootElement() method returns the document as a String object.

Example 1

Let us see a basic example of using the Java JDOM Document toString() method on an XML document. In the following program, we have created a new document, added root using addContent() method. Then, we have used toString() method on the document and printed it on the console.

import org.jdom2.Document;
import org.jdom2.Element;

public class ConvertToString {
   public static void main(String args[]) {
      try {	
    	 //Create a new Document
	     Document doc = new Document();
	     Element root = new Element("fooditems").setText("List of foods");
	     doc.addContent(root);
	     
	     //Printing the document
	     String doc_string = doc.toString();
	     System.out.println(doc_string);      
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays the document information as a string.

[Document:  No DOCTYPE declaration, Root is [Element: <fooditems/>]]

Example 2

We need to print the content of the following fooditems.xml file −

<?xml version = "1.0"?>
<!-- Information of food items -->
<fooditems>
	<item>
		<name>Biriyani</name>
		<price>120</price>
	</item>
		<item>
		<name>Omelet</name>
		<price>60</price>
	</item>
		<item>
		<name>Fried rice</name>
		<price>100</price>
	</item>
</fooditems>

The output() method of XMLOutputter class in Java JDOM library is used to print the entire content of an XML document. The setFormat() method of this class is used to get the XML representation of the document.

In the following program, we have used both toString() and output() methods to observe the difference between the two functions.

import java.io.File;
import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class ConvertToString {
   public static void main(String args[]) {
      try {	
    	 //Reading the XML file
  		 SAXBuilder saxBuilder = new SAXBuilder();
  		 File inputFile = new File("fooditems.xml");			          
  		 Document doc = saxBuilder.build(inputFile);
  		 
  		 //Printing document using toString
	     String doc_string = doc.toString();
	     System.out.println("-------Using toString() method---------");
	     System.out.println(doc_string);
	     
	     //Print document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     System.out.println("\n"+"-------Using XMLOutputter---------");
         xmlOutput.output(doc, System.out);        
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays the content of the document using two functions.

-------Using toString() method---------
[Document:  No DOCTYPE declaration, Root is [Element: <fooditems/>]]

-------Using XMLOutputter---------
<?xml version="1.0" encoding="UTF-8"?>
<!-- Information of food items -->
<fooditems>
  <item>
    <name>Biriyani</name>
    <price>120</price>
  </item>
  <item>
    <name>Omelet</name>
    <price>60</price>
  </item>
  <item>
    <name>Fried rice</name>
    <price>100</price>
  </item>
</fooditems>
Advertisements