解析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();
}
  }


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章