java—如何在现有xml文件中附加一段xml标记

ubbxdtey  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(267)

我只想将下面的xml标记附加到现有的xml中。但当我试图执行,它抛出下面的错误消息。
[致命错误]:9:16:文档中根元素后面的标记必须格式正确。线程“main”org.xml.sax.saxparseexception中出现异常;行号:9;列数:16;文档中根元素后面的标记必须格式良好。
下面的部分试图附加到现有xml中:

<test-method duration-ms="4"  finished-at="2018-08-16T21:46:55Z"  is-config="true"  test-instance-name="DummyTestcase" >
    <reporter-output>
        <line>
        </line>
    </reporter-output>
</test-method>

现有xml文件:

<?xml version="1.0" encoding="UTF-8"?>
    <testng-results skipped="0" failed="0" total="10" passed="10">
        <test-method status="FAIL" is-config="true" duration-ms="4"
            started-at="2018-08-16T21:43:38Z" finished-at="2018-08-16T21:43:38Z">
            <params>
                <param index="0">
                    <value>               
          <![CDATA[org.testng.TestRunner@31c2affc]]>
                    </value>
                </param>
            </params>
            <reporter-output>
            </reporter-output>
        </test-method> <!-- setParameter -->

        <test-method status="FAIL" is-config="true" duration-ms="5"
            started-at="2018-08-16T21:44:55Z" finished-at="2018-08-16T21:44:55Z">
            <reporter-output>
                <line>             
         <![CDATA[runSettlement Value Set :false]]>
                </line>
            </reporter-output>
        </test-method> <!-- setSettlementFlag -->

    </testng-results>

代码:

docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        is = new InputSource();
        is.setCharacterStream(new StringReader(content));
        doc = docBuilder.parse(is);
        NodeList rootElement = doc.getElementsByTagName("test-method");
        Element element=(Element)rootElement.item(0);

        StringBuilder sb=new StringBuilder();

        for(String str:dataObj)
        {
            sb.append(str);
        }

        String getContent=sb.toString();

        System.out.println(getContent);

        docBuilder1 = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        is1 = new InputSource();
        is1.setCharacterStream(new StringReader(getContent));
        doc1 = docBuilder1.parse(is1);

        Node copiedNode = (Node) doc1.importNode(element, true);

        doc1.getDocumentElement().appendChild(copiedNode);

预期产量:

<?xml version="1.0" encoding="UTF-8"?>
    <testng-results skipped="0" failed="0" total="10" passed="10">
        <test-method status="FAIL" is-config="true" duration-ms="4"
            started-at="2018-08-16T21:43:38Z" finished-at="2018-08-16T21:43:38Z">
            <params>
                <param index="0">
                    <value>              
          <![CDATA[org.testng.TestRunner@31c2affc]]>
                    </value>
                </param>
            </params>
            <reporter-output>
            </reporter-output>
        </test-method> <!-- setParameter -->
        <test-method status="FAIL" is-config="true" duration-ms="5"
            started-at="2018-08-16T21:44:55Z" finished-at="2018-08-16T21:44:55Z">
            <reporter-output>
                <line>                   
        <![CDATA[runSettlement Value Set :false]]>
                </line>
            </reporter-output>
        </test-method> <!-- setSettlementFlag -->

<!--The below lines are appended -->
         </test-method><test-method duration-ms="4"  finished-at="2018-08-16T21:46:55Z"  is-config="true"  test-instance-name="DummyTestcase" >
            <reporter-output>
                <line>
                </line>
            </reporter-output>
    </test-method>

</testng-results>

如上所示,上面的xml标记应该附加在现有xml文件的最后。
有人能分享一些想法来实现这一点吗?

ivqmmu1c

ivqmmu1c1#

举个例子:

static String EXISTING =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
        "<testng-results skipped=\"0\" failed=\"0\" total=\"10\"\n" +
        "    passed=\"10\">\n" +
        "    <test-method status=\"FAIL\" is-config=\"true\"\n" +
        "        duration-ms=\"4\" started-at=\"2018-08-16T21:43:38Z\"\n" +
        "        finished-at=\"2018-08-16T21:43:38Z\">\n" +
        "        <params>\n" +
        "            <param index=\"0\">\n" +
        "                <value>\n" +
        "          <![CDATA[org.testng.TestRunner@31c2affc]]>\n" +
        "                </value>\n" +
        "            </param>\n" +
        "        </params>\n" +
        "        <reporter-output>\n" +
        "        </reporter-output>\n" +
        "    </test-method> <!-- setParameter -->\n" +
        "\n" +
        "    <test-method status=\"FAIL\" is-config=\"true\"\n" +
        "        duration-ms=\"5\" started-at=\"2018-08-16T21:44:55Z\"\n" +
        "        finished-at=\"2018-08-16T21:44:55Z\">\n" +
        "        <reporter-output>\n" +
        "            <line>\n" +
        "         <![CDATA[runSettlement Value Set :false]]>\n" +
        "            </line>\n" +
        "        </reporter-output>\n" +
        "    </test-method> <!-- setSettlementFlag -->\n" +
        "\n" +
        "</testng-results>";

static String APPEND =
        "<test-method duration-ms=\"4\"  finished-at=\"2018-08-16T21:46:55Z\"  is-config=\"true\"  test-instance-name=\"DummyTestcase\" >\n" +
        "    <reporter-output>\n" +
        "        <line>\n" +
        "        </line>\n" +
        "    </reporter-output>\n" +
        "</test-method>";

public static void main(String[] args) throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

    Document existingDoc = builder.parse(
            new ByteArrayInputStream(EXISTING.getBytes(StandardCharsets.UTF_8)));
    Document appendDoc = builder.parse(
            new ByteArrayInputStream(APPEND.getBytes(StandardCharsets.UTF_8)));

    Node root = existingDoc.getDocumentElement();

    root.appendChild(existingDoc.importNode(appendDoc.getDocumentElement(), true));
    root.appendChild(existingDoc.createTextNode("\n"));
    print(existingDoc);
}

static void print(Document doc) throws Exception {
    TransformerFactory.newInstance()
        .newTransformer()
        .transform(new DOMSource(doc), new StreamResult(System.out));
}
uwopmtnx

uwopmtnx2#

在xml中可以看到
测试结果是根本因素。
其中,测试方法是子元素。
因此,如果您要将test method添加为子元素,请将其附加到testing reults元素,而不是附加到test method。另外,我认为您使用test方法作为根元素,并附带了这段代码

NodeList rootElement = doc.getElementsByTagName("test-method");

相关问题