xml文件如:
<?xml version="1.0" encoding="utf-8" ?>
<item name="aaa" id="2115">
<status>1</status>
<desc>developer</desc>
<users>
<user>
<name>zhangsan</name>
<id>456</id>
</user>
<user>
<name>lisi</name>
<id>789</id>
</user>
</users>
</item>
说明:以上xml文件格式中,开始元素无所谓,想解析那个元素都可以,重要的是结束元素的确定
user,item这两个元素都可以看成是结束元素,这两者都分别相当于是两个model类
public static void main(String[] args) throws IOException,
XMLStreamException {
StaxTest test = new StaxTest();
File file = new File("e:\\user.xml");
test.readTest(file);
}
public void readTest(File file) throws IOException, XMLStreamException {
Long time = null;
XMLInputFactory factory = XMLInputFactory.newInstance(); //建立对象
Reader fileReader = new FileReader(file); //读取文件
factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
XMLStreamReader reader = factory.createXMLStreamReader(fileReader);
com.text.Item item = null;
List<User> userList = new ArrayList<User>();
User user = null;
try {
while(reader.hasNext()){
int e = reader.next();//取节点标签是开始标签还是结束标签
//确定是开始节点元素
if(e == XMLStreamConstants.START_ELEMENT){
if("item".equalsIgnoreCase(reader.getLocalName())){
item = new com.text.Item();
item.setId(reader.getAttributeValue(null, "id")); //取节点中包含的属性值
item.setName(reader.getAttributeValue(null, "name"));
}
if("status".equalsIgnoreCase(reader.getLocalName())){
item.setStatus(reader.getElementText()); //取子节点中的节点值
}
if("desc".equalsIgnoreCase(reader.getLocalName())){
item.setDesc(reader.getElementText());
}
if("user".equalsIgnoreCase(reader.getLocalName())){ //每一个user开始标签,就new一个user对象,然后依次解析user的各个属性
user = new User();
}
if("id".equalsIgnoreCase(reader.getLocalName())){
user.setId(reader.getElementText());
}
if("name".equalsIgnoreCase(reader.getLocalName())){
user.setName(reader.getElementText());
}
}
//确定是节点结束元素
if(e == XMLStreamConstants.END_ELEMENT){
if("user".equalsIgnoreCase(reader.getLocalName())){ //每一个user结束标签就将整个user节点中包含的user对象加入到集合中去
userList.add(user);
}
if("item".equalsIgnoreCase(reader.getLocalName())){
item.setUserList(userList);
}
}
}
System.out.println(item.getId()+":"+item.getName()+":"+item.getStatus()+":"+item.getDesc());
for(User u:item.getUserList()){
System.out.println(u.getId()+":"+u.getName());
}
} catch(Exception e){
e.printStackTrace();
}finally {
reader.close();
}
}
实体类:item和user:
package com.text;
import java.util.List;
public class Item {
public String name;
public String id;
public String status;
public String desc;
public List<User> userList;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
package com.text;
public class User {
public String id;
public String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
整个解析过程就是:读取xml文件,依次读取元素标签,标签分两种,开始标记和结束标记,碰到开始标记就到开始状态的处理中进行处理,
碰到结束标记就道结束状态中处理