Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将xml存储到xml属性中_Java_Xml - Fatal编程技术网

Java 将xml存储到xml属性中

Java 将xml存储到xml属性中,java,xml,Java,Xml,如何将xml文档存储到xml属性中?我有这段代码,但是de-output给我转义字符,这些转义字符是左向右的吗 public static void main(String[] args) throws TransformerException, ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Doc

如何将xml文档存储到xml属性中?我有这段代码,但是de-output给我转义字符,这些转义字符是左向右的吗

public static void main(String[] args) throws TransformerException,
    ParserConfigurationException  {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();
        Element organizations = doc.createElement("ORGANIZATIONS");
        doc.appendChild(organizations);
        organizations.setAttribute("xml", "<root><first>01</first><second>02</second></root>");
        DOMSource domSource = new DOMSource(doc);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
        StreamResult sr = new StreamResult(new File("test.xml"));

        transformer.transform(domSource, sr);

    }
publicstaticvoidmain(字符串[]args)抛出TransformerException,
ParserConfiguration异常{
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
Document doc=builder.newDocument();
要素组织=doc.createElement(“组织”);
儿童(组织)文件;
setAttribute(“xml”、“0102”);
DOMSource DOMSource=新的DOMSource(doc);
TransformerFactory tf=TransformerFactory.newInstance();
变压器=tf.新变压器();
setOutputProperty(OutputKeys.OMIT_XML_声明,“yes”);
transformer.setOutputProperty(OutputKeys.INDENT,“是”);
transformer.setOutputProperty(“{http://xml.apache.org/xslt}缩进金额,“2”);
setOutputProperty(OutputKeys.METHOD,“xml”);
transformer.setOutputProperty(OutputKeys.ENCODING,“ISO-8859-1”);
StreamResult sr=newstreamresult(新文件(“test.xml”);
变换器(domSource,sr);
}
输出:

<ORGANIZATIONS xml="&lt;root&gt;&lt;first&gt;01&lt;/first&gt;&lt;second&gt;02&lt;/second&gt;&lt;/root&gt;"/>

这是正确的。在属性上设置时,应转义xml。 如果要查看纯xml,必须将其放在元素上,并强制对其使用CDATA节:

transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "thequalifiednameoftheelement");

CDATA节不能用于属性。。。属性不是用来填充xml文档的…

转义字符又怎么了?它可以工作。但是,如果您可以控制XML格式,我强烈建议您避免这种事情。它不可读,几乎没有用处。@SamIam我原以为:@user2566397会使xml无效。在解析xml时,最好只是取消对其进行扫描。