我只想将下面的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文件的最后。
有人能分享一些想法来实现这一点吗?
2条答案
按热度按时间ivqmmu1c1#
举个例子:
uwopmtnx2#
在xml中可以看到
测试结果是根本因素。
其中,测试方法是子元素。
因此,如果您要将test method添加为子元素,请将其附加到testing reults元素,而不是附加到test method。另外,我认为您使用test方法作为根元素,并附带了这段代码