XMLStreamReader读取xml文件

新建xml文件(users.xml):




海淀


张三
zhangsan



朝阳


李四
lisi



1 4
1 4
1 4
海淀
1 4 2 4
2 4
张三 1 4 2 4
zhangsan 1 4 2 4
2 4
1 4
1 4
朝阳


李四
lisi 1 4 2 4
2 4
2 8
- - >

XMLStreamReader读取xml文件:
/**
** XMLStreamConstants.START_ELEMENT = 1;
** XMLStreamConstants.CHARACTERS =4;
* XMLStreamConstants.END_ELEMENT = 2;
**/
XMLInputFactory factory = XMLInputFactory.newFactory();
InputStream stream = XmlInputFactory.class.getClassLoader()
.getResourceAsStream("webService/xml/users.xml");
XMLStreamReader reader = factory.createXMLStreamReader(stream);
while (reader.hasNext()) {
int type = reader.next();
if (type == XMLStreamConstants.START_ELEMENT) { // 开始节点
String name = reader.getName().toString();
if (name.equals("user")) {
System.out.print(reader.getAttributeValue(0) + ":");
}
if (name.equals("nickname")) {
System.out.println(reader.getElementText());
}
}
}

你可能感兴趣的:(XMLStreamReader读取xml文件)