0

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??

2 Answers 2

2

change:

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

to

NodeList tcname = eElement.getElementsByTagName("include"); 
Sign up to request clarification or add additional context in comments.

Comments

0

You have two nested for loops. In the outer you loop through all elements test of which there are two. in the inner you loop over all elements include of which there are six. But in both cases you retrieve the node list from the same root document node. So you iterate through your two test nodes and handle all includes in the document in each iteration.

You can do two different things:

  1. You do not need the outer loop, just retrieve the NodeList for include and iterate over it
  2. In the inner loop retrieve the NodeList from the current test node.

Based on your intention one or the other approach might be better.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.