为什么解析xml输出全是null??
代码:
Dom4JReaderUtils类
package com.xml;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
public class Dom4JReaderUtils {
private static final String RESOURCE;
static{
RESOURCE=Dom4JReaderUtils.class.getClassLoader().getResource("peopleList.xml").getPath();
}
public static Document getDocument(){
Document document=null;
SAXReader reader=new SAXReader();
try{
document=reader.read(new File(RESOURCE));
}catch(DocumentException e){
e.printStackTrace();
}
return document;
}
}
people实体类
package com.xml;
public class People {
private String position;
private String unid;
private String noteid;
private String sibings;
private String name;
private String FullName;
private String dept;
private String role;
private String duty;
private String wkgp;
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getUnid() {
return unid;
}
public void setUnid(String unid) {
this.unid = unid;
}
public String getNoteid() {
return noteid;
}
public void setNoteid(String noteid) {
this.noteid = noteid;
}
public String getSibings() {
return sibings;
}
public void setSibings(String sibings) {
this.sibings = sibings;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFullName() {
return FullName;
}
public void setFullName(String fullName) {
FullName = fullName;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getDuty() {
return duty;
}
public void setDuty(String duty) {
this.duty = duty;
}
public String getWkgp() {
return wkgp;
}
public void setWkgp(String wkgp) {
this.wkgp = wkgp;
}
@Override
public String toString(){
return "people[position="+position+",unid="+unid+",noteid="+noteid+",sibings="+sibings+
",name="+name+",FullName="+FullName+",dept="+dept+",role="+role+",duty="+duty+"]"+"\n";
}
}
测试类
package com.xml;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Test {
public static List<People> getPeopleListFromXml(Document document){
List<People> peopleList=new ArrayList<People>();
Element root=document.getRootElement();
Iterator<Element> peopleIterator=root.elementIterator();
while(peopleIterator.hasNext()){
Element element=peopleIterator.next();
People people=new People();
people.setName(element.elementText("name"));
people.setFullName(element.elementText("FullName"));
people.setDept(element.elementText("dept"));
people.setRole(element.elementText("role"));
people.setDuty(element.elementText("duty"));
people.setUnid(element.elementText("unid"));
Iterator<Attribute>peopleAttr=element.attributeIterator();
while(peopleAttr.hasNext()){
Attribute attribute=peopleAttr.next();
String attributeName=attribute.getName();
// String attributeName1=attribute.getName();
if(attributeName.equals("position")){
people.setPosition(attribute.getValue());
}else{
people.setUnid(attribute.getValue());
}
}
peopleList.add(people);
}
System.out.println(root.getName());
return peopleList;
}
/**
* @param args
*/
public static void main(String[] args) {
Document document=Dom4JReaderUtils.getDocument();
List<People>peopleList=getPeopleListFromXml(document);
System.out.println(peopleList);
}
}
xml:
<viewentry position="2" unid="A" noteid="3" siblings="14">
<entrydata columnnumber="1" name="name">
<text>王五</text></entrydata>
<entrydata columnnumber="2" name="FullName">
<text>王五/华夏/ZJCZ</text></entrydata>
<entrydata columnnumber="3" name="dept">
<text>办公室(科研处)</text></entrydata>
<entrydata columnnumber="4" name="role">
<text>办公室秘书岗</text></entrydata>
<entrydata columnnumber="5" name="duty">
<text>其他</text></entrydata>
<entrydata columnnumber="6" name="wkgp">
<text></text></entrydata>
</viewentry>