dom4j与jdom练习

一、dom4j使用

1.到官网http://www.dom4j.org/下载最新的dom4j;

2.将dom4j加入到我们的项目中:

dom4j与jdom练习_第1张图片

测试例子:

View Code
 

import java.io.FileOutputStream;
import java.io.FileWriter;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class Test1
{
    public static void main(String[] args) throws Exception
    {
        // 创建文档并设置文档的根元素节点 :第一种方式
        // Document document = DocumentHelper.createDocument();
        //
        // Element root = DocumentHelper.createElement("student");
        //
        // document.setRootElement(root);

        // 创建文档并设置文档的根元素节点 :第二种方式
        Element root = DocumentHelper.createElement("student");
        Document document = DocumentHelper.createDocument(root);

        root.addAttribute("name", "zhangsan");

        Element helloElement = root.addElement("hello");
        Element worldElement = root.addElement("world");

        helloElement.setText("hello");
        worldElement.setText("world");

        helloElement.addAttribute("age", "20");

        XMLWriter xmlWriter = new XMLWriter();
        xmlWriter.write(document);
        
        OutputFormat format = new OutputFormat("    ", true);
        
        XMLWriter xmlWriter2 = new XMLWriter(new FileOutputStream("student2.xml"), format);
        xmlWriter2.write(document);
        
        XMLWriter xmlWriter3 = new XMLWriter(new FileWriter("student3.xml"), format);
        
        xmlWriter3.write(document);
        xmlWriter3.close();

    }
}

效果:程序生成两个文件:

dom4j与jdom练习_第2张图片

 读取xml:

View Code
 

import java.io.File;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.DOMReader;
import org.dom4j.io.SAXReader;

public class Test2
{
    public static void main(String[] args) throws Exception
    {
        SAXReader saxReader = new SAXReader();
        
        Document doc = saxReader.read(new File("student2.xml"));
        
        Element root = doc.getRootElement();
        
        System.out.println("root element: " + root.getName());
        
        List childList = root.elements();
        
        System.out.println(childList.size());
        
        List childList2 = root.elements("hello");
        
        System.out.println(childList2.size());
        
        Element first = root.element("hello");
        
        System.out.println(first.attributeValue("age"));
        
        for(Iterator iter = root.elementIterator(); iter.hasNext();)
        {
            Element e = (Element)iter.next();
            
            System.out.println(e.attributeValue("age"));
        }
        
        System.out.println("---------------------------");
        
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        org.w3c.dom.Document document = db.parse(new File("student2.xml"));
        
        DOMReader domReader = new DOMReader();
        
        //将JAXP的Document转换为dom4j的Document
        Document d = domReader.read(document);
        
        Element rootElement = d.getRootElement();
        
        System.out.println(rootElement.getName());
        
        
        
        
        
        
        
        
        
    }
}

效果:

附: 园内有个很好的讲解:http://www.blogjava.net/i369/articles/154264.html

 二、jdom使用

1)、引入jdom-2.0.4.jar

2)、

测试类1:

View Code
import java.io.FileWriter;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class Test3
{
    public static void main(String[] args) throws Exception
    {
        Document document = new Document();

        Element root = new Element("联系人列表").setAttribute(new Attribute("公司",
                "A集团"));

        document.addContent(root);
        
        Element contactPerson = new Element("联系人");
        
        root.addContent(contactPerson);

        contactPerson
                .addContent(new Element("姓名").setText("张三"))
                .addContent(new Element("公司").setText("A公司"))
                .addContent(new Element("电话").setText("021-55556666"))
                .addContent(
                        new Element("地址")
                                .addContent(new Element("街道").setText("5街"))
                                .addContent(new Element("城市").setText("上海"))
                                .addContent(new Element("省份").setText("上海市")));

        XMLOutputter output = new XMLOutputter(Format.getPrettyFormat()
                .setIndent("    ").setEncoding("gbk"));

        output.output(document, new FileWriter("contact.xml"));

    }
}

测试类2:

View Code
 

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class JDomTest2
{
    public static void main(String[] args) throws Exception
    {
        SAXBuilder builder = new SAXBuilder();
        
        Document doc = builder.build(new File("jdom.xml"));
        
        Element element = doc.getRootElement();
        
        System.out.println(element.getName());
        
        Element hello = element.getChild("hello");
        
        System.out.println(hello.getText());
        
        List list = hello.getAttributes();
        
        for(int i = 0 ;i < list.size(); i++)
        {
            Attribute attr = (Attribute)list.get(i);
            
            String attrName = attr.getName();
            String attrValue = attr.getValue();
            
            System.out.println(attrName + "=" + attrValue);
        }
        
        hello.removeChild("world");
        
        XMLOutputter out = new XMLOutputter(Format.getPrettyFormat().setIndent("    "));
        
        
        out.output(doc, new FileOutputStream("jdom2.xml"));        
        
    }
}

测试类3:

View Code
 

import java.io.FileWriter;

import org.jdom2.Attribute;
import org.jdom2.Comment;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;



public class JDomTest1
{
    public static void main(String[] args) throws Exception
    {
        Document document = new Document();

        Element root = new Element("root");

        document.addContent(root);

        Comment comment = new Comment("This is my comments");

        root.addContent(comment);

        Element e = new Element("hello");

        e.setAttribute("sohu", "www.sohu.com");

        root.addContent(e);

        Element e2 = new Element("world");

        Attribute attr = new Attribute("test", "hehe");

        e2.setAttribute(attr);

        e.addContent(e2);

        e2.addContent(new Element("aaa").setAttribute("a", "b")
                .setAttribute("x", "y").setAttribute("gg", "hh").setText("text content"));

        
        Format format = Format.getPrettyFormat();
        
        format.setIndent("    ");
//        format.setEncoding("gbk");
        
        XMLOutputter out = new XMLOutputter(format);

        out.output(document, new FileWriter("jdom.xml"));
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        

    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(dom4j)