• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

为什么解析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

办公室(科研处)

办公室秘书岗

其他





064002dvkb065255bttkew.jpg

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

发新帖

发布任务需求已有1031167位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!