Java DOM parsing XML file process

Time:2020-8-1

This article mainly introduces the Java DOM parsing XML file process in detail, the article through the example code introduction is very detailed, to everyone’s study or work has certain reference learning value, need friends can refer to

The code is as follows

import java.io.IOException;
import javax.xml.parsers.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Domtest {

  public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
    //Create a documentbuilderfactory object
    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
    //Create a doucumentbuild object
    DocumentBuilder db=dbf.newDocumentBuilder();
    //Parsing the corresponding XML file
    Document doc=db.parse("tes.xml");
    //Get the node list according to the tag name
    NodeList nodelist=doc.getElementsByTagName("book");
    System.out.println ("common"+ nodelist.getLength () + "the book");
    //Traverse each book node
    for(int i=0;i<nodelist.getLength();i++) {  
      System.out.println ("No." + I + "this book");
      //Get book nodes
      //Get book using node type
      Node book=nodelist.item(i);
      System.out.println("Name: "+book.getNodeName()+" Value: "+book.getNodeValue()+" Type: "+book.getNodeType());
      //Gets the properties in the node node
      NamedNodeMap attrs= book.getAttributes();
      //Traverse to get properties
      for(int j=0;j<attrs.getLength();j++) { 
      Node x=attrs.item(j);
      //System.out.println(x.getNodeName()+" "+x.getNodeValue()+" "+x.getNodeType());
      }
      
      //Get node using element object
      Element node =(Element) nodelist.item(i); 
      //Use the getattribute method under the element object to get the attribute value of the specified name
      String id=node.getAttribute("id");
      System.out.println(id); 
      String type=node.getAttribute("type");
      System.out.println(type);
       
      //Using getchildnode under the node node can obtain the NodeList array for loop parsing
      NodeList childnode=book.getChildNodes();
      for(int j=0;j< childnode.getLength (); j + +) {// getlength, 9 nodes will be obtained, because the text type is also a node, one < name > < / name > counts as a node, so there are 9 nodes in total. Among these nodes, only the object node is needed 
        Node x=childnode.item(j);
        if(x.getNodeType()== Node.ELEMENT_ Node) {// when the node type is element, get the node
          //Gets the node name of element type
          System.out.println (the name of "node" + j + ": + x.getnodename() +" value: / "+ x.getlastchild(). Getnodevalue() +" / type is "+ x.getlastchild(). Getnodetype()); // < name > XYZ < name >, XYZ belongs to the child node of < name >, and the effect of using getfirstchild or getlastnode is the same
          System.out.println (the name of "node" + j + ": + x.getnodename() +" value: / "+ x.gettextcontent() +" / type "+ x.getnodetype()); // gettextcontent method can obtain all text content in the node. Change < name > XYZ < / name > to < name > < a > 123 < / A > XYZ < / name >, and xyz123 will be obtained
        }
      }
    }
  }
}
//In order to save the content of the obtained XML file, the content can be saved to the object array once to store the data
<?xml version="1.0" encoding="UTF-8" ?>
<Bookstore>
  <book type="text">
  < name > Song of ice and fire
  < author > George Martin
  <year>2014</year>
  <price>80</price>
  </book>
<book>
  < name > Andersen's Fairy Tales
  <year>2004</year>
  <price>79</price>
  <language>English</language>
</book>
</Bookstore>

Attention point

1. A blank newline character is also a node, so when traversing a node, you need to pay attention that these useless nodes will be mixed in the list

2. The returned name values of text class nodes are all ා text, while the value values returned by element class nodes are all null. Note that

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.