Category: 17. Java XML

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

  • Modify XML Document

    Java SAX parser is a Java API that can be used to parse and modify XML documents. To modify any XML document using SAX parser, We have to store the data in any data structure because we cannot go back as it has only forward read-only access. In this chapter, we are going to see how to read and modify existing XML document in detail.

    Modify XML Using Java SAX Parser

    • Step 1: Implementing a Handler class
    • Step 2: Creating a SAXParser Object
    • Step 3: Reading the XML
    • Step 4: Creating object for Handler class
    • Step 5: Parsing the XML Document
    • Step 6: Writing the updated content into file

    Step 6: Writing the Updated Content into File

    The updated content can be written into the file using write() method of FileWriter object as follows −

    FileWriter filewriter =newFileWriter("newfile.xml");
    filewriter.write("content_here");

    Modifying XML File

    SAX parser will not provide random access to elements as DOM provides. We can implement the call back methods in Handler class that implements DefaultHandler to create and add new elements to the already exisiting XML document.

    Example

    In this example, we are going to see how to modify studentData.xml file by adding result element to the student element. Here is the sudentData.xml file that we need to modify by appending <Result>Pass<Result/> at the end of </marks> tag.

    <?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 ModifyXML.java program, we have created SAXParser object and parsed the input file. We have implemented the Handler class with the necessary call back methods and writeFile() method to write the updated content into the file. We have used an array of String to store and write the content to the file.

    importjava.io.*;importorg.xml.sax.*;importjavax.xml.parsers.*;importorg.xml.sax.helpers.DefaultHandler;//Implementing a Handler classclassUserDemoHandlerextendsDefaultHandler{staticString displayText[]=newString[1000];staticint numberLines =0;staticString indentation ="";publicvoidstartDocument(){
    
      displayText&#91;numberLines]= indentation;
    displayText[numberLines]+="<?xml version = \"1.0\" encoding = \""+"UTF-8"+"\"?>"; numberLines++;}publicvoidprocessingInstruction(String target,String data){
      displayText&#91;numberLines]= indentation;
    displayText[numberLines]+="<?"; displayText[numberLines]+= target;if(data !=null&& data.length()>0){
         displayText&#91;numberLines]+=' ';
         displayText&#91;numberLines]+= data;}
       displayText&#91;numberLines]+="?&gt;";
       numberLines++;}publicvoidstartElement(String uri,String localName,String qualifiedName,Attributes attributes){
      displayText&#91;numberLines]= indentation;
    indentation +=" ";
      displayText&#91;numberLines]+='&lt;';
    displayText[numberLines]+= qualifiedName;if(attributes !=null){int numberAttributes = attributes.getLength();for(int loopIndex =0; loopIndex < numberAttributes; loopIndex++){
            displayText&#91;numberLines]+=' ';
            displayText&#91;numberLines]+= attributes.getQName(loopIndex);
            displayText&#91;numberLines]+="=\"";
            displayText&#91;numberLines]+= attributes.getValue(loopIndex);
            displayText&#91;numberLines]+='"';}}
          displayText&#91;numberLines]+='&gt;';
          numberLines++;}publicvoidcharacters(char characters&#91;],int start,int length){String characterData =(newString(characters, start, length)).trim();if(characterData.indexOf("\n")&lt;0&amp;&amp; characterData.length()&gt;0){
         displayText&#91;numberLines]= indentation;
         displayText&#91;numberLines]+= characterData;
         numberLines++;}}publicvoidendElement(String uri,String localName,String qualifiedName){
       
      indentation = indentation.substring(0, indentation.length()-4);
    displayText[numberLines]= indentation; displayText[numberLines]+="</"; displayText[numberLines]+= qualifiedName; displayText[numberLines]+='>'; numberLines++;if(qualifiedName.equals("marks")){startElement("","Result","Result",null);characters("Pass".toCharArray(),0,"Pass".length());endElement("","Result","Result");}}publicvoidwriteFile(FileWriter filewriter)throwsIOException{for(int loopIndex =0; loopIndex < numberLines; loopIndex++){
         filewriter.write(displayText&#91;loopIndex].toCharArray());
         filewriter.write('\n');System.out.println(displayText&#91;loopIndex].toString());}
      filewriter.close();}}publicclassModifyXMLextendsDefaultHandler{publicstaticvoidmain(String args&#91;]){try{//Creating SAXParser object SAXParserFactory factory =SAXParserFactory.newInstance();SAXParser saxParser = factory.newSAXParser();//Reading the XML File inputFile =newFile("src/input.txt");//Creating object for Handler classUserDemoHandler userHandler =newUserDemoHandler();//Parsing the XML
         saxParser.parse(inputFile, userHandler);//Writing the updated content into fileFileWriter filewriter =newFileWriter("src/newfile.xml");
         userHandler.writeFile(filewriter);}catch(Exception e){
         e.printStackTrace(System.err);}}}</code></pre>

    The output window displays the updated content of the XML document after adding result element to each student element.

    <?xml version = "1.0" encoding = "UTF-8"?>
    <class>
       <student rollno = "393">
    
      &lt;firstname&gt;
         dinkar
      &lt;/firstname&gt;
      &lt;lastname&gt;
         kad
      &lt;/lastname&gt;
      &lt;nickname&gt;
         dinkar
      &lt;/nickname&gt;
      &lt;marks&gt;
         85
      &lt;/marks&gt;
      &lt;Result&gt;
         Pass
      &lt;/Result&gt;
    </student> <student rollno = "493">
      &lt;firstname&gt;
         Vaneet
      &lt;/firstname&gt;
      &lt;lastname&gt;
         Gupta
      &lt;/lastname&gt;
      &lt;nickname&gt;
         vinni
      &lt;/nickname&gt;
      &lt;marks&gt;
         95
      &lt;/marks&gt;
      &lt;Result&gt;
         Pass
      &lt;/Result&gt;
    </student> <student rollno = "593">
      &lt;firstname&gt;
         jasvir
      &lt;/firstname&gt;
      &lt;lastname&gt;
         singn
      &lt;/lastname&gt;
      &lt;nickname&gt;
         jazz
      &lt;/nickname&gt;
      &lt;marks&gt;
         90
      &lt;/marks&gt;
      &lt;Result&gt;
         Pass
      &lt;/Result&gt;
    </student> </class>
  • Create XML Document

    Java SAX Parser is a read-only parser which is used to read the XML documents in a forward read-only manner. We cannot create XML documents using a SAX parser.

    It is better to use StAX parser for creating XML documents rather than using SAX parser. Please refer the Java StAX Parser section for the same.

  • Query XML Document

    Java SAX Parser is an API in java which can be used to query XML documents. To query large XML documents using event based APIs, we can use Java SAX parser API. The SAXParser API is present inside the javax.xml.parsers package. We can query any XML document by implementing the call back methods inside Handler class by inheriting DefaultHandler class which in turn inherits ContentHandler interface.

    Query XML using Java SAX parser

    We can query any XML document in Java using SAX parser through following steps −

    • Step 1: Implementing a Handler class
    • Step 2: Creating a SAXParser Object
    • Step 3: Reading the XML
    • Step 4: Creating object for Handler class
    • Step 5: Parsing the XML Document
    • Step 6: Querying the Elements

    Step 6: Querying the Elements

    The ContenHnadler and Attributes interfaces provide various methods which help us query elements and their attributes. For querying required information, We implement code inside our Handler class.

    Querying Elements by TextContent

    The characters() method is used to get the content of elements. The endDocument() function is called at the end of the document. We can use this method to perform some necessary actions that we need to do before closing our XML file.

    Example

    We need to query the following cars.xml file to find out whether there is “Bentley 2” car in the document.

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

    In the following java program, we have implemented characters() and endDocument() methods inside UserHandler class. We have written the code to find Bentley cars inside the characters() function and the end result is printed using endDocument() method.

    importjava.io.File;importjavax.xml.parsers.SAXParser;importjavax.xml.parsers.SAXParserFactory;importorg.xml.sax.SAXException;importorg.xml.sax.helpers.DefaultHandler;//Implementing UserHandler ClassclassUserHandlerextendsDefaultHandler{boolean found =false;publicvoidcharacters(char[] ch,int start,int length)throwsSAXException{String textContent =newString(ch,start,length);if(textContent.equals("Bentley 2")){
    
    	  found=true;}}publicvoidendDocument(){if(found)System.out.println("Bentley 2 car is found");elseSystem.out.println("Bentley 2 car is not Found");}}publicclassQueryXMLElements{publicstaticvoidmain(String args&#91;]){try{//Creating a DocumentBuilder Object             	  SAXParserFactory factory =SAXParserFactory.newInstance();SAXParser saxParser = factory.newSAXParser();//Reading the XMLFile xmlFile =newFile("cars.xml");//Creating UserHandler objectUserHandler userHandler =newUserHandler();//Parsing the XML Document
    saxParser.parse(xmlFile, userHandler);}catch(Exception e){
    	  e.printStackTrace();}}}</code></pre>

    The output displays that the Bentley 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 Elements by Attributes

    We can query the elements by their attributes using the Attributes interface. We get the list of attributes of an element as a last argument from the startElement() function. As discussed in the previous chapter, the getValue("attr_name") function is used to obtain the attribute value.

    Example

    Let us use our cars.xml file that we used in the previous example to count number of Bentley cars. This information can be obtained only through the attribute value. As the parser encounters an element, we are using getValue("company") to get the company attribute and incrementing the count if that company is Bentley.

    importjava.io.File;importjavax.xml.parsers.SAXParser;importjavax.xml.parsers.SAXParserFactory;importorg.xml.sax.Attributes;importorg.xml.sax.SAXException;importorg.xml.sax.helpers.DefaultHandler;//Implementing UserHandler ClassclassUserHandlerextendsDefaultHandler{int count=0;publicvoidstartElement(String uri,String localName,String qName,Attributes attributes)throwsSAXException{String company = attributes.getValue("company");if(qName.equals("carname")&& company.equals("Bentley")){
    
    	  count++;}}publicvoidendDocument(){System.out.println("No. of Bentley cars found : "+ count);}}publicclassQueryXMLAttributes{publicstaticvoidmain(String args&#91;]){try{//Creating a DocumentBuilder Object             	  SAXParserFactory factory =SAXParserFactory.newInstance();SAXParser saxParser = factory.newSAXParser();//Reading the XMLFile xmlFile =newFile("cars.xml");//Creating UserHandler objectUserHandler userHandler =newUserHandler();//Parsing the XML Document
    saxParser.parse(xmlFile, userHandler);}catch(Exception e){
    	  e.printStackTrace();}}}</code></pre>

    The output is displayed when the endDocument() method is invoked.

    No.of Bentley cars found : 3
    
  • Parse XML Document

    Java SAX(Simple API for XML) parser is an API in Java to parse XML documents. SAX parser is an event based parser and uses a Handler class to handle the events. The call back methods such as startElement(), characters(), endElement() etc., are implemented inside the Handler class to obtain the details of elements and their attributes. These call back methods are called when the parser identifies the respective events.

    Parse XML Using Java SAX parser

    Following are the steps we need to follow to parse an XML document in Java using SAX parser −

    • Step 1: Implementing a Handler class
    • Step 2: Creating a SAXParser Object
    • Step 3: Reading the XML
    • Step 4: Creating object for Handler class
    • Step 5: Parsing the XML Document
    • Step 6: Retrieving the Elements

    Step 1: Implementing a Handler class

    Application program must implement a handler class to handle the events inside the XML document. After implementing the Handler class, it must be registered with the SAX parser.

    As discussed in the previous chapter, the DefaultHandler class implements ContentHandler interface. It has the methods, startDocument(), endDocument(), startElement(), endElement() and characters() functions that help us parse the XML documents. We write the code inside these methods according to our requirement.

    classUserHandlerextendsDefaultHandler{publicvoidstartDocument(){...}publicvoidstartElement(String uri,String localName,String qName,Attributes attributes){...}publicvoidcharacters(char[] ch,int start,int length){...}publicvoidendElement(String uri,String localName,String qName){...}publicvoidendDocument(){...}}

    Step 2: Creating a SAXParser Object

    The SAXParserFactory class is used to create a new factory instance which in turn is used to create the SAXParser object as follows −

    SAXParserFactory factory =SAXParserFactory.newInstance();SAXParser saxParser = factory.newSAXParser();

    Step 3: Reading the XML

    Read the XML file by specifying the proper file path as follows −

    File xmlFile =newFile("input.xml");

    Instead of reading files, we can create an InputStream of the XML content as follows −

    StringBuilder xmlBuilder =newStringBuilder(); 
    xmlBuilder.append(""<?xml version="1.0"?><rootElement></rootElement>"");ByteArrayInputStream inputStream =newByteArrayInputStream( xmlBuilder.toString().getBytes("UTF-8"));

    Step 4: Creating object for Handler class

    Create an object for the already implemented UserHandler class in first step as follows −

    UserHandler userHandler =newUserHandler();

    Step 5: Parsing the XML Document

    The SAXParser class has the parse() method that takes two arguments, one is the file and the other is the DefaultHandler object. This function parses the given file as XML document using the functions implemented inside the DefaultHandler class.

    saxParser.parse(xmlFile, userHandler);

    The SAXParser class also has the function parse() that takes the content as InputStream −

    saxParser.parse(inputStream, userHandler);

    Step 6: Retrieving the Elements

    After following the above five steps, we can now easily retrieve the required information about the elements. We should write the required code inside the methods of our Handler class in first step. All the methods available inside the ContentHandler interface are discussed in the previous chapter and in this chapter, we will implement these methods to retrieve the basic information about elements such as element name, text content and attributes.

    Retrieving Element Name

    Element name can be obtained from the startElement() method of ContentHandler interface. The third argument of this method is the name of the Element and it is of String type. We can implement this method in our Handler class and get the name of an Element.

    Example

    In the following example, we have taken XML content in the form of a String using StringBuilder class and converted into bytes using ByteArrayInputStream.

    In the UserHandler class, we have implemented the startElement() method and printed the name of the Element. Since, we have only single element in the XML content, that becomes the root element of the document.

    Open Compiler

    importjava.io.ByteArrayInputStream;importjavax.xml.parsers.SAXParser;importjavax.xml.parsers.SAXParserFactory;importorg.xml.sax.Attributes;importorg.xml.sax.SAXException;importorg.xml.sax.helpers.DefaultHandler;//Implementing UserHandler ClassclassUserHandlerextendsDefaultHandler{publicvoidstartElement(String uri,String localName,String qName,Attributes attributes)throwsSAXException{System.out.println("Root element is "+qName);}}publicclassRetrieveElementName{publicstaticvoidmain(String args[]){try{//Creating a SAXParser Object             	  SAXParserFactory factory =SAXParserFactory.newInstance();SAXParser saxParser = factory.newSAXParser();//Reading the XMLStringBuilder xmlBuilder =newStringBuilder();
    
       	     xmlBuilder.append("&lt;college&gt;XYZ College&lt;/college&gt;");ByteArrayInputStream input =newByteArrayInputStream(xmlBuilder.toString().getBytes("UTF-8"));//Creating UserHandler objectUserHandler userhandler =newUserHandler();//Parsing the XML Document
       	     saxParser.parse(input, userhandler);}catch(Exception e){
    	 e.printStackTrace();}}}</code></pre>

    Root Element name, 'college' is printed on the output screen.

    Root element is college
    

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

    Retrieving TextContent

    To retrieve text content of an element, we have characters() method in ContentHandler interface. There is character array, start and length arguments in this method. As soon as the parser sees the content after ">" symbol, this method is called. The start argument carries the index of the first character after ">" symbol and length has the number of characters before it encounters "<" symbol.

    Example

    The following college.xml file has a single sub element, "department" with text content "Computer Science". Let us write a Java program to retrieve this text content along with element names using SAX API.

    <college><department>Computer Science</department></college>

    The UserHandler class inherits DefaultHandler and we have implemented startElement(), endElement() and characters() method. When the parser sees the text content inside department element, this method is called and we are printing it on the console.

    importjava.io.File;importjavax.xml.parsers.SAXParser;importjavax.xml.parsers.SAXParserFactory;importorg.xml.sax.Attributes;importorg.xml.sax.SAXException;importorg.xml.sax.helpers.DefaultHandler;//Implementing UserHandler ClassclassUserHandlerextendsDefaultHandler{publicvoidstartElement(String uri,String localName,String qName,Attributes attributes)throwsSAXException{System.out.println("Start Element : "+ qName);}publicvoidendElement(String uri,String localName,String qName){System.out.println("End Element : "+ qName);}publicvoidcharacters(char[] ch,int start,int length)throwsSAXException{System.out.println("Text Content : "+newString(ch, start, length));}}publicclassRetrieveTextContent{publicstaticvoidmain(String args[]){try{//Creating a SAXParser Object             	  SAXParserFactory factory =SAXParserFactory.newInstance();SAXParser saxParser = factory.newSAXParser();//Reading the XMLFile xmlFile =newFile("college.xml");//Creating UserHandler objectUserHandler userHandler =newUserHandler();//Parsing the XML Document
    
          saxParser.parse(xmlFile, userHandler);}catch(Exception e){
          e.printStackTrace();}}}</code></pre>

    The text content for department element is displayed. As there is no text content inside the "college" element, it is left blank.

    Start Element : college
    Text Content : 
    	
    Start Element : department
    Text Content : Computer Science
    End Element : department
    Text Content : 
    
    End Element : college
    

    Retrieving Attributes

    The method startElement() has Attributes as last argument which has the list of attributes inside the current Element. The getValue("attr_name") function inside the Attributes interface is used to get the value of the specified attribute.

    Example

    We have added few more department elements to our "college.xml" file and also added an attribute "deptcode" to each of the departments. Let us write a java program to retrieve all the elements along with their attributes.

    <?xml version = "1.0"?><college><department deptcode = "DEP_CS23"><name>Computer Science</name><staffCount>20</staffCount></department><department deptcode = "DEP_EC34"><name>Electrical and Electronics</name><staffCount>23</staffCount></department><department deptcode = "DEP_MC89"><name>Mechanical</name><staffCount>15</staffCount></department></college>

    The following Java program implements startElement() and characters() methods in UserHandler class. We have initialised two boolean variables to let us notified about deptcode and staffCount attributes in department element, so that we can use these to print the attributes in characters() method.

    importjava.io.File;importjavax.xml.parsers.SAXParser;importjavax.xml.parsers.SAXParserFactory;importorg.xml.sax.Attributes;importorg.xml.sax.SAXException;importorg.xml.sax.helpers.DefaultHandler;//Implementing UserHandler ClassclassUserHandlerextendsDefaultHandler{boolean hasDeptName=false;boolean hasStaffCount=false;publicvoidstartElement(String uri,String localName,String qName,Attributes attributes)throwsSAXException{if(qName.equals("college")){System.out.println("Root Element : "+qName +"\n");}if(qName.equals("department")){System.out.println("Current Element : "+qName);System.out.println("Department code : "+ attributes.getValue("deptcode"));}if(qName.equals("name")){
    
         hasDeptName=true;}if(qName.equals("staffCount")){
    hasStaffCount=true;}}publicvoidcharacters(char[] ch,int start,int length)throwsSAXException{if(hasDeptName){System.out.println("Department Name : "+newString(ch, start, length)); hasDeptName=false;}if(hasStaffCount){System.out.println("Staff Count : "+newString(ch, start, length)+"\n");
         hasStaffCount=false;}}}publicclassParseAttributesSAX{publicstaticvoidmain(String args&#91;]){try{//Creating a DocumentBuilder Object             	  SAXParserFactory factory =SAXParserFactory.newInstance();SAXParser saxParser = factory.newSAXParser();//Reading the XMLFile xmlFile =newFile("college.xml");//Creating UserHandler objectUserHandler userHandler =newUserHandler();//Parsing the XML Document
         saxParser.parse(xmlFile, userHandler);}catch(Exception e){
              e.printStackTrace();}}}</code></pre>

    The ouput window displays names of each element along with the attributes.

    Root Element : college
    
    Current Element : department
    Department code : DEP_CS23
    Department Name : Computer Science
    Staff Count : 20
    
    Current Element : department
    Department code : DEP_EC34
    Department Name : Electrical and Electronics
    Staff Count : 23
    
    Current Element : department
    Department code : DEP_MC89
    Department Name : Mechanical
    Staff Count : 15
    
  • Overview

    Java SAX (Simple API for XML) is an event-based parser to parse XML documents. Unlike DOM parser, SAX parser does not create a parse tree. It will not load the entire document into memory, instead, it reads through the XML document and notifies the client program whenever it encounters elements, attributes, text content and other data items in the form of events. These events are handled by the methods implemented inside the Event Handler.

    What does SAX Parser do?

    A SAX Parser does the following to a client program −

    • Reads the XML document from top to bottom and identifies the tokens.
    • Processes the tokens in the same order of their appearance.
    • Reports the parser about the nature of the tokens.
    • Invokes the callback methods in the Event handler based on the identified tokens.

    When to Use Java SAX Parser?

    You should use a SAX parser when −

    • You want to process an XML document in a linear fashion from top to bottom.
    • The document is not deeply nested.
    • Your XML document is very large.
    • The problem to be solved involves only a part of the XML document.
    • You have streaming data (data is available as soon as it is seen by the parser).

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

    Advantages

    Following are some advantages of Java SAX Parser −

    • Consumes less memory
    • It is faster than DOM parser. Because, we need not wait for the entire document to get loaded in the memory.
    • You can still process the XML documents larger than the system memory.

    Disadvantages

    Here are some of the disadvantages of Java SAX Parser −

    • Random access to an XML document is not possible.
    • Creating XML documents is not possible.
    • If you want to keep track of data that the parser has seen or change the order of items, you must write the code and store the data on your own.

    ContentHandler Interface

    ContentHandler interface is the main interface in org.xml.sax package. Most of the application programs implement this interface to perform basic parsing events. These events include start and end of a document, start and end of the elements and character data. We must implement and register a Handler to perform any task in the XML document.

    There are built-in classes, namely, DefaultHandler, DefaultHandler2, ValidatorHandler that implement ContentHandler interface. We can use these classes to implement our user defined Handlers.

    This interface specifies the callback methods that the SAX parser uses to notify an application program of the components of the XML document that it has seen. Following are the methods of ContentHandler interface −

    MethodDescription
    void startDocument()Called at the beginning of a document.
    void endDocument()Called at the end of a document.
    void startElement(String uri, String localName, String qName, Attributes atts)Called at the beginning of an element.
    void endElement(String uri, String localName,String qName)Called at the end of an element.
    void characters(char[] ch, int start, int length)Called when character data is encountered.
    void ignorableWhitespace( char[] ch, int start, int length)Called when a DTD is present and ignorable whitespace is encountered.
    void processingInstruction(String target, String data)Called when a processing instruction is recognized.
    void setDocumentLocator(Locator locator))Provides a Locator that can be used to identify positions in the document.
    void skippedEntity(String name)Called when an unresolved entity is encountered.
    void startPrefixMapping(String prefix, String uri)Called when a new namespace mapping is defined.
    void endPrefixMapping(String prefix)Called when a namespace definition ends its scope.

    Attributes Interface

    The Attributes interface is in the package org.xml.sax. This interface is for the list of XML attributes specified in an Element. Following are the most commonly used methods of Attributes interface −

    MethodDescription
    int getLength()Returns number of attributes.
    int getIndex(String qName)Returns the index of the attribute in the list, -1 if not present.
    String getQName(int index)Returns the attribute name by index, null if the index is out of bounds.
    String getType(int index)Returns the type (“CDATA”, “ID”, “IDREF”, etc.) of attribute by index.
    String getValue(int index)Returns the value of attribute by index, null if index is out of bounds.
    String getValue(String qName)Returns the value of attribute by name, null if name is not present.
  • Modify XML Document

    Java DOM parser API provides methods to modify the already existing XML documents. We can add or remove elements and attributes, modify the text content of elements and attribute values using the methods of Java DOM parser.

    The setTextContent() method replaces the original text of elements, removeAttribute() removes the existing attributes and the setAttribute() method sets the new attributes to elements.

    Modify XML Using Java DOM Parser

    We can modify an XML document in java using DOM parser through following steps.

    • Step 1: Creating a DocumentBuilder Object
    • Step 2: Reading the XML
    • Step 3: Parsing the XML Document
    • Step 4: Updating the content of XML document
    • Step 5: Writing the content into XML file
    • Step 6: Output to console for testing

    Step4: Updating the content of XML document

    We can update text content, attribute values, add new elements and new attributes to our existing XML documents.

    Element element = xmldoc.getDocumentElement();
    element.setTextContent("14");
    element.removeAttribute("attr_name");
    element.setAttribute("attr_name","attr_value");

    The setTextContent(“text_content”) method is used to set the text content of an Element. This method is used with the Element object and takes String as an argument.

    The removeAttribute(“attr_name”) method removes the attribute from the Element object. It throws NO_MODIFICATION_ALLOWED_ERR error if the Element from which we want to remove the attribute is readonly.

    The setAttribute(“attr_name”,”attr_value”) method takes attribute name and attribute value as arguments and sets it to the Element object.

    Updating Text Content

    Let us consider college.xml file where we have three department details. Now, we will try to update staff count for Electrical and Electronics department from 23 to 14. The method setTextContent(“text”) is used to update the text content of an element.

    college.xml

    Following is the college.xml file before updating. Now let us try to update this in our java program.

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><college><department id="101"><name>Computer Science</name><staffCount>20</staffCount></department><department id="102"><name>Electrical and Electronics</name><staffCount>23</staffCount></department><department id="103"><name>Mechanical</name><staffCount>15</staffCount></department></college>

    ModifyXMLDemo.java

    In the following java program, we retrieved all the nodes with tag name, “department” into a NodeList. Then, we iterated all the nodes to find “Electrical and Electronics” department and changed the staffCount attribute to 14.

    importjava.io.File;importjava.io.FileOutputStream;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.transform.Transformer;importjavax.xml.transform.TransformerFactory;importjavax.xml.transform.dom.DOMSource;importjavax.xml.transform.stream.StreamResult;importorg.w3c.dom.Document;importorg.w3c.dom.Element;importorg.w3c.dom.NodeList;publicclassModifyXMLDemo{publicstaticvoidmain(String argv[]){try{//Creating a DocumentBuilder ObjectDocumentBuilderFactory docFactory =DocumentBuilderFactory.newInstance();DocumentBuilder docBuilder = docFactory.newDocumentBuilder();//Reading the XML fileFile inputFile =newFile("src/modifydom.xml");//Parsing the XML DocumentDocument doc = docBuilder.parse(inputFile);//Updating the staffCount for "Electrical and Electronics" departmentNodeList deptList=doc.getElementsByTagName("department");for(int i=0;i<deptList.getLength();i++){Element element=(Element)(deptList.item(i));String s=element.getElementsByTagName("name").item(0).getTextContent();if(s.equals("Electrical and Electronics")){Element staffCount =(Element) element.getElementsByTagName("staffCount").item(0);
    
            	staffCount.setTextContent("14");}}//Writing the updated content into the fileTransformerFactory transformerFactory =TransformerFactory.newInstance();Transformer transformer = transformerFactory.newTransformer();DOMSource source =newDOMSource(doc);FileOutputStream output =newFileOutputStream("college.xml");StreamResult result =newStreamResult(output);
         transformer.transform(source, result);//writing the content on consoleStreamResult consoleResult =newStreamResult(System.out);
         transformer.transform(source, consoleResult);}catch(Exception e){ e.printStackTrace();}}}</code></pre>

    Output

    Following is the updated XML document after updating staffCount.

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><college>
       <department id="101">
    
      &lt;name&gt;Computer Science&lt;/name&gt;
      &lt;staffCount&gt;20&lt;/staffCount&gt;
    </department> <department id="102">
      &lt;name&gt;Electrical and Electronics&lt;/name&gt;
      &lt;staffCount&gt;14&lt;/staffCount&gt;
    </department> <department id="103">
      &lt;name&gt;Mechanical&lt;/name&gt;
      &lt;staffCount&gt;15&lt;/staffCount&gt;
    </department> </college>

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

    Adding New Elements

    Now, let us go a bit further and try to add one more department named "Civil" to our above "college.xml" file. To add new elements, we can use createElement("Element_name") method to create and appendChild(Element) to append the element to the existing document's root element.

    ModifyXMLAddElementsDemo.java

    In the following program, we first got the root element by using getDocumentElement() method. Then, we created "Civil" department element and added to the root element.

    importjava.io.File;importjava.io.FileOutputStream;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.transform.Transformer;importjavax.xml.transform.TransformerFactory;importjavax.xml.transform.dom.DOMSource;importjavax.xml.transform.stream.StreamResult;importorg.w3c.dom.Document;importorg.w3c.dom.Element;publicclassModifyXMLAddElementsDemo{publicstaticvoidmain(String argv[]){try{//Creating a DocumentBuilder Object DocumentBuilderFactory docFactory =DocumentBuilderFactory.newInstance();DocumentBuilder docBuilder = docFactory.newDocumentBuilder();//Reading the XML fileFile inputFile =newFile("college.xml");//Parsing the XML DocumentDocument doc = docBuilder.parse(inputFile);//Adding new department elementElement rootElement = doc.getDocumentElement();Element department = doc.createElement("department");
    
         department.setAttribute("id","104");Element name = doc.createElement("name");Element staffCount = doc.createElement("staffCount");
         department.appendChild(name);
         department.appendChild(staffCount);
         name.appendChild(doc.createTextNode("Civil"));
         staffCount.appendChild(doc.createTextNode("10"));
         rootElement.appendChild(department);//Creating transformer objectTransformerFactory transformerFactory =TransformerFactory.newInstance();Transformer transformer = transformerFactory.newTransformer();//Writing updated content into the fileDOMSource source =newDOMSource(doc);FileOutputStream output =newFileOutputStream("college.xml");StreamResult result =newStreamResult(output);
         transformer.transform(source, result);//writing the content on consoleStreamResult consoleResult =newStreamResult(System.out);
         transformer.transform(source, consoleResult);}catch(Exception e){ e.printStackTrace();}}}</code></pre>

    Output

    The updated file after adding "Civil" department is as follows :

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><college>
       <department id="101">
    
      &lt;name&gt;Computer Science&lt;/name&gt;
      &lt;staffCount&gt;20&lt;/staffCount&gt;
    </department> <department id="102">
      &lt;name&gt;Electrical and Electronics&lt;/name&gt;
      &lt;staffCount&gt;14&lt;/staffCount&gt;
    </department> <department id="103">
      &lt;name&gt;Mechanical&lt;/name&gt;
      &lt;staffCount&gt;15&lt;/staffCount&gt;
    </department> <department id="104"><name>Civil</name><staffCount>10</staffCount></department></college>
  • Create XML Document

    Java DOM parser API has methods, interfaces and classes to create XML documents. Using this API, we can create XML documents from the scratch through our Java applications. The createElement() method creates new elements and the appendChild() method appends the created elements to already existing elements.

    Create XML Using Java DOM Parser

    We can create an XML document in Java using DOM parser through following steps −

    • Step 1: Creating a DocumentBuilder Object
    • Step 2: Create a new Document
    • Step 3: Creating the root element
    • Step 4: Appending elements to the root element
    • Step 5: Writing the content into XML file
    • Step 6: Testing the output using console

    Refer this page for step 1.

    Step2: Create a new Document

    Using DocumentBuilder object created in the above step, create a new document with newDocument() function.

    Document doc = dBuilder.newDocument();

    Step3: Creating the root element

    Every XML document should possess a single root element , which is also called a parent element. We can create the root element using createElement() method and append it to the document created in the previous step.

    Element rootElement = doc.createElement("root");
    doc.appendChild(rootElement);

    Step 4: Appending elements to the root element

    Inside the root element, we can create any number of child elements and append them the same way we appended root element to the document. To write text content inside an element, we can use createTextNode() method. We can also create attributes for elements using createAttribute() method.

    Element child = doc.createElement("child");
    rootElement.appendChild(child);
    child.appendChild(doc.createTextNode("text_content_here"));Attr attr = doc.createAttribute("child");

    Step 5: Writing the content into XML file

    After building the elements inside the document with their corresponding attributes, we need to write this content into an XML file by creating Tranformer object, which in-turn transforms our source document into StreamResult and stores it in the specified file path with the given name.

    TransformerFactory transformerFactory =TransformerFactory.newInstance();Transformer transformer = transformerFactory.newTransformer();DOMSource source =newDOMSource(doc);StreamResult result =newStreamResult(newFile("filepath:\\new.xml"));
    transformer.transform(source, result);

    Step 6: Testing the output using console

    We can test our XML file by printing it on the console. This is an optional step.

    StreamResult consoleResult =newStreamResult(System.out);
    transformer.transform(source, consoleResult);

    Creating Basic XML File

    To create a document element, we use the method createElement(“element_name”). This method takes element name as an argument and returns a new Element object.

    After getting the new Element object, we can use appendChild(Element) method to append this Element object to the document. This method is also used to append an element to another element (creating child elements).

    cars.xml

    We want to create the following xml file with the name “cars.xml” in D drive.

    <cars><supercars>Ferrari</supercars></cars>

    CreateXMLDemo.java

    In the following code, we have created one root element with name “cars” and appended one child element with name “supercars”. We have also added text content to supercars element.

    importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.transform.Transformer;importjavax.xml.transform.TransformerFactory;importjavax.xml.transform.dom.DOMSource;importjavax.xml.transform.stream.StreamResult;importorg.w3c.dom.Document;importorg.w3c.dom.Element;importjava.io.File;publicclassCreateXMLDemo{publicstaticvoidmain(String argv[]){try{//Creating a DocumentBuilder ObjectDocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance();DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();//Create a new DocumentDocument doc = dBuilder.newDocument();//Creating the root elementElement rootElement = doc.createElement("cars");
    
         doc.appendChild(rootElement);//Appending elements to the root elementElement supercar = doc.createElement("supercars");
         rootElement.appendChild(supercar);
         supercar.appendChild(doc.createTextNode("Ferrari"));//writing the content into XML fileTransformerFactory transformerFactory =TransformerFactory.newInstance();Transformer transformer = transformerFactory.newTransformer();DOMSource source =newDOMSource(doc);StreamResult result =newStreamResult(newFile("D:\\cars.xml"));
         transformer.transform(source, result);//Output to console for testingStreamResult consoleResult =newStreamResult(System.out);
         transformer.transform(source, consoleResult);}catch(Exception e){e.printStackTrace();}}}</code></pre>

    Output

    For testing, we have printed the document output on the console.

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><cars><supercars>Ferrari</supercars></cars>

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

    Creating XML File with Attributes

    Attributes can be created to an Element using the method, createAttribute("Attribute_name"). This method of Document interface takes name of the attribute as an argument and returns the Attr object. The setValue("value") method is used to set the value of the attribute.

    Now, to set this attribute node to the Element, we use setAttributeNode(Attr) method with the Element object to which we need to set this attribute.

    newcars.xml

    We need to create the following "newcars.xml" in D drive.

    <cars><supercars company="Ferrari"><carname type="formula one">Ferrari 101</carname><carname type="sports">Ferrari 202</carname></supercars></cars>

    CreateXMLAttributeDemo.java

    We have created two carname elements for supercars element. Also, using setAttributeNode() method, we have added two attributes for each of the carname elements.

    importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.transform.Transformer;importjavax.xml.transform.TransformerFactory;importjavax.xml.transform.dom.DOMSource;importjavax.xml.transform.stream.StreamResult;importorg.w3c.dom.Attr;importorg.w3c.dom.Document;importorg.w3c.dom.Element;importjava.io.File;publicclassCreateXMLAttributeDemo{publicstaticvoidmain(String argv[]){try{//Creating a DocumentBuilder ObjectDocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance();DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();//Creating a new DocumentDocument doc = dBuilder.newDocument();//Creating the root elementElement rootElement = doc.createElement("cars");
    
         doc.appendChild(rootElement);//Appending sub element to the root elementElement supercar = doc.createElement("supercars");
         rootElement.appendChild(supercar);//Setting attribute to the sub elementAttr attr = doc.createAttribute("company");
         attr.setValue("Ferrari");
         supercar.setAttributeNode(attr);//Adding First child element to sub elementElement carname = doc.createElement("carname");Attr attrType = doc.createAttribute("type");
         attrType.setValue("formula one");
         carname.setAttributeNode(attrType);
         carname.appendChild(doc.createTextNode("Ferrari 101"));
         supercar.appendChild(carname);//Adding second child element to sub elementElement carname1 = doc.createElement("carname");Attr attrType1 = doc.createAttribute("type");
         attrType1.setValue("sports");
         carname1.setAttributeNode(attrType1);
         carname1.appendChild(doc.createTextNode("Ferrari 202"));
         supercar.appendChild(carname1);//writing the content into XML fileTransformerFactory transformerFactory =TransformerFactory.newInstance();Transformer transformer = transformerFactory.newTransformer();DOMSource source =newDOMSource(doc);StreamResult result =newStreamResult(newFile("D:\\newcars.xml"));
         transformer.transform(source, result);//Output to console for testingStreamResult consoleResult =newStreamResult(System.out);
         transformer.transform(source, consoleResult);}catch(Exception e){ e.printStackTrace();}}}</code></pre>

    Output

    For testing, we have printed the document output on the console.

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

    Java DOM parser is an API in java to parse and query XML documents. Using Java DOM parser, we can query large XML documents to get insights about our data. Manually, it is not easy to check the entire XML document to obtain related information. We can query XML elements by their tag name using getElementsByTagName() method. To query based on attribute value, getAttribute() method can be used.

    Query XML Using Java DOM Parser

    We can query any XML document in Java using DOM parser through following steps −

    • Step 1: Creating a DocumentBuilder Object
    • Step 2: Reading the XML
    • Step 3: Parsing the XML Document
    • Step 4: Querying the XML Document

    Refer this page for first three steps.

    Step4: Querying the XML Document

    After following the first three steps we can query the XML document according to our business requirements using DOM methods.

    Querying Elements by TagName

    We can query the XML elements by their tag name by using the method, getElementsByTagName(‘carname’) on the Document interface. This method takes tag name in the form of a String and returns the list of nodes with the same tag name as a NodeList.

    The getTextContent() method of Node interface gives the text content inside the node in the form of a String.

    cars.xml

    The cars.xml file has seven <carname> elements inside the root element, <cars>.

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

    Query XML Elements

    In the following program, we got all the nodes into a nodeList and then iterated each node to get the text content and checked if that equals to “Bentley 2”. If it is found, we are printing on the console that it is found or else we are printing not found.

    importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.parsers.DocumentBuilder;importorg.w3c.dom.Document;importorg.w3c.dom.NodeList;importjava.io.File;publicclassQueryXMLDemo{publicstaticvoidmain(String argv[]){try{//Creating a DocumentBuilder ObjectDocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance();DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();//Reading the XML fileFile inputFile =newFile("cars.xml");//Parsing the XML DocumentDocument doc = dBuilder.parse(inputFile);//checking "Bentley 2" carint flag=0;NodeList nList = doc.getElementsByTagName("carname");for(int i=0;i<nList.getLength();i++){if(nList.item(i).getTextContent().equals("Bentley 2")){System.out.println("Bentley 2 car is "+"found");
    
            	flag=1;break;}}if(flag==0){System.out.println("Bentley 2 car is "+"not found");}}catch(Exception e){e.printStackTrace();}}}</code></pre>

    Output

    Since, "Bentley 2" car is present, it displays that it 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 Elements by Attributes

    We can query the elements by their attributes using the method getAttribute("Attribute_name") of Element interface. This method takes attribute name as an argument and returns value of the attribute.

    Example

    In the following program, we have parsed cars.xml file and incremented the count variable every time "Bentley" is found.

    importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.parsers.DocumentBuilder;importorg.w3c.dom.Document;importorg.w3c.dom.NodeList;importorg.w3c.dom.Element;importjava.io.File;publicclassQueryXMLAttributes{publicstaticvoidmain(String argv[]){try{//Creating a DocumentBuilder ObjectDocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance();DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();//Reading the XML fileFile inputFile =newFile("cars.xml");//Parsing the XML DocumentDocument doc = dBuilder.parse(inputFile);//counting Bentley carsint count=0;NodeList nList = doc.getElementsByTagName("carname");for(int i=0;i<nList.getLength();i++){Element ele =(Element) nList.item(i);if(ele.getAttribute("company").equals("Bentley")){
    
            	count++;}}System.out.println("No of Bentley cars: "+ count);}catch(Exception e){e.printStackTrace();}}}</code></pre>

    Output

    The above program counts the number of Bentley cars and displays on the console.

    No of Bentley cars: 3
    
  • Parse XML Document

    Java DOM parser is a Java API to parse any XML document. Using the methods provided, we can retrieve root element, sub elements and their attributes using Java DOM parser.

    In this tutorial we have used the getTagName() method to retrieve the tag name of elements, getFirstChild() to retrieve the first child of an element and getTextContent() to get the text content of elements.

    Parse XML Using Java DOM parser

    Having discussed various XML parsers available in Java, now let us see how we can use DOM parser to parse an XML file. We use parse() method to parse an XML file. Before jumping into the example directly, let us see the steps to parse XML document using Java DOM parser −

    • Step 1: Creating a DocumentBuilder Object
    • Step 2: Reading the XML
    • Step 3: Parsing the XML Document
    • Step 4: Retrieving the Elements

    Step 1: Creating a DocumentBuilder Object

    DocumentBuilderFactory is a factory API to obtain parser to parse XML documents by creating DOM trees. It has ‘newDocumentBuilder()’ method that creates an instance of the class ‘DocumentBuilder’. This DocumentBuilder class is used to get input in the form of streams, files, URLs and SAX InputSources.

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

    Step 2: Reading the XML

    Input can be of file type or stream type. To input an XML file, Create a file object and pass the file path as argument.

    File xmlFile =newFile("input.xml");

    To get input in the form of stream, we have used StringBuilder class and appended the input string and later converted it into bytes. The obtained ByteArrayInputStream is given as input to the document.

    StringBuilder xmlBuilder =newStringBuilder(); 
    xmlBuilder.append("<?xml version="1.0"?> <rootElement></rootElement>");ByteArrayInputStream input =newByteArrayInputStream( xmlBuilder.toString().getBytes("UTF-8"));

    Step 3: Parsing the XML Document

    DocumentBuilder created in above steps is used to parse the input XML file. It contains a method named parse() which accepts a file or input stream as a parameter and returns a DOM Document object. If the given file or input stream is NULL, this method throws an IllegalArgumentException.

    Document xmldoc = docBuilder.parse(input);

    Step4: Retrieving the Elements

    The Node and Element interfaces of the org.w3c.dom. package provides various methods to retrieve desired information about elements from the XML documents. This information includes element’s name, text content, attributes and their values. We have many DOM interfaces and methods to get this information.

    Retrieving Root Element Name

    XML document constitutes of many elements. In Java an XML/HTML document is represented by the interface named Element. This interface provides various methods to retrieve, add and modify the contents of an XML/HTML document.

    We can retrieve the name of the root element using the method named getTagName() of the Element interface. It returns the name of the root element in the form of a string.

    Since Element is an interface, to create its object we need to use the getDocumentElement() method. This method retrieves and returns the root element in the form of an object.

    Example

    In the following example we have passed a simple XML document with just one root element ‘college’ using StringBuilder class. Then, we are retrieving it and printing on the console.

    Open Compiler

    importjavax.xml.parsers.DocumentBuilderFactory;importorg.w3c.dom.Document;importorg.w3c.dom.Element;importjava.io.ByteArrayInputStream;importjavax.xml.parsers.DocumentBuilder;publicclassRetrieveRootElementName{publicstaticvoidmain(String[] args){try{//Creating a DocumentBuilder ObjectDocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();DocumentBuilder docBuilder = factory.newDocumentBuilder();//Reading the XMLStringBuilder xmlBuilder =newStringBuilder(); 
    
         xmlBuilder.append("&lt;college&gt;&lt;/college&gt;");//Parsing the XML DocumentByteArrayInputStream input =newByteArrayInputStream(xmlBuilder.toString().getBytes("UTF-8"));Document xmldoc = docBuilder.parse(input);//Retrieving the Root Element NameElement element = xmldoc.getDocumentElement();System.out.println("Root element name is "+element.getTagName());}catch(Exception e){
         e.printStackTrace();}}}</code></pre>

    Output

    The element name, 'college' is displayed on the output screen as shown below −

    Root element name is college
    

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

    Parsing Single Sub Element in XML

    We can parse a simple XML document with single element inside the root element. So far, we have seen how to retrieve the root element. Now, let us see how to get the sub element inside the root element.

    Since, we have only one sub element, we are using getFirstChild() method to retrieve it. This method is used with the root element to get its first child. It returns the child node in the form of a Node object.

    After retrieving the child node, getNodeName() method is used to get the name of the node. It returns the node name in the form of a string.

    To get the text content, we use getTextContent() method. It returns the text content in the form of a String.

    Example

    Let us see the following example where we have one root element and a sub element. Here, 'college' is the root element with 'department' as sub element. The 'department' element has text content, "Computer Science". We are retrieving the name and text content of the sub element.

    Open Compiler

    importjavax.xml.parsers.DocumentBuilderFactory;importorg.w3c.dom.Document;importorg.w3c.dom.Element;importorg.w3c.dom.Node;importjava.io.ByteArrayInputStream;importjavax.xml.parsers.DocumentBuilder;publicclassSingleSubElement{publicstaticvoidmain(String[] args){try{//Creating a DocumentBuilder ObjectDocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();DocumentBuilder docBuilder = factory.newDocumentBuilder();//Reading the XMLStringBuilder xmlBuilder =newStringBuilder(); 
    
    	 xmlBuilder.append("&lt;college&gt;&lt;department&gt;Computer Science&lt;/department&gt;&lt;/college&gt;");//Parsing the XML DocumentByteArrayInputStream input =newByteArrayInputStream(xmlBuilder.toString().getBytes("UTF-8"));Document xmldoc = docBuilder.parse(input);//Retrieving the Root ElementElement element = xmldoc.getDocumentElement();//Retrieving the Child NodeNode childNode = element.getFirstChild();String childNodeName = childNode.getNodeName();System.out.println("Sub Element name : "+ childNodeName);//Retrieving Text Content of the Child Node "+ childNodeName);System.out.println("Text content of Sub Element : "+childNode.getTextContent());}catch(Exception e){
    	  e.printStackTrace();}}}</code></pre>

    The output window displays Sub element name and text content.

    Sub Element name : department
    Text content of Sub Element : Computer Science
    

    Parsing Multiple Elements in XML

    To parse an XML document with multiple elements we need to use loops. The getChildNodes() method retrieves all the child nodes of an element and returns it as a NodeList. We need to loop through all the elements of the obtained NodeList and retrieve the desired information about each element as we did in the previous sections.

    Example

    Now, let us add two more departments to the XML file (multipleElements.xml). Let us try to retrieve all the department names and staff count.

    <college><department><name>Computer Science</name><staffCount>20</staffCount></department><department><name>Electrical and Electronics</name><staffCount>23</staffCount></department><department><name>Mechanical</name><staffCount>15</staffCount></department></college>

    In the following program, we retrieve the list of department elements into a NodeList and iterate all the departments to get the department name and staff count.

    importjava.io.File;importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.parsers.DocumentBuilder;importorg.w3c.dom.Document;importorg.w3c.dom.NodeList;importorg.w3c.dom.Node;importorg.w3c.dom.Element;publicclassMultipleElementsXmlParsing{publicstaticvoidmain(String[] args){try{//Input the XML fileFile inputXmlFile =newFile("src/multipleElements.xml");//creating DocumentBuilderDocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance();DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();Document xmldoc = docBuilder.parse(inputXmlFile);//Retrieving the Root ElementElement element = xmldoc.getDocumentElement();System.out.println("Root element name is "+element.getTagName());//Getting the child elements ListNodeList nList = element.getChildNodes();//Iterating through all the child elements of the rootfor(int temp =0; temp < nList.getLength(); temp++){Node nNode = nList.item(temp);System.out.println("\nCurrent Element :"+ nNode.getNodeName());if(nNode.getNodeType()==Node.ELEMENT_NODE){Element eElement =(Element) nNode;System.out.println("Name of the department : "+ eElement.getElementsByTagName("name").item(0).getTextContent());System.out.println("Staff Count of the department : "+ eElement.getElementsByTagName("staffCount").item(0).getTextContent());}}}catch(Exception e){
    
             e.printStackTrace();}}}</code></pre>

    All the three departments with name and staff count are displayed.

    Root element :college
    
    Current Element :department
    Name of the department : Computer Science
    Staff Count of the department : 20
    
    Current Element :department
    Name of the department : Electrical and Electronics
    Staff Count of the department : 23
    
    Current Element :department
    Name of the department : Mechanical
    Staff Count of the department : 15
    

    Parsing Attributes in XML

    XML elements can have attributes and these can be retrieved using the getAttribute() method. This method takes attribute name as a parameter and returns its corresponding attribute value as a String. It returns an empty string if there is no attribute value or default value for the attribute name specified.

    Example

    Now, let us add an attribute, 'deptcode' to all the department elements in the 'attributesParsing.xml' file.

    <?xml version = "1.0"?><college><department deptcode = "DEP_CS23"><name>Computer Science</name><staffCount>20</staffCount></department><department deptcode = "DEP_EC34"><name>Electrical and Electronics</name><staffCount>23</staffCount></department><department deptcode = "DEP_MC89"><name>Mechanical</name><staffCount>15</staffCount></department></college>

    In the following program, we are retrieving deptcode along with name and staff count for each department.

    importjava.io.File;importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.parsers.DocumentBuilder;importorg.w3c.dom.Document;importorg.w3c.dom.NodeList;importorg.w3c.dom.Node;importorg.w3c.dom.Element;publicclassAttributesXmlParsing{publicstaticvoidmain(String[] args){try{//Input the XML fileFile inputXmlFile =newFile("attributesParsing.xml");//creating DocumentBuilderDocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance();DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();Document xmldoc = docBuilder.parse(inputXmlFile);//Getting the root elementSystem.out.println("Root element :"+ xmldoc.getDocumentElement().getNodeName());NodeList nList = xmldoc.getElementsByTagName("department");//Iterating through all the child elements of the rootfor(int temp =0; temp < nList.getLength(); temp++){Node nNode = nList.item(temp);System.out.println("\nCurrent Element :"+ nNode.getNodeName());if(nNode.getNodeType()==Node.ELEMENT_NODE){Element eElement =(Element) nNode;System.out.println("Department Code : "+ eElement.getAttribute("deptcode"));System.out.println("Name of the department : "+ eElement.getElementsByTagName("name").item(0).getTextContent());System.out.println("Staff Count of the department : "+ eElement.getElementsByTagName("staffCount").item(0).getTextContent());}}}catch(Exception e){
    
         e.printStackTrace();}}}</code></pre>

    The three departments are displayed with their corresponding department code, name and staff count.

    Root element :college
    
    Current Element :department
    Department Code : DEP_CS23
    Name of the department : Computer Science
    Staff Count of the department : 20
    
    Current Element :department
    Department Code : DEP_EC34
    Name of the department : Electrical and Electronics
    Staff Count of the department : 23
    
    Current Element :department
    Department Code : DEP_MC89
    Name of the department : Mechanical
    Staff Count of the department : 15
    
  • Overview

    Java DOM parser is an API (Application Programming Interface) that has classes, interfaces and methods to parse XML documents by creating DOM tree structure.

    The Document Object Model (DOM) is an official recommendation of the World Wide Web Consortium (W3C). It defines an interface that enables programs to access and update the style, structure, and contents of XML documents.

    When to use Java DOM Parser?

    You should use a DOM parser when −

    • You need to know a lot about the structure of a 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.

    What is the result of Parsing?

    When you parse an XML document with a DOM parser, you will get a tree structure that contains all of the elements of your document. The DOM provides a variety of functions you can use to examine the contents and structure of 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.

    Advantages

    Following are some advantages of Java DOM parser −

    • DOM is a very simple API to use.
    • Easy to access and modify any part of the document since entire document is loaded into memory.
    • Java code written for one DOM-compliant parser should run on any other DOM-compliant parser without having to do any modifications.

    Disadvantages

    Following are some of the disadvantages of Java DOM parser −

    • Consumes more memory
    • Not suitable for large documents.
    • Not applicable for small devices like PDAs and cellular phones.

    DOM Interfaces

    Here are the most commonly used DOM interfaces −

    InterfaceDescription
    DocumentRepresents the XML or HTML document.
    Nodeprimary data type of the Document Object Model (DOM)
    NodeListan ordered collection of nodes.
    Elementrepresents an element in XML document.
    Attrrepresents an attribute of an Element object.
    Textrepresents textual content of an element or attribute, inherited from ‘CharacterData’ interface.

    DOM Methods

    Here are the most commonly used DOM methods −

    MethodDescription
    Document.getDocumentElement()returns the root element of the document.
    Document.getElementsByTagName()returns a NodeList of all elements within the given tag name.
    Node.getFirstChild()returns the first child of a given Node.
    Node.getLastChild()returns the last child of a given Node.
    Node.getNextSibling()returns the root element of the document.
    Node.getPreviousSibling()returns the root element of the document.
    Node.getAttribute(attrName)returns the attribute with the requested name for a given node.
    Node.getNodeName()returns the name of the node.
    Node.getTextContent()returns the text content of the current node and its descendants.
    NodeList.getLength()returns the number of nodes in the NodeList.
    NodeList.item(int index)returns the indexth node from the NodeList.