您的位置:首页 > 其它

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

2015-10-30 11:40 447 查看
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();
}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: