下面是一个需要解析的xml:
string stringxml=“
<?xml version="1.0" encoding="GB2312" ?>
<userlist>
<user>
<userid>test@test.com</userid>
<name>test123</name>
<departmentid>1</departmentid>
<office>CEO</office>
<mobile>13800138000</mobile>
<phone>01084291263</phone>
<fax>01084291263001</fax>
</user>
<user>
<userid>test2@test.com</userid>
<name>test333</name>
<departmentid>2</departmentid>
<office>CEO</office>
<mobile>138001380444</mobile>
<phone>0108429145</phone>
<fax>01084291263001</fax>
</user>
</userlist>
”
xml写得有点随便,就是一个需要解析的长xml
代码:
第一:先创建一个跟xml文件字段对应的javabean:
package com.zy.mailbox.bean;
public class User {
private String userid;
private String name;
private String departmentid;
private String office;
private String mobile;
private String phone;
private String fax;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartmentid() {
return departmentid;
}
public void setDepartmentid(String departmentid) {
this.departmentid = departmentid;
}
public String getOffice() {
return office;
}
public void setOffice(String office) {
this.office = office;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getFax() {
return fax;
}
public void setFax(String fax) {
this.fax = fax;
}
@Override
public String toString() {
return "User [userid=" + userid + ", name=" + name + ", departmentid=" + departmentid + ", office=" + office
+ ", mobile=" + mobile + ", phone=" + phone + ", fax=" + fax + "]";
}
}
2.<=================编写解析工具========================>
package com.zy.mailbox.utils;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;
import com.zy.mailbox.bean.User;
public class ReadXML {
public static List<User> xmlElements(String xmlDoc) {
//创建一个新的字符串
StringReader read = new StringReader(xmlDoc);
//创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
InputSource source = new InputSource(read);
//创建一个新的SAXBuilder
SAXBuilder sb = new SAXBuilder();
List<User> users = new ArrayList<>();
try {
//通过输入源构造一个Document
Document doc = sb.build(source);
//取的根元素
Element root = doc.getRootElement();
System.out.println(root.getName());//输出根元素的名称(测试)
//得到根元素所有子元素的集合
List jiedian = root.getChildren();
//获得XML中的命名空间(XML中未定义可不写)
Namespace ns = root.getNamespace();
Element et = null;
for(int i=0;i<jiedian.size();i++){
et = (Element) jiedian.get(i);//循环依次得到子元素
User user = new User();
String userid = et.getChild("userid",ns).getText();
String name = et.getChild("name",ns).getText();
String departmentid = et.getChild("departmentid",ns).getText();
String office = et.getChild("office",ns).getText();
String mobile = et.getChild("mobile",ns).getText();
String phone = et.getChild("phone",ns).getText();
String fax = et.getChild("fax",ns).getText();
user.setUserid(userid);
user.setName(name);
user.setDepartmentid(departmentid);
user.setOffice(office);
user.setMobile(mobile);
user.setPhone(phone);
user.setFax(fax);
users.add(user);
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return users;
}
}
最后运行打印:
User [userid=zhanjie@zy.com, name=张1杰, departmentid=717, office=教师, mobile=13268212733, phone=, fax=]
User [userid=zhangjixian1@zy.com, name=张洁娴, departmentid=9161, office=经理, mobile=+86-13922431120, phone=84419009-308, fax=]
User [userid=zhanjing1@zy.com, name=张静, departmentid=5104, office=校区校长, mobile=+86-13163789684, phone=, fax=]
User [userid=zhangjig@zy.com, name=张静, departmentid=5013, office=语文教师, mobile=13714430743, phone=, fax=]
User [userid=zhangjngxin@zy.com, name=张静欣, departmentid1=748, office=行政, mobile=13928888334, phone=, fax=]
User [userid=zhagjingya@zy.com, name=张静雅, departmentid=814, office=专职教师, mobile=18218880206, phone=, fax=]
User [userid=zhagjingyao@zy.com, name=张婧瑶, departmentid=9143, office=小升初英语研发主任, mobile=15975383408, phone=, fax=]
。。。。。。。。。。。。。。