首页 > 代码库 > XML--Java中的四种常见解析方式--jdom与dom4j

XML--Java中的四种常见解析方式--jdom与dom4j

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<Element> booklist=rootElement.getChildren();

List<Attribute> attrlist=booktemp.getAttributes()

List<Element> childs=booktemp.getChildren();

PS:乱码不改xml文件编码类型是可用inputStreamReader包装 FileInputStream更改读取的编码方式,再将reader传入sax.bulid

技术分享
public class Jdomtest {
private static List<book> bookslist=new ArrayList<book>();
    public static void main(String[] args) {
        SAXBuilder sax=new SAXBuilder();
        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<Element> booklist=rootElement.getChildren();
        for (Element booktemp : booklist) {
            book bookbuf=new book();
            List<Attribute> 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<Element> 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<Element> it = bookstore.elementIterator();

子节点根据不同类型有getStringValue();getIntValue()等

技术分享
public class Jdom4jtest {
private static ArrayList<book> booklist=new ArrayList<book>();
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SAXReader reader = new SAXReader();
        try {
            Document document = reader.read(new File("rec/books.xml"));
            Element bookstore = document.getRootElement();
            Iterator<Element> it = bookstore.elementIterator();
            while (it.hasNext()) {
                book tempbook =new book();
                Element elementbook = it.next();
                List<Attribute> 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<Element> 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 void printbook(){
    for (book booktemp : booklist) {
        System.out.println(booktemp.id+":"+booktemp.name);
        System.out.println("年份---"+booktemp.years);
        System.out.println("价格---"+booktemp.price);
        
    }
    
}
}
View Code

 

XML--Java中的四种常见解析方式--jdom与dom4j