XML用JAVA解析例子
- 提问者网友:饥饿走向夜
- 2021-02-17 09:52
- 五星知识达人网友:三千妖杀
- 2021-02-17 11:12
students>
这是XML
引入DOM4J的包
程序
public void iterateWholeXML(String filename,HashMap
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(new File(filename));
Element root = document.getRootElement();
// 用于记录学生编号的变量
int num=-1;
//遍历根结点(students)的所有孩子节点(肯定是student节点)
for ( Iterator iter = root.elementIterator(); iter.hasNext(); ) {
Element element = (Element) iter.next();
num++;
// 获取person节点的age属性的值
Attribute ageAttr=element.attribute("age");
if(ageAttr!=null){
String age = ageAttr.getValue();
if (age != null&&!age.equals("")) {
hm.put(element.getName() + "-"+ageAttr.getName()+ num, age);
} else {
hm.put(element.getName() + "-" +ageAttr.getName()+ num, "20");
}
}else{
hm.put(element.getName() + "-age"+ num, "20");
}
//遍历student结点的所有孩子节点(即name,college,telphone,notes),并进行处理
for ( Iterator iterInner = element.elementIterator(); iterInner.hasNext(); ) {
Element elementInner = (Element) iterInner.next();
if(elementInner.getName().equals("college")){
hm.put(elementInner.getName()+ num, elementInner.getText());
//获取college节点的leader属性的值
Attribute leaderAttr=elementInner.attribute("leader");
if(leaderAttr!=null){
String leader = leaderAttr.getValue();
if (leader != null&&!leader.equals("")) {
hm.put(elementInner.getName() + "-"+leaderAttr.getName()+ num, leader);
} else {
hm.put(elementInner.getName() + "-" +leaderAttr.getName()+ num, "leader");
}
}else {
hm.put(elementInner.getName() + "-leader"+ num, "leader");
}
}else{
hm.put(elementInner.getName()+ num, elementInner.getText());
}
}
} } catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
- 1楼网友:老鼠爱大米
- 2021-02-17 12:21
public void iteratewholexml(string filename,hashmap<string,string> hm){ saxreader saxreader = new saxreader(); try { document document = saxreader.read(new file(filename)); element root = document.getrootelement(); // 用于记录学生编号的变量 int num=-1; //遍历根结点(students)的所有孩子节点(肯定是student节点) for ( iterator iter = root.elementiterator(); iter.hasnext(); ) { element element = (element) iter.next(); num++; // 获取person节点的age属性的值 attribute ageattr=element.attribute("age"); if(ageattr!=null){ string age = ageattr.getvalue(); if (age != null&&!age.equals("")) { hm.put(element.getname() + "-"+ageattr.getname()+ num, age); } else { hm.put(element.getname() + "-" +ageattr.getname()+ num, "20"); } }else{ hm.put(element.getname() + "-age"+ num, "20"); } //遍历student结点的所有孩子节点(即name,college,telphone,notes),并进行处理 for ( iterator iterinner = element.elementiterator(); iterinner.hasnext(); ) { element elementinner = (element) iterinner.next(); if(elementinner.getname().equals("college")){ hm.put(elementinner.getname()+ num, elementinner.gettext()); //获取college节点的leader属性的值 attribute leaderattr=elementinner.attribute("leader"); if(leaderattr!=null){ string leader = leaderattr.getvalue(); if (leader != null&&!leader.equals("")) { hm.put(elementinner.getname() + "-"+leaderattr.getname()+ num, leader); } else { hm.put(elementinner.getname() + "-" +leaderattr.getname()+ num, "leader"); } }else { hm.put(elementinner.getname() + "-leader"+ num, "leader"); } }else{ hm.put(elementinner.getname()+ num, elementinner.gettext()); } } }
} catch (documentexception e) { // todo auto-generated catch block e.printstacktrace(); } }