java xml null_为什么解析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 getPeopleListFromXml(Document document){

List peopleList=new ArrayList();

Element root=document.getRootElement();

Iterator 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"));

IteratorpeopleAttr=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();

ListpeopleList=getPeopleListFromXml(document);

System.out.println(peopleList);

}

}

xml:

王五

王五/华夏/ZJCZ

办公室(科研处)

办公室秘书岗

其他

2ac6cfa57c92182e6043090a25282d09.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值