I only want to read the script tag from the given xml file.
testsuite.xml
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-
<TestSuite xsi:noNamespaceSchemaLocation="xyz.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.example.org/TestSuite">
<Version>1.0.</Version> <Description>CETAF for Mobile</Description>
<C.E.T.A.FType>testSuite</C.E.T.A.FType>
<C.E.T.A.FName>CETAF</C.E.T.A.FName> <Init/> -<TestVector> -<Test>
<Script>TC1_LocalExec</Script>
<Priority/> </Test> -<Test>
<Script>TC2_Remote</Script> <Priority/> </Test> -<Test>
<Script>TC3_DataDriven</Script> <Priority </Test> -<Test>
<Script>TC4_PreConditionCheck</Script> <Priority/> </Test> -<Test>
<Script>TC5_PreConditionFail</Script> <Priority/> </Test> -<Test>
<Script>TC6_Host</Script> <Priority/> </Test> -<Test>
<Script>TC7_Deadlock</Script> <Priority/> </Test> -<Test>
<Script>TC8_AdbTest</Script> <Priority/> </Test> -<Test>
<Script>TC9_AdbRemote</Script> <Priority/> </Test> </TestVector> </TestSuite>
My Code in Java is the following :
package xmlparse;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
public class ReadXMLFile {
public static void main(String argv[]) {
try {
File fXmlFile = new File("/Users/388033/Desktop/KeplerWorkSpace_20140102/ KeplerWorkSpace/cetaf/Engine/TestFiles/TestSuite/TestSuite.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("TestSuite");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
//System.out.println("Script : " + eElement.getAttribute("Script"));
System.out.println("Script : " + eElement.getElementsByTagName("Script").item(0).getTextContent());
System.out.println("Script : " + eElement.getElementsByTagName("Script").item(0).getTextContent());
//System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
//System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
//System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
But when I try this code, I only get the display for the 1st script. I would like to show every script, can you help me to find a way to do it.
Thanks.