jdom与dom4j作为特地为java的实现,比较灵活适合熟悉Java的人
jdom主要构建方法
SAXBuilder sax=new SAXBuilder();
方法一: InputStream in=new FileInputStream("rec\\books.xml");
Document document=sax.build(in);
方法二: Document document=sax.build("rec\\books.xml");
直接使用Element类获取节点 List接收集合
Element rootElement=document.getRootElement();
List booklist=rootElement.getChildren();
List attrlist=booktemp.getAttributes()
List childs=booktemp.getChildren();
PS:乱码不改xml文件编码类型是可用inputStreamReader包装FileInputStream更改读取的编码方式,再将reader传入sax.bulid
public classJdomtest {private static List bookslist=new ArrayList();public static voidmain(String[] args) {
SAXBuilder sax=newSAXBuilder();
InputStream in;try{//in=new FileInputStream("rec\\books.xml");//Document document=sax.build(in);
Document document=sax.build("rec\\books.xml");
Element rootElement=document.getRootElement();
List booklist=rootElement.getChildren();for(Element booktemp : booklist) {
book bookbuf=newbook();
List attrlist=booktemp.getAttributes();
System.out.println("解析第"+(booklist.indexOf(booktemp)+1)+"本书");for(Attribute attr:attrlist){
System.out.println("解析书本的第"+(attrlist.indexOf(attr)+1)+"个属性");
System.out.println(attr.getName()+":"+attr.getValue());if(attr.getName().equals("id"))
bookbuf.id=attr.getValue();
}
List childs=booktemp.getChildren();for(Element child:childs){
System.out.println(childs.indexOf(child)+1+":"+child.getName()+"----"+child.getValue());if(child.getName().equals("name"))
bookbuf.name=child.getValue();if(child.getName().equals("years"))
bookbuf.years=child.getValue();if(child.getName().equals("price"))
bookbuf.price=child.getValue();
}
bookslist.add(bookbuf);
System.out.println("第"+(booklist.indexOf(booktemp)+1)+"本书解析完毕"+"成功添加了"+bookbuf.name);
bookbuf=null;
}for(book bookq : bookslist) {
System.out.println("书名:"+bookq.name);
System.out.println("出版年份:"+bookq.years);
System.out.println("价格:"+bookq.price);
}
}catch(FileNotFoundException e) {//TODO Auto-generated catch block
e.printStackTrace();
}//TODO Auto-generated method stub
catch(JDOMException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
View Code
dom4j主要构建方法
SAXReader reader = new SAXReader();
Document document = reader.read(new File("rec/books.xml"));
其大致方法与jdom无异。
其获取子节点的方式常通过elementIterator实现得到一个Iterator
Iterator it = bookstore.elementIterator();
子节点根据不同类型有getStringValue();getIntValue()等
public classJdom4jtest {private static ArrayList booklist=new ArrayList();public static voidmain(String[] args) {//TODO Auto-generated method stub
SAXReader reader = newSAXReader();try{
Document document= reader.read(new File("rec/books.xml"));
Element bookstore=document.getRootElement();
Iterator it =bookstore.elementIterator();while(it.hasNext()) {
book tempbook=newbook();
Element elementbook=it.next();
List attributes =elementbook.attributes();
System.out.println("开始遍历属性");for(Attribute attr : attributes) {
System.out.println("第" + attributes.indexOf(attr) + 1
+ "个属性" + attr.getName() + ":"
+attr.getStringValue());
tempbook.id=attr.getStringValue();
}
Iterator itchild =elementbook.elementIterator();while(itchild.hasNext()) {
Element bookchild=itchild.next();
System.out.println(bookchild.getName()+ "---"
+bookchild.getStringValue());if(bookchild.getName().equals("name"))
tempbook.name=bookchild.getStringValue();if(bookchild.getName().equals("years"))
tempbook.years=bookchild.getStringValue();if(bookchild.getName().equals("price"))
tempbook.price=bookchild.getStringValue();
}
booklist.add(tempbook);
tempbook=null;
}
}catch(DocumentException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
printbook();
}public static voidprintbook(){for(book booktemp : booklist) {
System.out.println(booktemp.id+":"+booktemp.name);
System.out.println("年份---"+booktemp.years);
System.out.println("价格---"+booktemp.price);
}
}
}
View Code