加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 06:04:11 所属栏目:百科 来源:网络整理
导读: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 =
   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();
}
  }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读