
- Java XML Home
- Java XML Overview
- Java XML Parsers
- Java DOM Parser
- Java DOM Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java SAX Parser
- Java SAX Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- JDOM XML Parser
- JDOM XML Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java StAX Parser
- Java StAX Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java XPath Parser
- Java XPath Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java DOM4J Parser
- Java DOM4J Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java XML Useful Resources
- Java XML - Questions and Answers
- Java XML - Quick Guide
- Java XML - Useful Resources
- Java XML - Discussion
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>