dom4j组件操作xml文档

首先参考dom4j官方文档的描述:

Features

·        designed forthe Java platform with full support for the Java Collections Framework(Java 2 Collections)

·        full supportfor JAXP, TrAX,SAX,DOM,andXSLT

·        fullyintegrated XPath support for easy navigation of XML documents

·        event basedproccessing mode to support for massive documents or XML streams

·        based on Javainterfaces for flexible plug and play implementations.

·        support for XML Schema Data Type support using Kohsuke Kawaguchi'sexcellent Multi Schema Validatorlibrary

本人英语水平不济,在此不做翻译。敬请谅解!大家自己看吧。

这是dom4j开发需要的两个jar包,jaxen-1.1.1.jar.zip是针对xPath解析的,如果不涉及到xPath解析,则dom4j-1.6.1.jar一个包就够用了。


下面针对dom4j的部分重要接口或者类的API进行介绍:

1.      Document相关

(1)	// 读取xml文件,获得document对象
        SAXReader reader = new SAXReader();
        Document doc = reader.read(new File(fileName));
(2)	//解析XML形式的文本,获得document对象.
	String xml = "<books></books>";
	Document document = DocumentHelper.parseText(xml);
(3)	//创建document对象
        Document document = DocumentHelper.createDocument();
        Element rootElement = document.addElement("books");

2.      Element相关

(1)	//取得根节点
	Element rootNode= document.getRootElement();
(2)	//取得某个节点的节点名
        Element booksElement = rootElement.element("books");
(3)	//取得某个节点的值
        String text = booksElement.getText();
(4)	//取得books节点名为book的的子节点的文字值
        String bookElement = rootElement.elementText("book");
(5)	//取得books节点下名称为book的子节点并遍历
        List elements = rootElement.elements("book");
        for(Iterator ite = elements.iterator();ite.hasNext();){
	       Element ele = (Element)ite.next();
        }
(6)	//遍历某节点下所有子节点
        for(Iterator ite = rootNode.elementIterator();ite.hasNext();){
		Element element = (Element)ite.next();
	}
(7)	//向某节点添加子节点并赋值
        Element titleElement = booksElement.addElement("title");
        titleElement.addText("《百年孤独》");
(8)	//删除某节点
        booksElement.remove(titleElement);

3.      Attribute相关

(1)	//添加属性并设置属性值
        Element bookElement = booksElement.addElement("book");
        bookElement.addAttribute("show", "yes");
(2)	//设置节点的属性
        Attribute attri = booksElement.attribute("date");
        attri.setText(“2011-09-09”);
(3)	//删除属性
        boosElement.remove(attri);
(4)	//遍历属性
        Element root=document.getRootElement();      
        for(Iterator it=root.attributeIterator();it.hasNext();){   
            Attribute attribute = (Attribute) it.next();   
            String text=attribute.getText();   
        }  

4.      读写xml文档

(1)	//写入xml文档
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(fileName));
        xmlWriter.write(doc);
        xmlWriter.close();
(2)	//设置输出格式和编码格式
        Document doc = reader.read(fileName);
        OutputFormat format = OutputFormat.createPrettyPrint();
        //OutputFormat format = OutputFormat.createCompactFormat();
        format.setEncoding("GBK");
        //XMLWriter writer = new XMLWriter(new FileWriter(newFile),format);
        XMLWriter writer = new XMLWriter(System.out,format);
        writer.write(doc);
        writer.close();
        returnValue=1;

5.      字符串与xml转换

(1)	//xml转为字符串
        SAXReader reader = new SAXReader();   
        Document document = reader.read(new File("C:/dom4jTest/newTest.xml"));
        Element root=document.getRootElement();                  
        String docXmlText=document.asXML();   
        String rootXmlText=root.asXML();   
        Element memberElm=root.element("book");   
        String memberXmlText=memberElm.asXML();
(2)	//字符串转xml
        String text = "<User type=\"user\"><name type=\"PinYin\">马克思</name><age>29</age></User>";
        Document doc = DocumentHelper.parseText(text);

6.      xPath查找节点和属性

(1)	//根据xPath查找属性
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new File(fileName));
        //如果book中的属性show为yes则改为no
        List list = document.selectNodes("/books/book/@show");
        Iterator ite = list.iterator();
        while(ite.hasNext()){
                Attribute attribute = (Attribute)ite.next();
                if(attribute.getValue().equals("yes")){
		    attribute.setValue("no");
	        }else{
		    attribute.setValue("yes");
	        }
	}
(2)	//根据xPath查找节点
	//修改owner 并加入时间节点
	list = document.selectNodes("/books/owner");
	ite = list.iterator();
	while(ite.hasNext()){
		Element ownerElement = (Element)ite.next();
		ownerElement.setText("ChinaRen");
		Element dateElement = ownerElement.addElement("date");
		dateElement.setText("2011-09-05");
		dateElement.addAttribute("type", "Gregorian-calendar");	
	}

你可能感兴趣的:(dom4j组件操作xml文档)