【笔记】Java读取XML文件

前言

Java读取XML文件

通过w3c包

<file>:xml文件的路径、文件对象、URI地址
<name>:标签名
<index>:集合中子元素的下标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(<file>);

// 通过标签名获取所有同名标签的节点集合
NodeList elements = document.getElementByTagName("<name>");

// 获取节点集合中的一个节点
Node element = elements.item(<index>);

// 通过节点获取所有属性的集合
NamedNodeMap attrs = element.getAttributes();
// 获取属性集合的长度
attrs.getLength();
// 获取属性集合中的一个属性
Node attr = attrs.item(<index>);
// 获取属性名
String attrName = attr.getNodeName();
// 获取属性值
String attrValue = attr.getNodeValue();

// 通过节点获取所有子节点的集合
NodeList childNodes = element.getChildNodes();
// 获取子节点集合的长度
childNodes.getLength();
// 获取子节点集合中的一个子节点
Node node = childNodes.item(<index>);

// 判断节点是不是标签
if (node.getNodeType() == Node.ELEMENT_NODE) {
// 获取节点名
node.getNodeName();
// 获取节点值
node.getFirstChild().getNodeValue();
}

通过xml包

  • 基于SAX(Simple API for XML)

创建一个解析起处理器类

  • 创建一个自定义解析器处理器类SAXParserHandler,继承DefaultHandler类,用于处理XML数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
public class SAXParserHandler extends DefaultHandler {

// 用于存放标签内的值
private String value = null;

// 开始解析文档
@Override
public void startDocument() throws SAXException {
super.startDocument();

System.out.println("开始解析文档");
}

/**
* 开始解析标签
* @param uri
* @param localName
* @param qName 标签名
* @param attributes 标签属性
* @throws SAXException
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);

// 获取属性长度
int end = attributes.getLength();
if (end > 0) {
for (int i = 0; i < end; i++) {
// 获取属性名
System.out.println(attributes.getQName(i));
// 获取属性值
System.out.println(attributes.getValue(i));
}
}

}

/**
* 解析标签内数据
* @param ch
* @param start
* @param length
* @throws SAXException
*/
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);

value = new String(ch, start, length);
}

/**
* 结束解析标签
* @param uri
* @param localName
* @param qName 标签名
* @throws SAXException
*/
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);

// 获取指定标签的值
if (qName.equals("username")) {
System.out.println(value);
}

// 清空
value = null;
}

// 结束解析文档
@Override
public void endDocument() throws SAXException {
super.endDocument();

System.out.println("结束解析文档");
}
}

创建一个测试类

<src>:XML文件的路径

1
2
3
4
5
6
7
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();

// 利用自定义解析器处理器类创建对象
SAXParserHandler handler = new SAXParserHandler();

parser.parse("<src>", handler);

完成