Java read XML file

The specific code to read the XML file is as follows:
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;
  // can use absolute road strength
  File f = new File("TransData.xml");

  // documentBuilder is abstract and cannot be instantiated directly (converts XML files to DOM files)
  DocumentBuilder db = null;
  DocumentBuilderFactory dbf = null;
  try {
   // Return the documentBuilderFactory object
   dbf = DocumentBuilderFactory.newInstance();
   // Return the db object and use the documentBuilderFatory object to obtain the return documentBuilder object
   db = dbf.newDocumentBuilder();

   // Get a DOM and return it to the document object
   Document dt = db.parse(f);
   // get an element root element
   element = dt.getDocumentElement();
   // get the root node
   System.out.println("根元素:" + element.getNodeName());

   // Get the child nodes under the root element
   NodeList childNodes = element.getChildNodes();

   // traverse these child nodes
   for (int i = 0; i < childNodes.getLength(); i++) {
    // get each node corresponding to position i
    Node node1 = childNodes.item(i);
    if ("Account".equals(node1.getNodeName())) {
     // If the name of the node is "Account", output the Account element attribute type
     System.out.println("\r\nFound an account. The area it belongs to: " + node1.getAttributes().getNamedItem("type").getNodeValue() + ". ");
     // Get the nodes under <Accounts>
     NodeList nodeDetail = node1.getChildNodes();
     // Traverse the nodes under <Accounts>
     for (int j = 0; j < nodeDetail.getLength(); j++) {
      // Get each node of the <Accounts> element
      Node detail = nodeDetail.item(j);
      if ("code".equals(detail.getNodeName())) // 输出code
       System.out.println("卡号: " + detail.getTextContent());
      else if ("pass".equals(detail.getNodeName())) // 输出pass
       System.out.println("密码: " + detail.getTextContent());
      else if ("name".equals(detail.getNodeName())) // 输出name
       System.out.println("姓名: " + detail.getTextContent());
      else if ("money".equals(detail.getNodeName())) // 输出money
       System.out.println("余额: " + detail.getTextContent());
     }
    }

   }
  }

  catch (Exception e) {
   e.printStackTrace ();
  }
 }
}

The content of the read file TransData.xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Accounts>
 <Account type="type1">
  <code>100001</code>
  <pass>123</pass>
  <name>李四</name>
  <money>1000000.00</money>
 </Account>
 <Account type="type2">
  <code>100002</code>
  <pass>123</pass>
  <name>张三</name>
  <money>1000.00</money>
 </Account>
</Accounts>

The read result is as follows:

Root element: Accounts


found an account. Owned area: 
type1.Card number: 100001Password: 123Name
:
Li SiBalance
: 1000000.00Found


an account. Owned area  : type2.Card number:
100002Password :
123Name :
Zhang SanBalance
: 1000.00

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326080449&siteId=291194637