解决Jdom生成xml文件时的特殊字符问题

2022-09-29 09:52:20 浏览数 (3)

代码语言:txt复制
import org.jdom.CDATA;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

    

public static String toXML(String path) throws IOException {

        Element root = new Element("ADI");
        // 将根节点添加到文档中;
        Document Doc = new Document(root);

        CDATA text = null;
        // 创建节点
        Element elements = new Element("Objects");
       
        Element title = new Element("title");
        title.setAttribute("Name", "title");
        text = new CDATA("");
        text.setText("125722130016");
        title.setContent(text);
        object.addContent(title);

        Element vodfile = new Element("vodfile");
        vodfile.setAttribute("Name", "vodfile");
        vodfile.setText("<![CDATA[北125722130016.mp4]]>");
        object.addContent(vodfile);

        root.addContent(elements);
        // 使xml文件 缩进效果
        Format format = Format.getPrettyFormat();
        XMLOutputter XMLOut = new XMLOutputter(format);
        XMLOut.output(Doc, new FileOutputStream(path));
        return path;
    }

jdom引入:

compile 'org.jdom:jdom:1.1.3'

xml

0 人点赞