微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

解析xml简单实例,后续继续补充.......

   SAXReader reader = new SAXReader();
       Document document=null;
try {
document = reader.read(new File(System.getProperty("user.dir") + CONfigPATH));
} catch (DocumentException e) {
e.printstacktrace();

} 

//得到xml文件里面的第一个节点

       Element root = document.getRootElement();

//得到第一个节点下的所有子节点1

       List<Element> tasksChildren = root.elements();

//遍历出所有子节点1

for (Iterator<?> it = taskChildren.iterator(); it.hasNext();) {

Element tasksChild = (Element) it.next();

tasks.add(tasksChild);
TaskVo taskVo = new TaskVo();
// 得到task的全部属性
taskVo.setId(tasksChild.attributeValue("id"));
taskVo.setName(tasksChild.attributeValue("name"));
taskVo.setDescription(tasksChild
.attributeValue("description"));
taskVo.setEnable(Boolean.parseBoolean(tasksChild
.attributeValue("enable")));
taskChild.elementText("hostId")

}

for (Element tast : tasksChildren) {
try {  
//获取节点里面的属性值  tast.attributeValue("id")得到<xxx  id="yyyyy" />里面的id值
Document documentTast = reader.read((System.getProperty("user.dir") +  AppCf.config_path+"/crawler/"+tast.attributeValue("id")+".xml"));
Element roottast = documentTast.getRootElement();
String taskId = tast.attributeValue("id");
dealLabel.dealTask(roottast,taskId);
} catch (DocumentException e) {
e.printstacktrace();
}
  }

原文地址:https://www.jb51.cc/xml/295936.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。