i am facing a problem. After parsing an xml file the output is not actually what i want. To parse an xml file, i wrote a code like this:
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();
}
}
}
my xml file like this:
<!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>
after running my code, the output is:
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
i want the output like this:
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
where i have to change my code??