JAVA 读写XML(二) 使用(JDOM)
程序员文章站
2022-05-30 12:29:27
...
使用java jdom读取xml文件
public void readXml(){
SAXBuilder builder=new SAXBuilder();
try {
Document doc = builder.build("e:\\wind.xml");
Element root=doc.getRootElement();
List<Element> listActors=root.getChildren("Actress");
for (Element elem : listActors) {
if(elem.getAttributes().size()>0){
List<Attribute> attr=elem.getAttributes();
System.out.println("编号:"+attr.get(0).getValue());
System.out.println("姓名:"+elem.getChildText("name"));
System.out.println("性别:"+elem.getChildText("sex"));
System.out.println("第一职业:"+elem.getChildText("occupation"));
System.out.println("第二职业:"+attr.get(1).getValue());
}else{
System.out.println("编号:"+elem.getChildText("id"));
System.out.println("姓名:"+elem.getChildText("name"));
System.out.println("性别:"+elem.getChildText("sex"));
System.out.println("职业:"+elem.getChildText("occupation"));
}
}
listActors.clear();
listActors=root.getChildren("Actor");
//System.out.println("list(count):"+listActors.size());
for(Element elemOut:listActors){
List<Element> elmIn=elemOut.getChildren("actor");
for(Element elem:elmIn){
List<Attribute> attr=elem.getAttributes();
if(attr.size()>1){
System.out.println("编号:"+attr.get(0).getValue());
System.out.println("姓名:"+elem.getChildText("name"));
System.out.println("第一职业:"+elem.getChildText("occupation"));
System.out.println("第二职业:"+attr.get(1).getValue());
}else{
System.out.println("编号:"+attr.get(0).getValue());
System.out.println("姓名:"+elem.getChildText("name"));
System.out.println("职业:"+elem.getChildText("occupation"));
}
}
}
} catch (JDOMException ex) {
Logger.getLogger(ReadXml.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ReadXml.class.getName()).log(Level.SEVERE, null, ex);
}
}
上一篇: TensorFlow(三)常用函数
下一篇: Python Mysql:增删改查