JDOM
JDOM
http://jdom.orgt
JDOM使用JAVA语言读,写操作XML的新的API
JDOM弥补DOM和SAX
类驱动
JDOM直接了当
input读
output写
方法链的编程风格。
例:
package com.shengsiyuan.jdom;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jdom.Attribute;
import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class JDomTest1 {
public static void main(String[] args) throws Exception, IOException
{
Document document =new Document();
Element root =new Element("root");
document.addContent(root);
Comment comment =new Comment("This is my comment");
root.addContent(comment);
Element e=new Element("hello");
root.addContent(e);
e.setAttribute("sohu","www.soshu.com");
Element e2 =new Element("world");
Attribute attr=new Attribute("test","hh");
e2.setAttribute(attr);
e.addContent(e2);
e2.addContent(new Element("aaa").setAttribute("cc","dd").setAttribute("gg","hh").setText("ggggggggggggggggggggggggg"));
//设置格式:format
Format format=Format.getPrettyFormat();
XMLOutputter out=new XMLOutputter(format);
out.output(document,new FileOutputStream("jdom.xml"));
}
}
结果:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<!--This is my comment-->
<hello sohu="www.soshu.com">
<world test="hh">
<aaa cc="dd" gg="hh">ggggggggggggggggggggggggg</aaa>
</world>
</hello>
</root>
更多推荐



所有评论(0)