gpt4 book ai didi

Java xml解析解决方案

转载 作者:行者123 更新时间:2023-12-02 04:20:18 26 4
gpt4 key购买 nike

我遇到了一个问题。解析 xml 文件后,输出实际上并不是我想要的。为了解析 xml 文件,我编写了如下代码:

public static void main(String argv[]) throws IOException {
int suiteid = 0;
String scmoid = null;
int getsuiteid = -34343;

FileWriter fstream = new FileWriter("G:/filewriter.txt",false);
BufferedWriter out = new BufferedWriter(fstream);

try {

File fXmlFile = new File("NewFile.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

doc.getDocumentElement().normalize();

NodeList nList = doc.getElementsByTagName("test");

for (int temp = 0; temp < nList.getLength(); temp++) {

Node nNode = nList.item(temp);


if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;
String testcasename = eElement.getAttribute("name");


//testcase name of test suite.


NodeList tcname = doc.getElementsByTagName("include");

for(int temp2 =0; temp2 < tcname.getLength();temp2++){

Node nNode1 = tcname.item(temp2);

if (nNode1.getNodeType() == Node.ELEMENT_NODE) {

Element eElement1 = (Element) nNode1;

String testcasename1 = eElement1.getAttribute("name");
out.write( testcasename);
out.write( "_");
out.write( testcasename1);
out.newLine();

}
}

}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (out != null) {
out.close();
}
}
}

我的 xml 文件是这样的:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="test" verbose="1" preserve-order="true">
<test name="testone_7654">
<classes>
<class name="test.Signin_0001_Test">
<methods>
<include name="test_2610_Oneaddone" />
<include name="test_2611_Oneaddtwo" />
<include name="test_1677_Oneaddthree" />
</methods>
</class>
</classes>
</test>

<test name="testtwo_8764">
<classes>
<class name="com.scrollmotion.workcloud.Signin_0001_Test">
<methods>
<include name="test_2810_TwoOne" />
<include name="test_2181_TwoTwo" />
<include name="test_1877_TwoThree" />
</methods>
</class>
</classes>
</test>

</suite>

运行我的代码后,输出是:

testone_7654_test_2610_Oneaddone
testone_7654_test_2611_Oneaddtwo
testone_7654_test_1677_Oneaddthree
testone_7654_test_2810_TwoOne
testone_7654_test_2181_TwoTwo
testone_7654_test_1877_TwoThree
testtwo_8764_test_2610_Oneaddone
testtwo_8764_test_2611_Oneaddtwo
testtwo_8764_test_1677_Oneaddthree
testtwo_8764_test_2810_TwoOne
testtwo_8764_test_2181_TwoTwo
testtwo_8764_test_1877_TwoThree

我想要这样的输出:

testone_7654_test_2610_Oneaddone
testone_7654_test_2611_Oneaddtwo
testone_7654_test_1677_Oneaddthree
testtwo_8764_test_2810_TwoOne
testtwo_8764_test_2181_TwoTwo
testtwo_8764_test_1877_TwoThree

我必须在哪里更改我的代码?

最佳答案

更改:

NodeList tcname = doc.getElementsByTagName("include");

NodeList tcname = eElement.getElementsByTagName("include");

关于Java xml解析解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32846753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com