为了账号安全,请及时绑定邮箱和手机立即绑定

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


https://img1.sycdn.imooc.com//5b6a4f1200016d6813280074.jpg

正在回答

0 回答

举报

0/150
提交
取消
Java眼中的XML---文件读取
  • 参与学习       83200    人
  • 解答问题       431    个

通过Java认识并且创造XML文件,如何应用 Java“解析 XML

进入课程

为什么解析xml输出全是null??

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信