簡體   English   中英

如何在現有XML文件中附加一個XML標簽

[英]How to append a piece of XML tag inside existing XML file

我只想將以下XML標簽添加到現有XML中。 但是,當我嘗試執行該操作時,它將引發以下錯誤消息。

[致命錯誤]:9:16:根元素后的文檔中的標記必須格式正確。 線程“主”中的異常org.xml.sax.SAXParseException; lineNumber:9; columnNumber: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文件的最后,如上所示。

誰能分享一些想法來實現這一目標?

在XML中可見

  1. 測試結果是rootElement。
  2. 在那里面,測試方法是子元素。

因此,如果要將test-method作為子元素添加,請將其附加到testing-reults元素,而不是將其附加到test-method 我也認為您將測試方法作為附加了此代碼的根元素

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

這是一個例子:

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));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM