如何在Java中将无效数据写入xml文件

子部

有什么方法可以将以下数据写入Java中的xml。请帮我

<tag>  
   <abc="1"/>  
   <cde="a"/>  
   <xyz="3"/>  
</tag>  
奥利·普尤拉(Olli Puljula)

举个例子...

import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileOutputStream;

public class Foobar {

    public static void main(String[] args) throws Exception {
        /* one way */
        FileOutputStream fileOutputStream = new FileOutputStream(new File("/tmp/foobar.txt"));
        String data = "<tag>  \n" +
                "   <abc=\"1\"/>  \n" +
                "   <cde=\"a\"/>  \n" +
                "   <xyz=\"3\"/>  \n" +
                "</tag>  ";

        fileOutputStream.write(data.getBytes());

        /* another way */
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        Element tag = doc.createElement("tag");
        Attr abc = doc.createAttribute("abc");
        Attr cde = doc.createAttribute("cde");
        Attr xyz = doc.createAttribute("xyz");
        abc.setValue("1");
        cde.setValue("2");
        xyz.setValue("3");
        CDATASection cdataSection = doc.createCDATASection(makeInvalidTag(abc) + makeInvalidTag(cde) + makeInvalidTag(xyz));
        tag.appendChild(cdataSection);
        doc.appendChild(tag);
        StreamResult streamResult = new StreamResult(new File("/tmp/foobar2.txt"));
        DOMSource domSource = new DOMSource(doc);
        TransformerFactory.newInstance().newTransformer().transform(domSource, streamResult);
    }

    private static String makeInvalidTag(Attr attr) {
        return "\u003C" + attr.toString() + "/\u003E";
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章