How does Java read XML file implementation

  • 2020-04-01 02:35:05
  • OfStack

Today's CSDN FAQ shows you how to read the contents of an XML file in Java.

Directly on the code, comments written very clearly!


import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XMLReaderTest {
 public static void main(String args[]) {
  Element element = null;
  //Absolute path force can be used
  File f = new File("test.xml");
  //DocumentBuilder cannot be directly instantiated as an abstraction (converting XML files to DOM files)
  DocumentBuilder db = null;
  DocumentBuilderFactory dbf = null;
  try {
   //Returns the documentBuilderFactory object
   dbf = DocumentBuilderFactory.newInstance();
   //Returns the db object with the documentBuilderFatory object to get the returned documentBuildr object
   db = dbf.newDocumentBuilder();
   //You get a DOM and return it to the document object
   Document dt = db.parse(f);
   //Gets an elment root element
   element = dt.getDocumentElement();
   //Get the root node
   System.out.println(" The root element: " + element.getNodeName());
   //Gets the child node under the root element
   NodeList childNodes = element.getChildNodes();
   //Iterate over the child nodes
   for (int i = 0; i < childNodes.getLength(); i++) {
    //Get the node for each corresponding position I
    Node node1 = childNodes.item(i);
    if ("Account".equals(node1.getNodeName())) {
     //If the name of the node is "Account", the Account element attribute type is output
     System.out.println("rn Find an account number .  area : " + node1.getAttributes().getNamedItem("type").getNodeValue() + ". ");
     //Get <Accounts> The node
     NodeList nodeDetail = node1.getChildNodes();
     //Traverse <Accounts> The node
     for (int j = 0; j < nodeDetail.getLength(); j++) {
      //Get <Accounts> Element each node
      Node detail = nodeDetail.item(j);
      if ("code".equals(detail.getNodeName())) //The output code
       System.out.println(" The card number : " + detail.getTextContent());
      else if ("pass".equals(detail.getNodeName())) //Output pass
       System.out.println(" password : " + detail.getTextContent());
      else if ("name".equals(detail.getNodeName())) //Output the name
       System.out.println(" The name : " + detail.getTextContent());
      else if ("money".equals(detail.getNodeName())) //Output money
       System.out.println(" The balance of : " + detail.getTextContent());
     }
    }
   }
  }
  catch (Exception e) {
   e.printStackTrace();
  }
 }
}

Then the XML file we tested (test.xml) will be placed in the root directory of the project. Its contents are:


<?xml version="1.0" encoding="gbk"?>
<Accounts>
 <Account type="type1">
  <code>100001</code>
  <pass>123</pass>
  <name> Li si </name>
  <money>1000000.00</money>
 </Account>
 <Account type="type2">
  <code>100002</code>
  <pass>123</pass>
  <name> Zhang SAN </name>
  <money>1000.00</money>
 </Account>
</Accounts>

Directly run the code, output:

Root element: Accounts

Found an account. Area: type1.
Card number: 100001
Password: 123
Name: li si
Balance: 1000000.00

Found an account. Area: type2.
Card number: 100002
Password: 123
Name: zhang SAN
Balance: 1000.00


Related articles: