Category: 17. Java XML

https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSyAwuaRGlDJoi0l4ODVUyPWH2fCb7MkTvp-w&s

  • Modify XML Document

    Java DOM4J API provides methods to modify XML documents. We might come across situations where we need to update few details in a large XML document. For example, an e-commerce website has many products whose price may vary on a daily or monthly basis. In such cases, modifying the already existing file makes the job simpler than to create the XML documents again. In this chapter, we are going to look at some examples to learn how to modify existing XML documents using DOM4J API.

    Modify XML Using Java DOM4J Parser

    Following are the steps to modify XML documents using Java DOM4J Parser −

    • Step 1: Creating SAXReader object
    • Step 2: Reading the XML file
    • Step 3: Parsing the XML
    • Step 4: Extracting the root
    • Step 5: Modifying the elements
    • Step 6: Creating a FileOutputStream
    • Step 7: Writing the updated XML document into file
    • Step 8: Printing the XML document

    Step 5: Modifying the elements

    After extracting the root element in step 4, we can get any of its child elements by using the elements() method. We can modify already existing elements by editing their text content, changing attribute values, adding new attributes etc.

    Modify Text Content

    To modify text content of an element, we can use the method setText() to set the text content. It takes a string as an argument and updates the old text content with the new text content. This method is available in both Node and Element interfaces.

    Example

    In the following studentData.xml file, We need to update marks from 80 to 64 for the student with roll number 493.

    <?xml version="1.0" encoding="UTF-8"?><class><student rollno="393"><firstname>dinkar</firstname><lastname>kad</lastname><nickname>dinkar</nickname><marks>85</marks></student><student rollno="493"><firstname>Vaneet</firstname><lastname>Gupta</lastname><nickname>vinni</nickname><marks>80</marks></student><student rollno="593"><firstname>jasvir</firstname><lastname>singn</lastname><nickname>jazz</nickname><marks>90</marks></student></class>

    The following ModifyTextContent.java program reads the above studentData.xml file using SAXReader. Using elementIterator(“student”) method, we got all the student elements in an Iterator. We iterate all the elements to find the student with roll number 493 using attributeValue() method and updates the text content of marks element.

    importjava.io.File;importjava.io.FileOutputStream;importjava.util.Iterator;importorg.dom4j.Document;importorg.dom4j.Element;importorg.dom4j.io.OutputFormat;importorg.dom4j.io.SAXReader;importorg.dom4j.io.XMLWriter;publicclassModifyTextContent{publicstaticvoidmain(String[] args){try{//Creating SAXReaderSAXReader reader =newSAXReader();//Reading the XML fileFile inputFile =newFile("studentData.xml");//Parsing the XMLDocument document = reader.read(inputFile);//Extracting the rootElementRootElement= document.getRootElement();//Modifying the elementsIterator<Element> students =RootElement.elementIterator("student");while(students.hasNext()){Element student = students.next();if(student.attributeValue("rollno").equals("493")){Element marks = student.element("marks");
    
        	   marks.setText("64");}}//Creating a FileOutputStreamFileOutputStream newFile =newFileOutputStream("studentData.xml");//Writing the updated XML document into fileXMLWriter writer =newXMLWriter(newFile);
         writer.write( document );//Printing the XML documentOutputFormat format =OutputFormat.createPrettyPrint();XMLWriter consoleWriter =newXMLWriter(System.out, format );
         consoleWriter.write( document );}catch(Exception e){
    	 e.printStackTrace();}}}</code></pre>

    Output

    Marks for the student with roll number 493 is updated from 80 to 64.

    <?xml version="1.0" encoding="UTF-8"?><class><student rollno="393"><firstname>dinkar</firstname><lastname>kad</lastname><nickname>dinkar</nickname><marks>85</marks></student><student rollno="493"><firstname>Vaneet</firstname><lastname>Gupta</lastname><nickname>vinni</nickname><marks>64</marks></student><student rollno="593"><firstname>jasvir</firstname><lastname>singn</lastname><nickname>jazz</nickname><marks>90</marks></student></class>

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Add New Elements

    The addElement() method adds new elements to an existing XML document at the end of all the already existing child elements of that current element. We need to pass the tag name of the element as an argument. Similarly, the addText() method adds text content to the element. The addAttribute() adds new attribute to the current element. We need to pass attribute name and attribute value as arguments.

    The following AddNewElements.java program reads the studentData.xml file we have used in the previous example and uses the above methods to add information of a new student.

    importjava.io.File;importjava.io.FileOutputStream;importorg.dom4j.Document;importorg.dom4j.Element;importorg.dom4j.io.OutputFormat;importorg.dom4j.io.SAXReader;importorg.dom4j.io.XMLWriter;publicclassAddNewElements{publicstaticvoidmain(String[] args){try{//Creating SAXReaderSAXReader reader =newSAXReader();//Reading the XML fileFile inputFile =newFile("studentData.xml");//Parsing the XMLDocument document = reader.read(inputFile);//Extracting the rootElementRootElement= document.getRootElement();//Modifying the elementsElement student =RootElement.addElement("student").addAttribute("rollno","693");
    
    
         student.addElement("firstname").addText("John");
         student.addElement("lastname").addText("Daniel");
         student.addElement("nickname").addText("Johny");
         student.addElement("marks").addText("78");//Creating a FileOutputStreamFileOutputStream newFile =newFileOutputStream("studentData.xml");//Writing the updated XML document into fileXMLWriter writer =newXMLWriter(newFile);
         writer.write( document );//Printing the XML documentOutputFormat format =OutputFormat.createPrettyPrint();XMLWriter consoleWriter =newXMLWriter(System.out, format );
         consoleWriter.write( document );}catch(Exception e){
    	 e.printStackTrace();}}}</code></pre>

    Output

    The file content after adding new student information is as follows −

    <?xml version="1.0" encoding="UTF-8"?><class><student rollno="393"><firstname>dinkar</firstname><lastname>kad</lastname><nickname>dinkar</nickname><marks>85</marks></student><student rollno="493"><firstname>Vaneet</firstname><lastname>Gupta</lastname><nickname>vinni</nickname><marks>64</marks></student><student rollno="593"><firstname>jasvir</firstname><lastname>singn</lastname><nickname>jazz</nickname><marks>90</marks></student><student rollno="693"><firstname>John</firstname><lastname>Daniel</lastname><nickname>Johny</nickname><marks>78</marks></student></class>
  • Create XML Document

    Java DOM4J library has classes, interfaces and methods to create XML documents. The interfaces Document and Element of org.dom4j package has methods to create XML documents. The XMLWriter class of org.dom4j.io package writes the XML content of the document into the file. In this chapter we are going to use these interfaces and classes to create XML files.

    Create XML Using Java DOM4J Parser

    Following are the steps to create XML documents using Java DOM4J Parser −

    • Step 1: Creating a new Document
    • Step 2: Creating and Adding XML elements
    • Step 3: Creating a FileOutputStream
    • Step 4: Writing the XML document into file
    • Step 5: Printing the XML document

    Step 1: Creating a new Document

    The DocumentHelper class of org.dom4j package provides the helper methods to use DOM4J. The createDocument() method creates a new document as follows −

    Document document =DocumentHelper.createDocument();

    Step 2: Creating and Adding XML elements

    We can create root element and add it to the XML document using addElement() method. Also, we can add child elements to the root element using the same addElement() method. To add attributes, we use addAttribute() method.

    Element root = document.addElement("root_ele_name");Element child = root.addElement("child_ele_name").addAttribute("attr_name","attr_val");

    Step 3: Creating a FileOutputStream

    To copy the content of XML document into a file, we need to create a FileOutputStream as follows −

    FileOutputStream newFile =newFileOutputStream("D://newDOM4jFile.xml");

    Step 4: Writing the XML document into file

    The XMLWriter class has write() method to write the XML document into the specified file. We can also send the format we want to add to the content as second argument to the write() method.

    XMLWriter writer =newXMLWriter(newFile);
    writer.write( document );

    Step 5: Printing the XML document

    To print the content of XML document, we can pass System.out as the first argument and format as the second argument. The createPrettyPrint() method creates proper indentation of XML elements while displaying. This step is optional and can be used for testing purpose.

    OutputFormat format =OutputFormat.createPrettyPrint();XMLWriter consoleWriter =newXMLWriter(System.out, format );
    consoleWriter.write( document );

    Creating XML file

    The addElement() method can be used with either Document or Element interface. This method takes name of the element as a parameter and adds it to the specified document or element. To add root element, we use Document.addElement(root_ele_name).

    The addAttribute() method takes name of the attribute as first argument and value as the second argument. It adds the attribute to the specified element.

    The addText() method adds the text content to the element. It takes the text content as a String and adds to the corresponding element.

    Example

    Using the methods discussed above, we are going to create an XML file with ‘cars’ as root element and company as its attribute. Let us add two ‘carname’ elements with ‘type’ as attribute.

    We need to create the following cars.xml file −

    <?xml version = "1.0" encoding = "UTF-8"?><cars><supercars company = "Ferrari"><carname type = "formula one">Ferrari 101</carname><carname type = "sports">Ferrari 202</carname></supercars></cars>

    The following CreateXMLDemo.java program creates a document and adds the elements using addElement() method and attributes using addAttribute() method. To set the text content, addText() method is used. The XML content of the document is transferred into a file and also printed on the console.

    packagejava_xml_dom4j;importjava.io.FileOutputStream;importorg.dom4j.Document;importorg.dom4j.DocumentHelper;importorg.dom4j.Element;importorg.dom4j.io.OutputFormat;importorg.dom4j.io.XMLWriter;publicclassCreateDemo{publicstaticvoidmain(String[] args){try{//Creating a new DocumentDocument document =DocumentHelper.createDocument();//Creating and Adding XML elementsElement root = document.addElement("cars");Element supercarElement = root.addElement("supercars").addAttribute("company","Ferrai");
    
    
         supercarElement.addElement("carname").addAttribute("type","Ferrari 101").addText("Ferrari 101");
         supercarElement.addElement("carname").addAttribute("type","sports").addText("Ferrari 202");//Creating a FileOutputStreamFileOutputStream newFile =newFileOutputStream("D://newDOM4jFile.xml");//Writing the XML document into fileXMLWriter writer =newXMLWriter(newFile);
         writer.write( document );//Printing the XML documentOutputFormat format =OutputFormat.createPrettyPrint();XMLWriter consoleWriter =newXMLWriter(System.out, format );
         consoleWriter.write( document );}catch(Exception e){
         e.printStackTrace();}}}</code></pre>

    Output

    The output window displays the XML content as follows −

    <?xml version = "1.0" encoding = "UTF-8"?>
    <cars>
       <supercars company = "Ferrari">
    
      &lt;carname type = "formula one"&gt;Ferrari 101&lt;/carname&gt;
      &lt;carname type = "sports"&gt;Ferrari 202&lt;/carname&gt;
    </supercars> </cars>
  • Query XML Document

    Java DOM4J parser is an open source Java library to parse and query the necessary information from XML documents. We can query XML documents using the methods of Document and Element interfaces of DOM4J. The elements() method retrieves all the elements of root element. Using attributeValue() method, we can query the attributes of elements.

    Query XML Using Java DOM4J Parser

    Following are the steps used while querying a document using Java DOM4J Parser −

    • Step 1: Creating SAXReader object
    • Step 2: Reading the XML file
    • Step 3: Parsing the XML
    • Step 4: Extracting the root
    • Step 5: Querying the Document

    Step 5: Querying the Document

    After extracting the root in step 4, we can query the child elements using the root element. Now we are going to query XML documents by querying their elements and by querying their attributes.

    Querying Elements

    To get elements with specific name, we can use elements() method of Element interface and pass the name of the element we need to obtain as a parameter in the form of a string. This method returns the list of elements with the same name.

    The getData() method of Element interface returns the text data in the form of a String. To query an element based on its text content, we can use this method with equals() method for comparison.

    Example

    Following is the cars.xml file we need to query −

    <?xml version = "1.0"?><cars><carname company="Ferarri" >Ferarri 101</carname><carname company="Lamborgini">Lamborgini 001</carname><carname company="Lamborgini">Lamborgini 002</carname><carname company="Lamborgini">Lamborgini 003</carname><carname company="Bentley">Bentley 1</carname><carname company="Bentley">Bentley 2</carname><carname company="Bentley">Bentley 3</carname></cars>

    Using the QueryElements.java program, we are querying the cars.xml file to find if there is “Bentley 2” car available. We get all the ‘carname’ elements from elements() method and comparing the text content using getData() method.

    importjava.io.File;importjava.util.List;importorg.dom4j.Document;importorg.dom4j.Element;importorg.dom4j.io.SAXReader;publicclassQueryElements{publicstaticvoidmain(String[] args){try{//Creating SAXReaderSAXReader reader =newSAXReader();//Reading the XML fileFile inputFile =newFile("cars.xml");//Parsing the XMLDocument document = reader.read(inputFile);//Extracting the rootElementRootElement= document.getRootElement();List<Element> elements =RootElement.elements("carname");boolean found =false;//Iterating over the Listfor(Element ele : elements){if(ele.getData().equals("Bentley 2")){
    
        	    	found =true;}}if(found){System.out.println("Bentley 2 car is found");}else{System.out.println("Bentley 2 car is not found");}}catch(Exception e){
         e.printStackTrace();}}}</code></pre>

    Output

    The output window displays that 'Bentley 2' car is found in the document.

    Bentley 2 car is found

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Querying Attributes

    We can query for a specific attribute of an Element interface by using the attributeValue() method. We can pass a string value which is the name of the attribute as an argument to this method and it returns the value as a string.

    Example 1

    The following QueryAttributes.java program gets the list of 'carname' elements and checks if the value of its attribute 'company' equals to 'Bentley' and the integer variable 'count' is incremented.

    importjava.io.File;importjava.util.List;importorg.dom4j.Document;importorg.dom4j.DocumentException;importorg.dom4j.Element;importorg.dom4j.io.SAXReader;publicclassQueryAttributes{publicstaticvoidmain(String[] args){try{//Creating SAXReaderSAXReader reader =newSAXReader();//Reading the XML fileFile inputFile =newFile("cars.xml");//Parsing the XMLDocument document = reader.read(inputFile);//Extracting the rootElementRootElement= document.getRootElement();List<Element> elements =RootElement.elements("carname");int count =0;for(Element ele : elements){if(ele.attributeValue("company").equals("Bentley")) 
    
        	    	count++;}System.out.println("No of Bentley cars found: "+ count);}catch(DocumentException e){
         e.printStackTrace();}}}</code></pre>

    Output

    The output window displays the number of Bentley cars found in the document.

    No of Bentley cars found: 3
    

    Example 2

    Consider the following studentData.xml file with three student elements inside the root element 'class'. Let us now try to print the information about the student with roll number 493.

    <?xml version = "1.0"?><class><student rollno = "393"><firstname>dinkar</firstname><lastname>kad</lastname><nickname>dinkar</nickname><marks>85</marks></student><student rollno = "493"><firstname>Vaneet</firstname><lastname>Gupta</lastname><nickname>vinni</nickname><marks>95</marks></student><student rollno = "593"><firstname>jasvir</firstname><lastname>singn</lastname><nickname>jazz</nickname><marks>90</marks></student></class>

    The following QueryStudent.java program gets all the elements of the root element 'class'. It the compares the value of roll number attribute with '493'. If it matches, it prints all the student details using elementText() method.

    importjava.io.File;importjava.util.List;importorg.dom4j.Document;importorg.dom4j.DocumentException;importorg.dom4j.Element;importorg.dom4j.io.SAXReader;publicclassQueryStudent{publicstaticvoidmain(String[] args){try{//Creating SAXReaderSAXReader reader =newSAXReader();//Reading the XML fileFile inputFile =newFile("studentData.txt");//Parsing the XMLDocument document = reader.read(inputFile);//Extracting the rootElementRootElement= document.getRootElement();System.out.println("Root element :"+RootElement.getName());List<Element> elements =RootElement.elements();System.out.println("----------------------------");for(Element ele : elements){if(ele.attributeValue("rollno").equals("493")){System.out.println("\nCurrent Element :"+ ele.getName());System.out.println("Student roll no : "+ ele.attributeValue("rollno"));System.out.println("First Name : "+ ele.elementText("firstname"));System.out.println("Last Name : "+ ele.elementText("lastname"));System.out.println("First Name : "+ ele.elementText("nickname"));System.out.println("Marks : "+ ele.elementText("marks"));}}}catch(DocumentException e){
    
         e.printStackTrace();}}}</code></pre>

    Output

    All the details of the student with roll number 493 are displayed.

    Root element :class
    ----------------------------
    
    Current Element :student
    Student roll no : 493
    First Name : Vaneet
    Last Name : Gupta
    First Name : vinni
    Marks : 95
    
  • Parse XML Document

    Java DOM4J parser is an API in Java to parse XML documents. It creates DOM4J document from a built-in SAX parser or a DOM parser. After getting the document, we can retrieve the information of elements and attributes using the built-in methods of DOM4J Document and Element interfaces.

    In this chapter, we have used getRootElement() to extract root element of the document and the elements() method to get all of its child elements.

    Parse XML Using Java DOM4J Parser

    Following are the steps used while parsing a document using Java DOM4J Parser −

    • Step 1: Creating SAXReader object
    • Step 2: Reading the XML file
    • Step 3: Parsing the XML
    • Step 4: Extracting the root
    • Step 5: Retrieving Elements

    Step 1: Creating SAXReader object

    The SAXReader class is used to create a DOM4J document from an XML file or stream. It has its own built-in SAX parser to parse the file. We create a SAXReader object as follows −

    SAXReader reader =newSAXReader();

    Step 2: Reading the XML file

    To read XML content as a string, we can use StringBuilder class and later convert it into a ByteStream to create XML document. If XML content is available in the form of a file, we can read it using File class of java.io as follows −

    File inputFile =newFile("src/input.txt");

    Step 3: Parsing the XML

    To parse the XML file, we have created SAXReader object in step 1. Using the read() method of SAXReader, we create DOM4J document by passing the file we read in step 2 as an argument as follows −

    Document document = reader.read(inputFile);

    Step 4: Extracting the Root

    The root element needs to be extracted from DOM4J document to obtain any information of elements. Using the getRootElement() method of Document interface we obtain the root element as follows −

    ElementRootElement= document.getRootElement();

    Step 5: Retrieving Elements

    After we have followed the first four steps, we now have the root element to obtain the information of its child elements. Now, we are going to perform some tasks such as retrieving the root, retrieving attributes and retrieving element text of an XML document with examples.

    Retrieving the Root

    The getRootElement() method of Document interface returns the root element of the document in the form of an Element object. To get the name of the element, we use getName() method of Element interface. It returns the name of the element in the form of a String.

    Example

    The studentData.xml file we need to parse is as follows −

    <?xml version = "1.0"?><class><student rollno = "393"><firstname>dinkar</firstname><lastname>kad</lastname><nickname>dinkar</nickname><marks>85</marks></student><student rollno = "493"><firstname>Vaneet</firstname><lastname>Gupta</lastname><nickname>vinni</nickname><marks>95</marks></student><student rollno = "593"><firstname>jasvir</firstname><lastname>singn</lastname><nickname>jazz</nickname><marks>90</marks></student></class>

    The RetrieveRoot.java program reads the above studentData.xml file using a SAXReader and obtains a DOM4J document. After getting the document, we use getRootElement() method to extract the root.

    importjava.io.File;importorg.dom4j.Document;importorg.dom4j.Element;importorg.dom4j.io.SAXReader;publicclassRetrieveRoot{publicstaticvoidmain(String[] args){try{//Creating SAXReaderSAXReader reader =newSAXReader();//Reading the XML fileFile inputFile =newFile("studentData.xml");//Parsing the XMLDocument document = reader.read(inputFile);//Extracting the rootElementRootElement= document.getRootElement();//Printing the Root Element NameSystem.out.println("Root element Name :"+RootElement.getName());}catch(Exception e){
    
    	 e.printStackTrace();}}}</code></pre>

    Output

    The root element name, 'class' is displayed on the output screen.

    Root element Name :class
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Retrieving Attributes

    The attributeValue() method of Element interface retrieves the value of the specified attribute in the form of a String. If there is no such attribute for that element, it returns null. If there is no value specified for the attribute, it returns an empty string.

    Example

    The following RetrieveAttributes.java program uses the attributeValue() method and retrieves all the roll numbers of student elements.

    importjava.io.File;importjava.util.List;importorg.dom4j.Document;importorg.dom4j.Element;importorg.dom4j.io.SAXReader;publicclassRetrieveAttributes{publicstaticvoidmain(String[] args){try{//Creating SAXReaderSAXReader reader =newSAXReader();//Reading the XML fileFile inputFile =newFile("studentData.xml");//Parsing the XMLDocument document = reader.read(inputFile);//Extracting the rootElementRootElement= document.getRootElement();//Iterating over the ListList<Element> elements =RootElement.elements();System.out.println("Student Roll numbers - ");for(Element ele : elements){System.out.println(ele.attributeValue("rollno"));}}catch(Exception e){
    
    	 e.printStackTrace();}}}</code></pre>

    All the student roll numbers are displayed on the output screen.

    Output

    Student Roll numbers - 
    393
    493
    593
    

    Retrieving Element Text

    The elements() method of Element interface returns the list of Elements contained in it. The elementText() method of Element interface returns the text content of the element in the form of a string.

    importjava.io.File;importjava.util.List;importorg.dom4j.Document;importorg.dom4j.DocumentException;importorg.dom4j.Element;importorg.dom4j.io.SAXReader;publicclassDemoParse{publicstaticvoidmain(String[] args){try{//Creating SAXReaderSAXReader reader =newSAXReader();//Reading the XML fileFile inputFile =newFile("studentData.xml");//Parsing the XMLDocument document = reader.read(inputFile);//Extracting the rootElementRootElement= document.getRootElement();System.out.println("Root Element: "+RootElement.getName());List<Element> elements =RootElement.elements();System.out.println("---------------------------------");//Iterating over the Listfor(Element ele : elements){System.out.println("\nCurrent Element :"+ ele.getName());System.out.println("Student roll no : "+ ele.attributeValue("rollno"));System.out.println("First Name : "+ ele.elementText("firstname"));System.out.println("Last Name : "+ ele.elementText("lastname"));System.out.println("First Name : "+ ele.elementText("nickname"));System.out.println("Marks : "+ ele.elementText("marks"));}}catch(DocumentException e){
    
         e.printStackTrace();}}}</code></pre>

    Output

    All the information of students is displayed on the output screen.

    Root Element: class
    ---------------------------------
    
    Current Element :student
    Student roll no : 393
    First Name : dinkar
    Last Name : kad
    First Name : dinkar
    Marks : 85
    
    Current Element :student
    Student roll no : 493
    First Name : Vaneet
    Last Name : Gupta
    First Name : vinni
    Marks : 95
    
    Current Element :student
    Student roll no : 593
    First Name : jasvir
    Last Name : singn
    First Name : jazz
    Marks : 90
    
  • Overview

    DOM4J is an open source, Java-based library to parse XML documents. It is a highly flexible and memory-efficient API. It is Java-optimized and uses Java collections like List and Arrays.

    DOM4J works with DOM, SAX, XPath, and XSLT. It can parse large XML documents with very low memory footprint.

    Environment Setup

    In order to use DOM4J parser, you should have dom4j-2.1.4.jar in your application’s classpath. You can download the jar file from here.

    When to Use DOM4J?

    You should use a DOM4J parser when −

    • You need to know a lot about the structure of an XML document.
    • You need to move parts of an XML document around (you might want to sort certain elements, for example).
    • You need to use the information in an XML document more than once.
    • You are a Java developer and want to leverage Java-optimized parsing of XML.

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    What is the Result of Parsing?

    When you parse an XML document with a DOM4J parser, you get the flexibility to get back a tree structure that contains all of the elements of your document without impacting the memory footprint of the application.

    DOM4J provides a variety of utility functions that you can use to examine the contents and structure of an XML document. XPath expressions can also be used to navigate through an XML document.

    Advantages

    Following are some advantages of DOM4J −

    • Flexible and easily maintainable
    • Open source, lightweight and quick API
    • Random access of elements is possible

    DOM4J Interfaces

    The package ‘org.dom4j’ defines several Java interfaces. Here are the most commonly used interfaces −

    InterfaceDescription
    DocumentRepresents the entire XML document. A Document object is often referred to as a JDOM tree.
    ElementRepresents an XML element. Element object has methods to manipulate its child elements, text, attributes, and namespaces.
    AttributeRepresents an attribute of an element. Attribute has methods to get and set the values of attributes.
    NodeRepresents Element, Attribute, or ProcessingInstruction.

    DOM4J methods

    When you are working with DOM4J, there are several methods you’ll often use. Some of them are as follows −

    MethodDescription
    SAXReader.read(xmlSource)Builds a DOM4J document from an XML source.
    Document.getRootElement()Returns the root element of an XML document.
    Element.node(index)Returns the XML node at a particular index in an element.
    Element.attributes()Returns all the attributes of an element.
    Node.valueOf(@Name)Returns the values of an attribute with the given name of an element.
  • Modify XML Document

    XPath parser is used to navigate XML Documents only. It is better to use DOM parser for modifying XML. Please refer the Java DOM Parser section for the same.

  • Create XML Document

    XPath parser is used to navigate XML Documents only. It is better to use DOM parser for creating XML. Please refer the Java DOM Parser section for the same.

  • Query XML Document

    Java XPath is a Java API that helps us query XML documents using XPath expressions. Using XPath expressions, random access of elements is possible and hence we can select specific nodes from an XML document. This makes querying the documents more flexible.

    In this chapter, we have used XPath expressions with predicates such as ‘/cars/carname/@company’, ‘/class/student[@rollno = ‘493’]’ to query an XML document with various examples in detail.

    Query XML Using Java XPath Parser

    Following are the steps used while querying a document using Java XPath Parser −

    • Step 1: Creating a DocumentBuilder
    • Step 2: Reading the XML
    • Step 3: Creating Document from file or Stream
    • Step 4: Building XPath
    • Step 5: Preparing and Evaluating XPath expression
    • Step 6: Iterating over NodeList
    • Step 7: Querying Elements

    Step 7: Querying Elements

    Querying the XML document using XPath follows the same steps as parsing the XML document, but the only difference is seen in the way we prepare our XPath expressions. While querying, we query for a particular attribute or element.

    Querying by Element Names

    We can query the XML document based on their element names by specifying the element name inside the XPath expression. The XPath expression, ‘root_element/element_name’ retrieves all the nodes with the specified name inside the root element.

    Example

    The following cars.xml file has information about seven cars with the element name as ‘carname’ inside the root element ‘cars’. We are going to query this XML file to check if there is ‘Bentley 2’car.

    <?xml version = "1.0"?><cars><carname company="Ferarri" >Ferarri 101</carname><carname company="Lamborgini">Lamborgini 001</carname><carname company="Lamborgini">Lamborgini 002</carname><carname company="Lamborgini">Lamborgini 003</carname><carname company="Bentley">Bentley 1</carname><carname company="Bentley">Bentley 2</carname><carname company="Bentley">Bentley 3</carname></cars>

    The following QueryByElements.java program reads the above cars.xml file and builds a document. Using the XPath expression ‘/cars/carname’, we get the carname elements as nodes inside a nodeList. We iterate through the NodeList to find out Bentley 2 car.

    importjava.io.File;importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.xpath.XPath;importjavax.xml.xpath.XPathConstants;importjavax.xml.xpath.XPathFactory;importorg.w3c.dom.Document;importorg.w3c.dom.NodeList;importorg.w3c.dom.Node;publicclassQueryByElements{publicstaticvoidmain(String[] args){try{//Creating DocumentBuilderDocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance();DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();//Reading the XMLFile inputFile =newFile("cars.xml");//Creating Document from file or StreamDocument doc = dBuilder.parse(inputFile);//Building XPathXPath xPath =XPathFactory.newInstance().newXPath();//Preparing and Evaluating XPath expressionString expression ="/cars/carname";NodeList nodeList =(NodeList) xPath.compile(expression).evaluate(
    
            doc,XPathConstants.NODESET);//Iterating over NodeListboolean found =false;for(int i =0; i &lt; nodeList.getLength(); i++){Node nNode = nodeList.item(i);//Querying the Elementsif(nNode.getTextContent().equals("Bentley 2"))
               found=true;}if(found)System.out.println("Bentley 2 car is found");elseSystem.out.println("Bentley 2 car is not found");}catch(Exception e){
         e.printStackTrace();}}}</code></pre>

    Output

    The output window displays that the Bentley 2 car is found.

    Bentley 2 car is found
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Querying by Attributes

    To query attributes inside elements, we use the XPath expression '/root_element/element_name/@attr_name'. This expression fetches all the attributes with specified name of the specified element inside the root element. All these attributes are in the form of nodes inside the NodeList.

    Example 1

    We use the same cars.xml file we have used in the previous example to count the total number of Bentley cars. We have used the expression, '/cars/carname/@company' to get all the company attribute nodes and by checking it with 'Bentley', we are incrementing the count variable.

    importjava.io.File;importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.xpath.XPath;importjavax.xml.xpath.XPathConstants;importjavax.xml.xpath.XPathFactory;importorg.w3c.dom.Document;importorg.w3c.dom.NodeList;importorg.w3c.dom.Node;publicclassQueryByAttributes{publicstaticvoidmain(String[] args){try{//Creating a DocumentBuilderDocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance();DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();//Reading the XMLFile inputFile =newFile("cars.xml");//Creating Document from file or StreamDocument doc = dBuilder.parse(inputFile);//Building XPathXPath xPath =XPathFactory.newInstance().newXPath();//Preparing and Evaluating XPath expressionString expression ="/cars/carname/@company";NodeList nodeList =(NodeList) xPath.compile(expression).evaluate(
    
            doc,XPathConstants.NODESET);//Iterating over NodeListint count=0;for(int i =0; i &lt; nodeList.getLength(); i++){Node nNode = nodeList.item(i);//Querying the Elementsif(nNode.getNodeValue().equals("Bentley"))
               count++;}System.out.println("Number of Bentley cars found: "+ count);}catch(Exception e){
         e.printStackTrace();}}}</code></pre>

    Output

    The output window displays number of Bentley cars found in the XML document.

    Number of Bentley cars found: 3
    

    Example 2

    We need to query the following studentData.xml to get only the information related to the student with roll number 493.

    <?xml version = "1.0"?><class><student rollno = "393"><firstname>dinkar</firstname><lastname>kad</lastname><nickname>dinkar</nickname><marks>85</marks></student><student rollno = "493"><firstname>Vaneet</firstname><lastname>Gupta</lastname><nickname>vinni</nickname><marks>95</marks></student><student rollno = "593"><firstname>jasvir</firstname><lastname>singn</lastname><nickname>jazz</nickname><marks>90</marks></student></class>

    In the following QueryXPathDemo.java program, we have parsed the above studentData.xml file and built a document. The XPath expression, '/class/student[@rollno = '493']' is used to get that student element with roll number 493.

    importjava.io.File;importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.xpath.XPath;importjavax.xml.xpath.XPathConstants;importjavax.xml.xpath.XPathFactory;importorg.w3c.dom.Document;importorg.w3c.dom.NodeList;importorg.w3c.dom.Node;importorg.w3c.dom.Element;publicclassQueryXPathDemo{publicstaticvoidmain(String[] args){try{//Creating a DocumentBuilderDocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance();DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();//Reading the XMLFile inputFile =newFile("studentData.xml");//Creating Document from file or StreamDocument doc = dBuilder.parse(inputFile);//Building XPathXPath xPath =XPathFactory.newInstance().newXPath();//Preparing and Evaluating XPath expressionString expression ="/class/student[@rollno = '493']";NodeList nodeList =(NodeList) xPath.compile(expression).evaluate(
    
            doc,XPathConstants.NODESET);//Iterating over NodeListfor(int i =0; i &lt; nodeList.getLength(); i++){Node nNode = nodeList.item(i);System.out.println("\nCurrent Element :"+ nNode.getNodeName());//Getting student info with roll number 493if(nNode.getNodeType()==Node.ELEMENT_NODE){Element eElement =(Element) nNode;System.out.println("Student roll no : "+ eElement.getAttribute("rollno"));System.out.println("First Name : "+ eElement
                  .getElementsByTagName("firstname").item(0).getTextContent());System.out.println("Last Name : "+ eElement
                  .getElementsByTagName("lastname").item(0).getTextContent());System.out.println("Nick Name : "+ eElement
                  .getElementsByTagName("nickname").item(0).getTextContent());System.out.println("Marks : "+ eElement
                  .getElementsByTagName("marks").item(0).getTextContent());}}}catch(Exception e){
         e.printStackTrace();}}}</code></pre>

    The output window displays the information of the student with roll number 493.

    Output

    Current Element :student
    Student roll no : 493
    First Name : Vaneet
    Last Name : Gupta
    Nick Name : vinni
    Marks : 95
    
  • Parse XML Document

    Java XPath parser is an API in Java to parse XML documents using XPath expressions and functions. It helps us to traverse through the entire XML document and obtain elements as nodes inside a NodeList. The package ‘javax.xml.xpath’ provides the API for the evaluation of XPath expressions. In this chapter, we will see how to traverse through all the nodes in an XML document.

    Parse XML Using Java XPath Parser

    Following are the steps used while parsing a document using Java XPath Parser −

    • Step 1: Creating a DocumentBuilder
    • Step 2: Reading the XML
    • Step 3: Creating Document from file or Stream
    • Step 4: Building XPath
    • Step 5: Preparing and Evaluating XPath expression
    • Step 6: Iterating over NodeList
    • Step 7: Retrieving Elements

    Step 1: Create a DocumentBuilder

    The DocumentBuilderFactory is a factory API that is used to create DocumentBuilder objects. The newDocumentBuilder() method of DocumentBuilderFactory returns a DocumentBuilder object as follows −

    DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();

    Step 2: Reading the XML

    The FileReader class is used to read streams of characters from the input file. The following statement throws “FileNotFoundException” if the file can’t be found or if the file can’t be read for some reason.

    FileReader fileReader =newFileReader("src/input.txt");

    Instead of reading XML content from the file, we can also get the content in the form of a string and convert it into bytes as follows −

    StringBuilder xmlBuilder =newStringBuilder();
    xmlBuilder.append("<class>xyz</class>");ByteArrayInputStream input =newByteArrayInputStream(xmlBuilder.toString().getBytes("UTF-8"));

    Step 3: Create a Document from a file or stream

    The DocumentBuilder object created in the first step is used to create a document from the input file or input stream. The parse() method takes file or stream as an argument and returns a Document object as follows −

    Document doc = builder.parse(input);

    Step 4: Building XPath

    To parse XML document using XPath, we need to build a newXPath using newXPath() method of XPathFactory. This method returns a new XPath as follows −

    XPath xPath =XPathFactory.newInstance().newXPath();

    Step 5: Preparing and Evaluating XPath expression

    As we have discussed in the previous chapter, XPath has expressions that help us retrieve information from the XML documents, here we need to create one such expression based on the requirement and evaluate it. The evaluate() method returns the result of the expression as a NodeList as follows −

    String expression ="/class/student";NodeList nodeList =(NodeList) xPath.compile(expression).evaluate(
       doc,XPathConstants.NODESET);

    Step 6: Iterating over NodeList

    The NodeList we get in step 5 is now iterated to examine each node and retrieve information accordingly. Here, we have used a for loop to iterate over the NodeList, you can use any loop of your choice.

    for(int i =0; i < nodeList.getLength(); i++){Node nNode = nodeList.item(i);...}

    Step 7: Retrieving Elements

    After following the above six steps, we obtain the elements in the form of nodes. By using the methods of interfaces available in DOM, we can retrieve the necessary elements and attributes.

    Retrieve Root Element

    To retrieve root element from the XML document, we have the XPath expression ‘/‘. Using this expression and by evaluating this, we get the NodeList with just a single Node.

    The getNodeName() method of Node interface retrieves the name of the node as a String object and the getTextContent() method returns the text content of the node as a String.

    Example

    In the following example, we have taken XML content as a StringBuilder object and parsed it using parse() function. We have only a single element, which is obviously the root and we have text content as ‘xyz class’. Using the methods discussed above we retrieve the information of root element.

    Open Compiler

    importjava.io.ByteArrayInputStream;importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.xpath.XPath;importjavax.xml.xpath.XPathConstants;importjavax.xml.xpath.XPathFactory;importorg.w3c.dom.Document;importorg.w3c.dom.NodeList;importorg.w3c.dom.Node;publicclassRetrieveRoot{publicstaticvoidmain(String[] args){try{//Creating a DocumentBuilderDocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance();DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();//Reading the XMLStringBuilder xmlBuilder =newStringBuilder();
    
     	     xmlBuilder.append("&lt;class&gt;xyz class&lt;/class&gt;");ByteArrayInputStream input =newByteArrayInputStream(xmlBuilder.toString().getBytes("UTF-8"));//Creating Document from file or StreamDocument doc = dBuilder.parse(input);//Building XPathXPath xPath =XPathFactory.newInstance().newXPath();//Preparing and Evaluating XPath expressionString expression ="/";NodeList nodeList =(NodeList) xPath.compile(expression).evaluate(
            doc,XPathConstants.NODE);//Iterating over NodeListfor(int i =0; i &lt; nodeList.getLength(); i++){Node node = nodeList.item(i);//Retrieving Root ElementSystem.out.println("Root Element Name: "+ node.getNodeName());System.out.println("Text Content: "+ node.getTextContent());}}catch(Exception e){
    	  e.printStackTrace();}}}</code></pre>

    Output

    The root element name and text content are displayed on the console.

    Root Element Name: class
    Text Content: xyz class
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Retrieving Attributes

    The NodeList we get after evaluating the XPath expression has nodes with different node types. We can convert those nodes into Elements if the node type is equal to 'ELEMENT_NODE'. The getAttribute("attribute_name") method of Element interface is used to retrieve the value of attribute in the form of a String.

    Example

    The following studentData.xml file has information of three students. We are going to retrieve this information using XPath parser library in Java. The class element is the root element with three student child elements. Let us see how to use XPath library in java to retrieve the infromation of three students.

    <?xml version = "1.0"?><class><student rollno = "393"><firstname>dinkar</firstname><lastname>kad</lastname><nickname>dinkar</nickname><marks>85</marks></student><student rollno = "493"><firstname>Vaneet</firstname><lastname>Gupta</lastname><nickname>vinni</nickname><marks>95</marks></student><student rollno = "593"><firstname>jasvir</firstname><lastname>singh</lastname><nickname>jazz</nickname><marks>90</marks></student></class>

    In the following RetrieveAttributes.java program, we have parsed the student.xml file and built a document. The expression '/class/student' is used to get all the 'student' nodes inside the 'class' node into a NodeList. The NodeList is then iterated and got the information of each student.

    importjava.io.File;importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.xpath.XPath;importjavax.xml.xpath.XPathConstants;importjavax.xml.xpath.XPathFactory;importorg.w3c.dom.Document;importorg.w3c.dom.NodeList;importorg.w3c.dom.Node;importorg.w3c.dom.Element;publicclassRetrieveAttributes{publicstaticvoidmain(String[] args){try{//Creating a DocumentBuilderDocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance();DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();//Reading the XMLFile inputFile =newFile("studentData.xml");//Creating Document from file or StreamDocument doc = dBuilder.parse(inputFile);//Building XPathXPath xPath =XPathFactory.newInstance().newXPath();//Preparing and Evaluating XPath expressionString expression ="/class/student";NodeList nodeList =(NodeList) xPath.compile(expression).evaluate(
    
            doc,XPathConstants.NODESET);//Iterating over NodeListfor(int i =0; i &lt; nodeList.getLength(); i++){Node nNode = nodeList.item(i);System.out.println("\nCurrent Element :"+ nNode.getNodeName());//Retrieving Elementsif(nNode.getNodeType()==Node.ELEMENT_NODE){Element eElement =(Element) nNode;System.out.println("Student roll no :"+ eElement.getAttribute("rollno"));System.out.println("First Name : "+ eElement
                  .getElementsByTagName("firstname").item(0).getTextContent());System.out.println("Last Name : "+ eElement
                  .getElementsByTagName("lastname").item(0).getTextContent());System.out.println("Nick Name : "+ eElement
                  .getElementsByTagName("nickname").item(0).getTextContent());System.out.println("Marks : "+ eElement
                  .getElementsByTagName("marks").item(0).getTextContent());}}}catch(Exception e){
         e.printStackTrace();}}}</code></pre>

    Output

    All the information of three students is displayed on the console.

    Current Element :student
    Student roll no : 393
    First Name : dinkar
    Last Name : kad
    Nick Name : dinkar
    Marks : 85
    
    Current Element :student
    Student roll no : 493
    First Name : Vaneet
    Last Name : Gupta
    Nick Name : vinni
    Marks : 95
    
    Current Element :student
    Student roll no : 593
    First Name : jasvir
    Last Name : singh
    Nick Name : jazz
    Marks : 90
    
  • Overview

    XPath is an XML Path language to find information in an XML file. It is an official recommendation of the World Wide Web Consortium (W3C). It is used to traverse elements and attributes of an XML document. XPath provides various types of expressions which can be used to enquire relevant information from the XML document and it is mainly used in XSLT standard.

    XPath Terminology

    • Structure Definitions − XPath defines the parts of an XML document like element, attribute, text, namespace, processing-instruction, comment, and document nodes.
    • Path Expressions − XPath provides powerful path expressions such as select nodes or list of nodes in XML documents.
    • Standard Functions − XPath provides a rich library of standard functions for manipulation of string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values, etc.
    • Axes − XPath has thirteen different axes that retrieves the relative elements of the current element such as ancestor, child, descendant, preceding, following etc.

    XPath Expressions

    XPath uses a path expression to select node or list of nodes from an XML document. Following is a list of useful path expressions to select any node/list of nodes from an XML document.

    ExpressionDescription
    node-nameSelects all nodes with the given “nodename”
    /Selection starts from the root node
    //Selection starts from the current node that match the selection
    .Selects the current node
    ..Selects the parent of the current node
    @Selects attributes
    studentSelects all nodes with the name “student”
    class/studentSelects all student elements that are children of class
    //studentSelects all student elements no matter where they are in the document

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Expressions with Predicates

    XPath expressions can be used along with predicates to obtain a specific node or a node containing specific value and are defined using […].

    ExpressionResult
    /class/student[1]Selects the first student element that is the child of the class element.
    /class/student[last()]Selects the last student element that is the child of the class element.
    /class/student[last()-1]Selects the last but one student element that is the child of the class element.
    //student[@rollno = ‘493’]Selects all the student elements that have an attribute named rollno with a value of ‘493’