个性化阅读
专注于IT技术分析

如何在Java中读取XML文件

本文概述

用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 file
File file = new File("F:\\XMLFile.xml");
//an instance of factory that gives a document builder
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//an instance of builder to parse the specified xml file
DocumentBuilder 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 loop
for (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: class

Node Name: student
Student id: 101
First Name: Naman
Last Name: Kumar
Subject: Math
Marks: 83

Node Name: student
Student id: 102
First Name: Kapil
Last Name: Kumar
Subject: Chemistry
Marks: 60

Node Name: student
Student id: 103
First Name: Harsh
Last Name: Singh
Subject: English
Marks: 70

Node Name: student
Student id: 104
First Name: Jitesh
Last Name: Singh
Subject: Physics
Marks: 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 value
System.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 nodes
printNodeList(elemNode.getChildNodes());
}
System.out.println("Node Name =" + elemNode.getNodeName()+ " [CLOSE]");
}
}
}
}

输出:

Root element: class

Node Name =class [OPEN]
Node Content =
	
		101
		Naman
		Kumar
		Maths
		83
	
		102
		Kapil
		Kumar
		Chemistry
		60
	
		103
		Harsh
		Singh
		English
		70
	
		104
		Jitesh
		Singh
		Physics
		76
	
Node Name =student [OPEN]
Node Content =
		101
		Naman
		Kumar
		Maths
		83
	
Node Name =id [OPEN]
Node Content =101
Node Name =id [CLOSE]

Node Name =firstname [OPEN]
Node Content =Naman
Node Name =firstname [CLOSE]

Node Name =lastname [OPEN]
Node Content =Kumar
Node Name =lastname [CLOSE]

Node Name =subject [OPEN]
Node Content =Math
Node Name =subject [CLOSE]

Node Name =marks [OPEN]
Node Content =83
Node Name =marks [CLOSE]
Node Name =student [CLOSE]

Node Name =student [OPEN]
Node Content =
		102
		Kapil
		Kumar
		Chemistry
		60
	
Node Name =id [OPEN]
Node Content =102
Node Name =id [CLOSE]

Node Name =firstname [OPEN]
Node Content =Kapil
Node Name =firstname [CLOSE]

Node Name =lastname [OPEN]
Node Content =Kumar
Node Name =lastname [CLOSE]

Node Name =subject [OPEN]
Node Content =Chemistry
Node Name =subject [CLOSE]

Node Name =marks [OPEN]
Node Content =60
Node Name =marks [CLOSE]
Node Name =student [CLOSE]

Node Name =student [OPEN]
Node Content =
		103
		Harsh
		Singh
		English
		70
	
Node Name =id [OPEN]
Node Content =103
Node Name =id [CLOSE]

Node Name =firstname [OPEN]
Node Content =Harsh
Node Name =firstname [CLOSE]

Node Name =lastname [OPEN]
Node Content =Singh
Node Name =lastname [CLOSE]

Node Name =subject [OPEN]
Node Content =English
Node Name =subject [CLOSE]

Node Name =marks [OPEN]
Node Content =70
Node Name =marks [CLOSE]
Node Name =student [CLOSE]

Node Name =student [OPEN]
Node Content =
		104
		Jitesh
		Singh
		Physics
		76
	
Node Name =id [OPEN]
Node Content =104
Node Name =id [CLOSE]

Node Name =firstname [OPEN]
Node Content =Jitesh
Node Name =firstname [CLOSE]

Node Name =lastname [OPEN]
Node Content =Singh
Node Name =lastname [CLOSE]

Node Name =subject [OPEN]
Node Content =Physics
Node Name =subject [CLOSE]

Node Name =marks [OPEN]
Node Content =76
Node 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 document
public void endElement(String uri, String localName, String qName) throws SAXException 
{
System.out.println("End Element:" + qName);
}
//reads the text value of the currently parsed element
public 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();
}
}
}

输出:

Start Element: class
Start Element: student
Start Element: id
ID: 101
End Element: id
Start Element: firstname
First Name: Naman
End Element: firstname
Start Element: lastname
Last Name: Kumar
End Element: lastname
Start Element: subject
Subject: Math
End Element: subject
Start Element: marks
Marks: 83
End Element: marks
End Element: student
Start Element: student
Start Element: id
ID: 102
End Element: id
Start Element: firstname
First Name: Kapil
End Element: firstname
Start Element: lastname
Last Name: Kumar
End Element: lastname
Start Element: subject
Subject: Chemistry
End Element: subject
Start Element: marks
Marks: 60
End Element: marks
End Element: student
Start Element: student
Start Element: id
ID: 103
End Element: id
Start Element: firstname
First Name: Harsh
End Element: firstname
Start Element: lastname
Last Name: Singh
End Element: lastname
Start Element: subject
Subject: English
End Element: subject
Start Element: marks
Marks: 70
End Element: marks
End Element: student
Start Element: student
Start Element: id
ID: 104
End Element: id
Start Element: firstname
First Name: Jitesh
End Element: firstname
Start Element: lastname
Last Name: Singh
End Element: lastname
Start Element: subject
Subject: Physics
End Element: subject
Start Element: marks
Marks: 76
End Element: marks
End Element: student
End Element: class

赞(0)
未经允许不得转载:srcmini » 如何在Java中读取XML文件

评论 抢沙发

评论前必须登录!