XmlPullParser (Java version)
The following are common methods and instructions for XmlPullParser to parse XML files:
1. XmlPullParserFactory.newInstance():
Create an XmlPullParserFactory instance.
2. XmlPullParserFactory.setNamespaceAware(boolean):
Set whether to support XML namespace.
3. XmlPullParserFactory.setValidating(boolean):
Set whether to support DTD verification.
4. XmlPullParserFactory.newPullParser():
Create an XmlPullParser instance.
5. XmlPullParser.setInput(InputStream, String):
Set the input stream and encoding method of the XML file to be parsed.
6. XmlPullParser.getEventType():
Gets the event type of the current parser.
7. XmlPullParser.getName():
Get the name of the current node.
8. XmlPullParser.getText():
Get the text content of the current node.
9. XmlPullParser.next():
Move to next node.
10. XmlPullParser.nextTag():
Move to next label node.
11. XmlPullParser.getAttributeCount():
Get the number of attributes of the current node.
12. XmlPullParser.getAttributeName(int):
Get the attribute name of the current node at the specified position.
13. XmlPullParser.getAttributeValue(int):
Get the attribute value of the current node at the specified position.
14. XmlPullParser.getDepth():
Get the depth of the current node.
15. XmlPullParser.END_DOCUMENT:
Indicates that the end of the XML file has been reached.
16. XmlPullParser.START_TAG:
Indicates that the current node is a label node.
17. XmlPullParser.END_TAG:
Indicates that the current node is an end tag node.
Parsing order: starting tag event----> text event----> then ending tag event
For example:
<?xml version="1.0" encoding="utf-8"?>
<keys>
<board id="xml1">1
<source>android 01</source>2
<source>android 02</source>2
<source>android 03</source>2
</board>
<board id="xml2">1
<source>ios 01</source>3
<source>ios 02</source>3
<source>ios 05</source>3
</board>4
</keys>
Parsing code:
private void parseData(){
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
InputStream inputStream = getAssets().open("text.xml");
parser.setInput(inputStream,"utf-8");
int eventType = parser.getEventType();
String board = "";
while (eventType != XmlPullParser.END_DOCUMENT){
switch (eventType) {
case XmlPullParser.TEXT:
Log.d(TAG, "parseData: text " + parser.getName() + " " + parser.getText());
break;
case XmlPullParser.START_TAG:
String keys = parser.getName();
Log.d(TAG, "parseData: start -----" + parser.getName());
if (keys.equals("board")) {
board = parser.getAttributeValue(null, "id");
}
if (board.equals("xml1")) {
if (keys.equals("source")) {
// String result = parser.getText();
// Log.d(TAG, "initData: result = " + result);
// parser.next(); // 移动解析器到下一个事件,即 TEXT 事件
// String result = parser.getText();
// Log.d(TAG, "initData: result = " + result);
}
}
break;
case XmlPullParser.END_TAG:
Log.d(TAG, "initData: end -------" + parser.getName());
break;
}
eventType = parser.next();
}
} catch (IOException | XmlPullParserException e) {
throw new RuntimeException(e);
}
}
operation result:
2023-06-06 14:35:41.553 22162-22162 MainActivity com.example.third D parseData: start -----keys
2023-06-06 14:35:41.553 22162-22162 MainActivity com.example.third D parseData: text null
2023-06-06 14:35:41.553 22162-22162 MainActivity com.example.third D parseData: start -----board
2023-06-06 14:35:41.553 22162-22162 MainActivity com.example.third D parseData: text null 1
2023-06-06 14:35:41.553 22162-22162 MainActivity com.example.third D parseData: start -----source
2023-06-06 14:35:41.553 22162-22162 MainActivity com.example.third D parseData: text null android 01
2023-06-06 14:35:41.554 22162-22162 MainActivity com.example.third D initData: end -------source
2023-06-06 14:35:41.554 22162-22162 MainActivity com.example.third D parseData: text null 2
2023-06-06 14:35:41.554 22162-22162 MainActivity com.example.third D parseData: start -----source
2023-06-06 14:35:41.554 22162-22162 MainActivity com.example.third D parseData: text null android 02
2023-06-06 14:35:41.554 22162-22162 MainActivity com.example.third D initData: end -------source
2023-06-06 14:35:41.554 22162-22162 MainActivity com.example.third D parseData: text null 2
2023-06-06 14:35:41.554 22162-22162 MainActivity com.example.third D parseData: start -----source
2023-06-06 14:35:41.554 22162-22162 MainActivity com.example.third D parseData: text null android 03
2023-06-06 14:35:41.554 22162-22162 MainActivity com.example.third D initData: end -------source
2023-06-06 14:35:41.554 22162-22162 MainActivity com.example.third D parseData: text null 2
2023-06-06 14:35:41.554 22162-22162 MainActivity com.example.third D initData: end -------board
2023-06-06 14:35:41.554 22162-22162 MainActivity com.example.third D parseData: text null
2023-06-06 14:35:41.554 22162-22162 MainActivity com.example.third D parseData: start -----board
2023-06-06 14:35:41.555 22162-22162 MainActivity com.example.third D parseData: text null 1
2023-06-06 14:35:41.555 22162-22162 MainActivity com.example.third D parseData: start -----source
2023-06-06 14:35:41.555 22162-22162 MainActivity com.example.third D parseData: text null ios 01
2023-06-06 14:35:41.555 22162-22162 MainActivity com.example.third D initData: end -------source
2023-06-06 14:35:41.555 22162-22162 MainActivity com.example.third D parseData: text null 3
2023-06-06 14:35:41.555 22162-22162 MainActivity com.example.third D parseData: start -----source
2023-06-06 14:35:41.555 22162-22162 MainActivity com.example.third D parseData: text null ios 02
2023-06-06 14:35:41.555 22162-22162 MainActivity com.example.third D initData: end -------source
2023-06-06 14:35:41.555 22162-22162 MainActivity com.example.third D parseData: text null 3
2023-06-06 14:35:41.555 22162-22162 MainActivity com.example.third D parseData: start -----source
2023-06-06 14:35:41.555 22162-22162 MainActivity com.example.third D parseData: text null ios 05
2023-06-06 14:35:41.555 22162-22162 MainActivity com.example.third D initData: end -------source
2023-06-06 14:35:41.555 22162-22162 MainActivity com.example.third D parseData: text null 3
2023-06-06 14:35:41.555 22162-22162 MainActivity com.example.third D initData: end -------board
2023-06-06 14:35:41.555 22162-22162 MainActivity com.example.third D parseData: text null 4
2023-06-06 14:35:41.555 22162-22162 MainActivity com.example.third D initData: end -------keys
DocumentBuilderFactory(kotlin版)
The following are common methods and usage instructions of org.w3c.dom.Node:
1. getNodeName():
Get the name of the node.
2. getNodeValue():
Get the value of a node.
3. getNodeType():
Get the type of node and return an integer value. The specific values are as follows:
- ELEMENT_NODE(1):
Element node
- ATTRIBUTE_NODE(2):
Attribute
- TEXT_NODE(3):
node Text node
- CDATA_SECTION_NODE(4):
CDATA node
- ENTITY_REFERENCE_NODE(5):
Entity reference node
- ENTITY_NODE(6):
Entity node
- PROCESSING_INSTRUCTION_NODE(7):
Processing instruction node
- COMMENT_NODE(8):
Comment node Document
- DOCUMENT_NODE(9):
node
- DOCUMENT_TYPE_NODE(10):
Document type node Document
- DOCUMENT_FRAGMENT_NODE(11):
fragment node Symbol
- NOTATION_NODE(12):
node
4. getChildNodes():
Get the node's child node list and return a NodeList object.
-
getParentNode():
Get the parent node of the node and return a Node object. -
getAttributes():
Get the attribute list of the node and return a NamedNodeMap object. -
hasChildNodes():
Determine whether the node has child nodes and return a Boolean value. -
getTextContent():
Get the text content of the node and return a string. -
getFirstChild():
Get the first child node of the node and return a Node object. -
getLastChild():
Get the last child node of the node and return a Node object. -
getNextSibling():
Get the next sibling node of the node and return a Node object. -
getPreviousSibling():
Get the previous sibling node of the node and return a Node object.
xml used by test cases:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<!--我是注释,也能被解析出来-->
<A>Apple</A>
我是A和B中间的文本
<B>Banana</B>
<C>我是C文本
<D>Dog</D>我是D和E之间的文本
<E>elephant</E>
</C>我是C和F之间的文本
<F>foot</F>我是F和G之间的文本
<G>god</G>
</config>
Test case code:
fun main() {
try {
val factory = DocumentBuilderFactory.newInstance()
val builder = factory.newDocumentBuilder()
val file = File("D:\\DevSpace\\IDEA\\Project\\GUIProject\\src\\kotlinLearning\\config.xml")
if(file.exists()){
val stream = file.inputStream()
val document = builder.parse(stream)
val rootElement = document.documentElement
println("${
rootElement.tagName} ${
rootElement.nodeName} ${
rootElement.nodeValue}")
println("============================================================================")
val nodeList = rootElement.childNodes
for(i in 0 until nodeList.length){
val configNode : Node = nodeList.item(i)
//单独判断C,有子标签的
if(configNode.nodeName == "C"){
val element = configNode as Element
println("tagName: ${
element.tagName} 节点name: ${
element.nodeName} 节点值: ${
element.nodeValue} ")
val childNodes = element.childNodes
for (j in 0 until childNodes.length){
val child = childNodes.item(j)
if(child.nodeType == Element.ELEMENT_NODE){
val firstChild = child.firstChild
val nodeValue = firstChild.nodeValue
val nodeName = firstChild.nodeName
println("launcherInfo子节点的名为:$nodeName 值为:$nodeValue")
}
}
}else{
println("Node的类型为: ${
configNode.nodeType} Node的名字为: ${
configNode.nodeName} Node的值为: ${
configNode.nodeValue} Node的子值为: ${
configNode.firstChild?.nodeValue}")
}
}
}else{
println(file.absoluteFile)
println("文件不存在")
}
}catch (e:Exception){
e.stackTrace
}
}
operation result:
config config null
============================================================================
Node的类型为: 3 Node的名字为: #text Node的值为:
Node的子值为: null
Node的类型为: 8 Node的名字为: #comment Node的值为: 我是注释,也能被解析出来 Node的子值为: null
Node的类型为: 3 Node的名字为: #text Node的值为:
Node的子值为: null
Node的类型为: 1 Node的名字为: A Node的值为: null Node的子值为: Apple
Node的类型为: 3 Node的名字为: #text Node的值为:
我是A和B中间的文本
Node的子值为: null
Node的类型为: 1 Node的名字为: B Node的值为: null Node的子值为: Banana
Node的类型为: 3 Node的名字为: #text Node的值为:
Node的子值为: null
tagName: C 节点name: C 节点值: null
launcherInfo子节点的名为:#text 值为:Dog
launcherInfo子节点的名为:#text 值为:elephant
Node的类型为: 3 Node的名字为: #text Node的值为: 我是C和F之间的文本
Node的子值为: null
Node的类型为: 1 Node的名字为: F Node的值为: null Node的子值为: foot
Node的类型为: 3 Node的名字为: #text Node的值为: 我是F和G之间的文本
Node的子值为: null
Node的类型为: 1 Node的名字为: G Node的值为: null Node的子值为: god
Node的类型为: 3 Node的名字为: #text Node的值为:
Node的子值为: null