相关疑难解决方法(0)

XML文档到字符串

获取XML Document(org.w3c.dom.Document)的String表示形式的最简单方法是什么?这就是所有节点都在一条线上.

举个例子,来自

<root>
  <a>trge</a>
  <b>156</b>
</root>
Run Code Online (Sandbox Code Playgroud)

(这只是一个树形表示,在我的代码中它是一个org.w3c.dom.Document对象,所以我不能把它当作String)

"<root> <a>trge</a> <b>156</b> </root>"
Run Code Online (Sandbox Code Playgroud)

谢谢!

java xml string xmldocument dom

100
推荐指数
1
解决办法
16万
查看次数

如何在序列化之前从DOM中删除仅空白文本节点?

我有一些Java(5.0)代码从各种(缓存)数据源构造DOM,然后删除不需要的某些元素节点,然后使用以下方法将结果序列化为XML字符串:

// Serialize DOM back into a string
Writer out = new StringWriter();
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "no");
tf.transform(new DOMSource(doc), new StreamResult(out));
return out.toString();
Run Code Online (Sandbox Code Playgroud)

但是,由于我要删除多个元素节点,最终在最终的序列化文档中会有很多额外的空格.

是否有一种简单的方法可以在将序列化为字符串之前(或同时)从DOM中删除/折叠无关的空格?

java xml whitespace dom

18
推荐指数
2
解决办法
4万
查看次数

缩进使用Transformer制作的XML

我正在尝试从Java创建XML并且在缩进时遇到问题.在以下代码中,您可以看到OutputKeys.INDENT设置为yes...

        //set up a transformer
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");

        //create string from xml tree
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(doc);
        trans.transform(source, result);
        String xmlString = sw.toString();

        //print xml
        System.out.println(xmlString);
Run Code Online (Sandbox Code Playgroud)

但它似乎没有影响,输出是:

<dataset id="1"><br>
<path></path><br>
<session id="1"><br>
<method><br>
<timestamp>a timestamp</timestamp><br>
<signiture><br>
<classPath></classPath><br>
<name>methodName</name><br>
<declarationType>String</declarationType><br>
<parameters><br>
<parameter>String</parameter><br>
<parameter>int</parameter><br>
</parameters><br>
</signiture><br>
<arguments><br>
<argument>SomeValue</argument><br>
<argument>AnotherValue</argument><br>
</arguments><br>
<return>ReturnValue</return><br>
</method><br>
</session><br>
</dataset><br>
Run Code Online (Sandbox Code Playgroud)

java xml formatting transformer-model

4
推荐指数
1
解决办法
3765
查看次数

Java XML输出 - 子项的正确缩进

我想将一些简单的数据模型序列化为xml,我一直在使用标准的java.org.w3c相关代码(见下文),缩进比没有"OutputKeys.INDENT"好,但还有一点点剩下的东西 - 适合儿童元素的缩进.

我知道之前已经在stackoverflow上询问过,但是这个配置对我来说不起作用,这是我正在使用的代码:

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();

        doc = addItemsToDocument(doc);
        // The addItemsToDocument method adds childElements to the document.

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", new Integer(4));
        // switching to setAttribute("indent-number", 4); doesn't help

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

         DOMSource source = new DOMSource(doc);
       StreamResult result = new StreamResult(outFile);
        // outFile is a regular File outFile = new File("some/path/foo.xml");

        transformer.transform(source, result);
Run Code Online (Sandbox Code Playgroud)

产生的输出是:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<stuffcontainer> …
Run Code Online (Sandbox Code Playgroud)

java xml indentation

2
推荐指数
1
解决办法
2万
查看次数

奇怪的XML缩进

我正在写一个XML文件,标签出现了一些错误:

<BusinessEvents>

<MailEvent>
          <to>Wellington</to>
          <weight>10.0</weight>
          <priority>air priority</priority>
          <volume>10.0</volume>
          <from>Christchurch</from>
          <day>Mon May 20 14:30:08 NZST 2013</day>
          <PPW>8.0</PPW>
          <PPV>2.5</PPV>
     </MailEvent>
<DiscontinueEvent>
          <to>Wellington</to>
          <priority>air priority</priority>
          <company>Kiwi Co</company>
          <from>Sydney</from>
     </DiscontinueEvent>
<RoutePriceUpdateEvent>
          <weightcost>3.0</weightcost>
          <to>Wellington</to>
          <duration>15.0</duration>
          <maxweight>40.0</maxweight>
          <maxvolume>20.0</maxvolume>
          <priority>air priority</priority>
          <company>Kiwi Co</company>
          <day>Mon May 20 14:30:08 NZST 2013</day>
          <frequency>3.0</frequency>
          <from>Wellington</from>
          <volumecost>2.0</volumecost>
     </RoutePriceUpdateEvent>
<CustomerPriceUpdateEvent>
          <weightcost>3.0</weightcost>
          <to>Wellington</to>
          <priority>air priority</priority>
          <from>Sydney</from>
          <volumecost>2.0</volumecost>
     </CustomerPriceUpdateEvent>
</BusinessEvents>
Run Code Online (Sandbox Code Playgroud)

如您所见,第一个子节点根本没有缩进,但节点子节点缩进两次?然后close标签只缩进一次?

我怀疑它可能与将根不添加到文档中有关doc.appendChild(root),但是当我这样做时,我得到一个错误

"试图插入一个不允许的节点."

这是我的解析器:

DocumentBuilderFactory icFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder icBuilder;
        try {
            icBuilder = icFactory.newDocumentBuilder();
            String businessEventsFile = System.getProperty("user.dir") + "/testdata/businessevents/businessevents.xml";
            Document doc …
Run Code Online (Sandbox Code Playgroud)

java xml dom transformer-model

2
推荐指数
1
解决办法
2643
查看次数