91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

java怎么讀取xml文件內容

小億
160
2023-11-16 10:25:15
欄目: 編程語言

在Java中讀取XML文件內容有多種方法,以下是兩種常見的方法:

  1. 使用DOM解析器:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;

public class ReadXMLUsingDOM {
   public static void main(String[] args) {
      try {
         File inputFile = new File("input.xml");
         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
         Document doc = dBuilder.parse(inputFile);
         doc.getDocumentElement().normalize();
         System.out.println("根元素:" + doc.getDocumentElement().getNodeName());
         NodeList nodeList = doc.getElementsByTagName("student");
         for (int temp = 0; temp < nodeList.getLength(); temp++) {
            Node node = nodeList.item(temp);
            System.out.println("\n當前元素:" + node.getNodeName());
            if (node.getNodeType() == Node.ELEMENT_NODE) {
               Element element = (Element) node;
               System.out.println("學號:" + element.getAttribute("id"));
               System.out.println("姓名:" + element.getElementsByTagName("name").item(0).getTextContent());
               System.out.println("年齡:" + element.getElementsByTagName("age").item(0).getTextContent());
               System.out.println("性別:" + element.getElementsByTagName("gender").item(0).getTextContent());
            }
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}
  1. 使用SAX解析器:
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ReadXMLUsingSAX {
   public static void main(String[] args) {
      try {
         File inputFile = new File("input.xml");
         SAXParserFactory factory = SAXParserFactory.newInstance();
         SAXParser saxParser = factory.newSAXParser();
         DefaultHandler handler = new DefaultHandler() {
            boolean bName = false;
            boolean bAge = false;
            boolean bGender = false;

            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
               if (qName.equalsIgnoreCase("name")) {
                  bName = true;
               }
               if (qName.equalsIgnoreCase("age")) {
                  bAge = true;
               }
               if (qName.equalsIgnoreCase("gender")) {
                  bGender = true;
               }
            }

            public void endElement(String uri, String localName, String qName) throws SAXException {
               if (qName.equalsIgnoreCase("student")) {
                  System.out.println();
               }
            }

            public void characters(char ch[], int start, int length) throws SAXException {
               if (bName) {
                  System.out.println("姓名:" + new String(ch, start, length));
                  bName = false;
               }
               if (bAge) {
                  System.out.println("年齡:" + new String(ch, start, length));
                  bAge = false;
               }
               if (bGender) {
                  System.out.println("性別:" + new String(ch, start, length));
                  bGender = false;
               }
            }
         };
         saxParser.parse(inputFile, handler);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

這兩種方法分別使用DOM解析器和SAX解析器來解析XML文件,可以根據需要選擇使用的解析器。

0
兰西县| 察隅县| 临西县| 高陵县| 龙门县| 屯昌县| 揭西县| 瓦房店市| 苏尼特左旗| 定南县| 湖州市| 青阳县| 盘山县| 同仁县| 乐昌市| 栾城县| 湘潭市| 定兴县| 临沂市| 剑川县| 大名县| 木兰县| 枣阳市| 宜川县| 巴林左旗| 扬州市| 都昌县| 揭阳市| 十堰市| 珠海市| 商水县| 乐山市| 安乡县| 开江县| 信宜市| 南漳县| 壶关县| 灵武市| 罗山县| 临漳县| 衡东县|