java 遍历xml文件
时间: 2023-10-22 08:10:07 浏览: 72
Java递归遍历XML所有元素
4星 · 用户满意度95%
在 Java 中,可以使用 DOM(文档对象模型)或 SAX(简单 API for XML)来遍历 XML 文件。DOM 是一种将整个 XML 文档加载到内存中的方法,而 SAX 是一种基于事件驱动的方法,它会在遍历 XML 文件时触发不同的事件。
以下是使用 DOM 和 SAX 遍历 XML 文件的示例代码:
使用 DOM:
```java
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class XMLReader {
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("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("student");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Student id : " + eElement.getAttribute("id"));
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("Age : " + eElement.getElementsByTagName("age").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
使用 SAX:
```java
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
public class XMLReader {
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 bfname = false;
boolean blname = false;
boolean bage = false;
boolean bid = false;
public void startElement(String uri, String localName, String qName, Attributes attributes) {
if (qName.equalsIgnoreCase("STUDENT")) {
String id = attributes.getValue("id");
System.out.println("Student id : " + id);
bid = true;
} else if (qName.equalsIgnoreCase("FIRSTNAME")) {
bfname = true;
} else if (qName.equalsIgnoreCase("LASTNAME")) {
blname = true;
} else if (qName.equalsIgnoreCase("AGE")) {
bage = true;
}
}
public void endElement(String uri, String localName, String qName) {
}
public void characters(char ch[], int start, int length) {
if (bfname) {
System.out.println("First Name : " + new String(ch, start, length));
bfname = false;
} else if (blname) {
System.out.println("Last Name : " + new String(ch, start, length));
blname = false;
} else if (bage) {
System.out.println("Age : " + new String(ch, start, length));
bage = false;
}
}
};
saxParser.parse(inputFile, handler);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
以上代码可以读取以下格式的 XML 文件:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<CLASS>
<STUDENT id="1">
<FIRSTNAME>John</FIRSTNAME>
<LASTNAME>Doe</LASTNAME>
<AGE>23</AGE>
</STUDENT>
<STUDENT id="2">
<FIRSTNAME>Jane</FIRSTNAME>
<LASTNAME>Smith</LASTNAME>
<AGE>25</AGE>
</STUDENT>
</CLASS>
```
希望能对你有所帮助!
阅读全文