[Java] Java中分析和读取xml文件 →→→→→进入此内容的聊天室

来自 , 2019-07-17, 写在 Java, 查看 100 次.
URL http://www.code666.cn/view/fc2c7c47
  1. package net.viralpatel.java.xmlparser;
  2.  
  3. import java.io.File;
  4. import javax.xml.parsers.DocumentBuilder;
  5. import javax.xml.parsers.DocumentBuilderFactory;
  6.  
  7. import org.w3c.dom.Document;
  8. import org.w3c.dom.Element;
  9. import org.w3c.dom.Node;
  10. import org.w3c.dom.NodeList;
  11.  
  12. public class XMLParser {
  13.  
  14.     public void getAllUserNames(String fileName) {
  15.         try {
  16.             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  17.             DocumentBuilder db = dbf.newDocumentBuilder();
  18.             File file = new File(fileName);
  19.             if (file.exists()) {
  20.                 Document doc = db.parse(file);
  21.                 Element docEle = doc.getDocumentElement();
  22.  
  23.                 // Print root element of the document
  24.                 System.out.println("Root element of the document: "
  25.                         + docEle.getNodeName());
  26.  
  27.                 NodeList studentList = docEle.getElementsByTagName("student");
  28.  
  29.                 // Print total student elements in document
  30.                 System.out
  31.                         .println("Total students: " + studentList.getLength());
  32.  
  33.                 if (studentList != null && studentList.getLength() > 0) {
  34.                     for (int i = 0; i < studentList.getLength(); i++) {
  35.  
  36.                         Node node = studentList.item(i);
  37.  
  38.                         if (node.getNodeType() == Node.ELEMENT_NODE) {
  39.  
  40.                             System.out
  41.                                     .println("=====================");
  42.  
  43.                             Element e = (Element) node;
  44.                             NodeList nodeList = e.getElementsByTagName("name");
  45.                             System.out.println("Name: "
  46.                                     + nodeList.item(0).getChildNodes().item(0)
  47.                                             .getNodeValue());
  48.  
  49.                             nodeList = e.getElementsByTagName("grade");
  50.                             System.out.println("Grade: "
  51.                                     + nodeList.item(0).getChildNodes().item(0)
  52.                                             .getNodeValue());
  53.  
  54.                             nodeList = e.getElementsByTagName("age");
  55.                             System.out.println("Age: "
  56.                                     + nodeList.item(0).getChildNodes().item(0)
  57.                                             .getNodeValue());
  58.                         }
  59.                     }
  60.                 } else {
  61.                     System.exit(1);
  62.                 }
  63.             }
  64.         } catch (Exception e) {
  65.             System.out.println(e);
  66.         }
  67.     }
  68.     // http://yuncode.net
  69.     public static void main(String[] args) {
  70.  
  71.         XMLParser parser = new XMLParser();
  72.         parser.getAllUserNames("c:\\test.xml");
  73.     }
  74. }
  75.  
  76. //源代码片段来自云代码http://yuncode.net
  77.                        

回复 "Java中分析和读取xml文件"

这儿你可以回复上面这条便签

captcha