技术开发 频道

学习dom4j开源XML处理框架

  【IT168 文档】API帮助文档:http://www.dom4j.org/dom4j-1.6.1/apidocs/index.html

  处理DEMO整理:

  DEMO一:使用API生成XML文档

package xml.utils;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Document;
public class Foo {
    
public Document createDocument() {
        Document document
= DocumentHelper.createDocument();
        Element root
= document.addElement("root");
        Element author1
= root.addElement("author")
        .addAttribute(
"name","job")
        .addAttribute(
"location", "US")
        .addText(
"HKEBAO");
        
return document;
    }
    
public static void main(String[] args) {
        Document document
= new Foo().createDocument();
        System.out.println(document.asXML());
    }
}

   输出的结果:

<?xml version="1.0" encoding="UTF-8"?>
<root><author name="job" location="US">HKEBAO</author></root>

  直接通过内置的API输出一段XML出来!

  DEMO2 :通过使用API进行读取XML里面的数据。

  原理:将XML解析到内存然后生成一棵DOCUMENT树!

  解析的方法有多种

    /**
     *
@param File
     *
@return Document
     *
@throws DocumentException
     *
*/
    
public Document parse(File aFile) throws DocumentException {
        SAXReader xmlReader
= new SAXReader();
        
return xmlReader.read(aFile);
    }

   注意:JAVA里面的File路径问题需要做一个专题讲一下!!!

public static void main(String[] args) throws MalformedURLException, DocumentException {
        File file
= new File("dom4j.xml");      
        Document document
= new Foo().parse(file);
        
new Foo().bar(document);
        System.out.println(document.asXML());
    }

   我们可以通过这样的方法来读出来一个XML文档。读出来了之后就可以方便进行解析了!

  DEMO3

public void modifyDocumet(File inputxml) {
        
try {
            SAXReader saxReader
= new SAXReader();
            Document document
= saxReader.read(inputxml);
            List list
= document.selectNodes("//article/@level");//使用了XPath语法
            Iterator iter = list.iterator();
            
while (iter.hasNext()) {
                Attribute attribute
= (Attribute) iter.next();
                
if (attribute.getValue().equals("Intermediate")) {
                    attribute.setValue(
"Introductory");
                }
            }
            list
= document.selectNodes("//article/@date" );
            iter
= list.iterator();
            
while (iter.hasNext()) {
                Attribute attribute
= (Attribute) iter.next();
                
if(attribute.getValue().equals("December-2001"))
                    attribute.setValue(
"2002");
            }
            list
= document.selectNodes("//article" );
            iter
=list.iterator();
            
while(iter.hasNext()){
                Element element
=(Element)iter.next();
                Iterator iterator
=element.elementIterator("title");
                
while(iterator.hasNext()){
                    Element titleElement
=(Element)iterator.next();
                    
if(titleElement.getText().equals("Java configuration with XMLSchema"))
                        titleElement.setText(
"Create flexible and extensible XML schema");
                }
            }
        }
catch (Exception e) {
            
// TODO: handle exception
        }
0
相关文章