Enclosed is my xml and code i have written.How can i test that if "MX" and "D3" is present in same container and if yes it should return True and if 'MX' is present in one container and 'D3' in another it should return false. I I am able to validate only one value at a time but i am not able to validate if both 'MX' and 'D3' are coming from same container
<?xml version="1.0" encoding="utf-8"?>
<IVG>
-
<row rowNumber="0">
<value columnNumber="0">TEST</value>
<value columnNumber="1">3030</value>
<value columnNumber="2">MX</value>
<value columnNumber="3">D1</value>
<value columnNumber="4">2014-09-08 22:06:58.0</value>
<value columnNumber="5">3</value>
<value columnNumber="6">45</value>
<value columnNumber="7">2</value>
<value columnNumber="8">2014-09-08 22:10:53.753</value>
<value columnNumber="9">24</value>
</row>
-
<row rowNumber="1">
<value columnNumber="0">TEST</value>
<value columnNumber="1">3030</value>
<value columnNumber="2">MX</value>
<value columnNumber="3">D3</value>
<value columnNumber="4">2014-09-08 22:06:58.0</value>
<value columnNumber="5">0</value>
<value columnNumber="6">45</value>
<value columnNumber="7">2</value>
<value columnNumber="8">2014-09-08 22:10:53.753</value>
<value columnNumber="9">25</value>
</row>
-
<row rowNumber="2">
<value columnNumber="0">TEST</value>
<value columnNumber="1">3030</value>
<value columnNumber="2">MX</value>
<value columnNumber="3">D</value>
<value columnNumber="4">2014-09-08 22:06:58.0</value>
<value columnNumber="5">0</value>
<value columnNumber="6">45</value>
<value columnNumber="7">2</value>
<value columnNumber="8">2014-09-08 22:10:53.753</value>
<value columnNumber="9">26</value>
</row>
-
<row rowNumber="3">
<value columnNumber="0">TEST</value>
<value columnNumber="1">3030</value>
<value columnNumber="2">UI</value>
<value columnNumber="3">D3</value>
<value columnNumber="4">2014-09-08 22:06:58.0</value>
<value columnNumber="5">0</value>
<value columnNumber="6">45</value>
<value columnNumber="7">2</value>
<value columnNumber="8">2014-09-08 22:10:53.753</value>
<value columnNumber="9">27</value>
</row>
</IVG>
Code:
package parsingxml;
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 TestXml {
public static void main(String argv[]) {
try {
File fXmlFile = new File(
"C:\\workspace\\DataValidation\\src\\datafiles\\NewFile.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :"
+ doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("row");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
//System.out.println("testing-----"+eElement.getElementsByTagName("value").item(2).getTextContent().contains("MX"));
if(eElement.getElementsByTagName("value").item(2).getTextContent().contains("MX")); //Need code{
System.out.println("MX is present true");
break;
}
else {
System.out.println("print false");
}
}
}catch (Exception e) {
e.printStackTrace();
}
}}
No comments:
Post a Comment