如何在Java中读取XML文件

本文概述

  • Java DOM解析器
  • 使用Eclipse在Java中读取XML文件的步骤
  • Java SAX解析器
用Java读取XML文件与读取其他文件(如.docx和.txt)有很大不同, 因为XML文件包含标签之间的数据。 Java提供了许多解析XML文件的方法。 Java中有两个解析器, 用于解析XML文件:
  • Java DOM解析器
  • Java SAX解析器
Java DOM解析器DOM API提供了用于读取和写入XML文件的类。我们可以使用DOM API创建, 删除, 修改和重新排列节点。 DOM解析器解析整个XML文件并在内存中创建DOM对象。它以树形结构对XML文件进行建模, 以便于遍历和操作。在DOM中, XML文件中的所有内容都是一个节点。该节点表示XML文件的组件。 DOM解析器处理缓慢, 并且在将XML文件加载到内存时会占用大量内存。
我们必须遵循以下过程来读取Java中的XML文件:
  • 实例化XML文件:DOM解析器将XML文件加载到内存中, 并将每个标记视为一个元素。
  • 获取根节点:Document类提供了getDocumentElement()方法来获取根节点和XML文件的元素。
  • 获取所有节点:getElementByTagName()方法从XML文件中检索所有特定的标记名称。其中ELEMENT_NODE类型是指具有子元素的非文本节点。如果需要从头开始访问所有节点(包括根节点), 则可以递归调用getChildElement()方法。
  • 按文本值获取节点:我们可以使用getElementByTextValue()方法来按其值搜索节点。
  • 按属性值获取节点:如果要按特定属性的值搜索节点, 则可以将getElementByTagName()方法与getAttribute()方法一起使用。
使用Eclipse在Java中读取XML文件的步骤步骤1:创建一个简单的Java项目。
步骤2:创建一个类文件并提供一个类文件名。我们创建了名为ReadXMLFileExample1的类文件。
步骤3:编写以下代码。
步骤4:下载dom-2.3.0-jaxb-1.0.6.jar文件:单击此处…
步骤5:在项目中创建一个lib文件夹。
步骤6:复制dom-2.3.0-jaxb-1.0.6.jar文件并粘贴到lib文件夹中。
步骤7:设定类别路径:
右键单击项目-> 构建路径-> 配置构建路径-> 添加外部JAR-> 选择JAR文件-> 单击打开按钮-> 应用并关闭。
步骤8:创建XML文件。我们已经创建了一个名为XMLFile.xml的XML文件, 并将以下数据写入其中。
步骤9:运行项目。
创建XML文件:XMLFile.xml
< ?xml version="1.0"?> < class> < student> < id> 101< /id> < firstname> Naman< /firstname> < lastname> Kumar< /lastname> < subject> Math< /subject> < marks> 83< /marks> < /student> < student> < id> 102< /id> < firstname> Kapil< /firstname> < lastname> Kumar< /lastname> < subject> Chemistry< /subject> < marks> 60< /marks> < /student> < student> < id> 103< /id> < firstname> Harsh< /firstname> < lastname> Singh< /lastname> < subject> English< /subject> < marks> 70< /marks> < /student> < student> < id> 104< /id> < firstname> Jitesh< /firstname> < lastname> Singh< /lastname> < subject> Physics< /subject> < marks> 76< /marks> < /student> < /class>

使用DOM解析器读取XML文件的示例
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; import org.w3c.dom.Element; import java.io.File; public class ReadXMLFileExample1{public static void main(String argv[]) {try {//creating a constructor of file class and parsing an XML fileFile file = new File("F:\\XMLFile.xml"); //an instance of factory that gives a document builderDocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //an instance of builder to parse the specified xml fileDocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(file); doc.getDocumentElement().normalize(); System.out.println("Root element: " + doc.getDocumentElement().getNodeName()); NodeList nodeList = doc.getElementsByTagName("student"); // nodeList is not iterable, so we are using for loopfor (int itr = 0; itr < nodeList.getLength(); itr++) {Node node = nodeList.item(itr); System.out.println("\nNode Name :" + node.getNodeName()); if (node.getNodeType() == Node.ELEMENT_NODE) {Element eElement = (Element) node; System.out.println("Student id: "+ eElement.getElementsByTagName("id").item(0).getTextContent()); System.out.println("First Name: "+ eElement.getElementsByTagName("firstname").item(0).getTextContent()); System.out.println("Last Name: "+ eElement.getElementsByTagName("lastname").item(0).getTextContent()); System.out.println("Subject: "+ eElement.getElementsByTagName("subject").item(0).getTextContent()); System.out.println("Marks: "+ eElement.getElementsByTagName("marks").item(0).getTextContent()); }}} catch (Exception e) {e.printStackTrace(); }}}

输出:
Root element: classNode Name: studentStudent id: 101First Name: NamanLast Name: KumarSubject: MathMarks: 83Node Name: studentStudent id: 102First Name: KapilLast Name: KumarSubject: ChemistryMarks: 60Node Name: studentStudent id: 103First Name: HarshLast Name: SinghSubject: EnglishMarks: 70Node Name: studentStudent id: 104First Name: JiteshLast Name: SinghSubject: PhysicsMarks: 76

让我们看另一个读取xml文件的示例。
使用DOM解析器读取XML文件的示例
下面的示例读取相同的XML文件XMLFile.xml, 并说明如何一个接一个地循环节点。它将打印节点值, 名称和属性(如果有)。

import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class ReadXMLFileExample2{public static void main(String[] args){try {File file = new File("F:\\XMLFile.xml"); DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = documentBuilder.parse(file); System.out.println("Root element: "+ document.getDocumentElement().getNodeName()); if (document.hasChildNodes()) {printNodeList(document.getChildNodes()); }} catch (Exception e){System.out.println(e.getMessage()); }}private static void printNodeList(NodeList nodeList){for (int count = 0; count < nodeList.getLength(); count++) {Node elemNode = nodeList.item(count); if (elemNode.getNodeType() == Node.ELEMENT_NODE) {// get node name and valueSystem.out.println("\nNode Name =" + elemNode.getNodeName()+ " [OPEN]"); System.out.println("Node Content =" + elemNode.getTextContent()); if (elemNode.hasAttributes()) {NamedNodeMap nodeMap = elemNode.getAttributes(); for (int i = 0; i < nodeMap.getLength(); i++) {Node node = nodeMap.item(i); System.out.println("attr name : " + node.getNodeName()); System.out.println("attr value : " + node.getNodeValue()); }}if (elemNode.hasChildNodes()) {//recursive call if the node has child nodesprintNodeList(elemNode.getChildNodes()); }System.out.println("Node Name =" + elemNode.getNodeName()+ " [CLOSE]"); }}}}

输出:
Root element: classNode Name =class [OPEN]Node Content = 101NamanKumarMaths83 102KapilKumarChemistry60 103HarshSinghEnglish70 104JiteshSinghPhysics76 Node Name =student [OPEN]Node Content =101NamanKumarMaths83 Node Name =id [OPEN]Node Content =101Node Name =id [CLOSE]Node Name =firstname [OPEN]Node Content =NamanNode Name =firstname [CLOSE]Node Name =lastname [OPEN]Node Content =KumarNode Name =lastname [CLOSE]Node Name =subject [OPEN]Node Content =MathNode Name =subject [CLOSE]Node Name =marks [OPEN]Node Content =83Node Name =marks [CLOSE]Node Name =student [CLOSE]Node Name =student [OPEN]Node Content =102KapilKumarChemistry60 Node Name =id [OPEN]Node Content =102Node Name =id [CLOSE]Node Name =firstname [OPEN]Node Content =KapilNode Name =firstname [CLOSE]Node Name =lastname [OPEN]Node Content =KumarNode Name =lastname [CLOSE]Node Name =subject [OPEN]Node Content =ChemistryNode Name =subject [CLOSE]Node Name =marks [OPEN]Node Content =60Node Name =marks [CLOSE]Node Name =student [CLOSE]Node Name =student [OPEN]Node Content =103HarshSinghEnglish70 Node Name =id [OPEN]Node Content =103Node Name =id [CLOSE]Node Name =firstname [OPEN]Node Content =HarshNode Name =firstname [CLOSE]Node Name =lastname [OPEN]Node Content =SinghNode Name =lastname [CLOSE]Node Name =subject [OPEN]Node Content =EnglishNode Name =subject [CLOSE]Node Name =marks [OPEN]Node Content =70Node Name =marks [CLOSE]Node Name =student [CLOSE]Node Name =student [OPEN]Node Content =104JiteshSinghPhysics76 Node Name =id [OPEN]Node Content =104Node Name =id [CLOSE]Node Name =firstname [OPEN]Node Content =JiteshNode Name =firstname [CLOSE]Node Name =lastname [OPEN]Node Content =SinghNode Name =lastname [CLOSE]Node Name =subject [OPEN]Node Content =PhysicsNode Name =subject [CLOSE]Node Name =marks [OPEN]Node Content =76Node Name =marks [CLOSE]Node Name =student [CLOSE]Node Name =class [CLOSE]

Java SAX解析器Java SAX解析器代表XML的简单API。 SAX解析器逐行解析XML文件。当遇到xml文件中的开始标记, 结束标记和字符数据时, 它将触发事件。 SAX解析器也称为基于事件的解析器。
SAX解析器不会将任何XML文件加载到内存中。它不会创建XML文档的任何对象表示形式。 SAX解析器使用回调函数将XML文档结构通知客户端。与DOM解析器相比, 它更快并且使用的内存更少。
SAX是XML的流接口, 这意味着XML文件从文档顶部开始, 到根元素结束为止, 按顺序解析。
使用SAX解析器读取XML文件的示例
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 ReadXMLFileExample3{public static void main(String args[]) {try {SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() {boolean id = false; boolean firstname = false; boolean lastname = false; boolean subject = false; boolean marks = false; //parser starts parsing a specific element inside the document public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {System.out.println("Start Element :" + qName); if(qName.equalsIgnoreCase("Id")){id=true; }if (qName.equalsIgnoreCase("FIRSTNAME")) {firstname = true; }if (qName.equalsIgnoreCase("LASTNAME")) {lastname = true; }if (qName.equalsIgnoreCase("SUBJECT")) {subject = true; }if (qName.equalsIgnoreCase("MARKS")) {marks = true; }}//parser ends parsing the specific element inside the documentpublic void endElement(String uri, String localName, String qName) throws SAXException {System.out.println("End Element:" + qName); }//reads the text value of the currently parsed elementpublic void characters(char ch[], int start, int length) throws SAXException {if (id) {System.out.println("ID : " + new String(ch, start, length)); id = false; }if (firstname) {System.out.println("First Name: " + new String(ch, start, length)); firstname = false; }if (lastname) {System.out.println("Last Name: " + new String(ch, start, length)); lastname = false; }if (subject){System.out.println("Subject: " + new String(ch, start, length)); subject = false; }if (marks) {System.out.println("Marks : " + new String(ch, start, length)); marks = false; }}}; saxParser.parse("F:\\XMLFile.xml", handler); } catch (Exception e) {e.printStackTrace(); }}}

【如何在Java中读取XML文件】输出:
Start Element: classStart Element: studentStart Element: idID: 101End Element: idStart Element: firstnameFirst Name: NamanEnd Element: firstnameStart Element: lastnameLast Name: KumarEnd Element: lastnameStart Element: subjectSubject: MathEnd Element: subjectStart Element: marksMarks: 83End Element: marksEnd Element: studentStart Element: studentStart Element: idID: 102End Element: idStart Element: firstnameFirst Name: KapilEnd Element: firstnameStart Element: lastnameLast Name: KumarEnd Element: lastnameStart Element: subjectSubject: ChemistryEnd Element: subjectStart Element: marksMarks: 60End Element: marksEnd Element: studentStart Element: studentStart Element: idID: 103End Element: idStart Element: firstnameFirst Name: HarshEnd Element: firstnameStart Element: lastnameLast Name: SinghEnd Element: lastnameStart Element: subjectSubject: EnglishEnd Element: subjectStart Element: marksMarks: 70End Element: marksEnd Element: studentStart Element: studentStart Element: idID: 104End Element: idStart Element: firstnameFirst Name: JiteshEnd Element: firstnameStart Element: lastnameLast Name: SinghEnd Element: lastnameStart Element: subjectSubject: PhysicsEnd Element: subjectStart Element: marksMarks: 76End Element: marksEnd Element: studentEnd Element: class

    推荐阅读