Java JDOM Element toString() Method



The Java JDOM toString() method of Element class is used to get the string representation of an XML Element that helps in debugging purpose. To get the XML representation of the element, XMLOutputter class can be used.

Syntax

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

Element.toString();

Parameters

The Java Element.toString() method doesn't accept any parameter.

Return Value

The Java toString() method returns string representation of the element.

Example 1

Here is the basic example of using the Java JDOM Element toString() method −

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

public class ToString {
   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 root element
	     String ele_string = root.toString();
	     System.out.println(ele_string);      
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The string representation of the root element is displayed.

[Element: <fooditems/>]

Example 2

We need to parse 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 toString() method only gives the local name of the element, to get the XML representation of the element, XMLOutputter class can be used.

The following example shows the difference between the result given by toString() method and XMLOutputter class.

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

public class ToString {
   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);
  		 Element root =  doc.getRootElement();		 
  		 //Printing root using toString
	     String ele_string = root.toString();
	     System.out.println("Using toString() method");
	     System.out.println(ele_string);
	     //Print root using XMLOutputter
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     System.out.println("\n"+"Using XMLOutputter");
         xmlOutput.output(root, System.out);        
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }  
}

The result obtained from toString() method and XMLOutputter are displayed.

Using toString() method
[Element: <fooditems/>]

Using XMLOutputter
<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