Tag: XML预览模式: 普通 | 列表
07-06
12

XML的读写操作样例

创建并写入XML:
package xmltest;

import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;

public class CreateXML {
    public void Create() {
        try {
            Document doc = new Document();
            ProcessingInstruction pi = new ProcessingInstruction(
                    "xml-stylesheet", "type='text/xsl' href='test.xsl'");
            doc.addContent(pi);

            Element root = new Element("user");
            doc.setRootElement(root);
            Element el1 = new Element("name");

            //Text text1 = new Text("blurxx");
            Element em = new Element("name_last").addContent("blurxx");

            el1.addContent(em);

            Element el2 = new Element("passwd").addContent("1234");

            root.addContent(el1);
            root.addContent(el2);

            //缩进四个空格,自动换行,gb2312编码

查看更多...

Tags: XML

分类:学习 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 616