Java implementation of simple parsing XML file function example

This paper describes the function of simple parsing XML files in Java. Share with you for your reference, as follows:

package demo;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class JieXiXML {
  public static void main(String[] args) throws ParserConfigurationException,SAXException,IOException {
    DocumentBuilderFactory bdf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = bdf.newDocumentBuilder();
    Document document = db.parse(new File("user.xml"));
    NodeList list = document.getElementsByTagName("usa");
    System.out.println("编程小技巧测试结果:");
    for (int i = 0; i < list.getLength(); i++) {
      Element element = (Element)list.item(i);
      String name = element.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
      System.out.println(name);
      String pass = element.getElementsByTagName("pass").item(0).getFirstChild().getNodeValue();
      System.out.println(pass);
      System.out.println("------------------");
      }
  }
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>