OR.xml is below:
<?xml version="1.0" encoding="UTF-8"?>
<OR>
<Page name="page1">
<Object name = 'pg1obj1'>
<propertytype>id </propertytype>
<propertyvalue>//*[@id='pg1obj1'] </propertyvalue>
</Object>
<Object name = 'pg1obj2'>
<propertytype>classname </propertytype>
<propertyvalue>//*[@classname='pg1obj2'] </propertyvalue>
</Object>
</Page>
<Page name="page2">
<Object name = 'pg2obj1'>
<propertytype>xpath </propertytype>
<propertyvalue>//*[@xpath='pg2obj1']</propertyvalue>
</Object>
<Object name = 'pg2obj2'>
<propertytype>linktext </propertytype>
<propertyvalue>//*[@linktext='pg2obj2'] </propertyvalue>
</Object>
</Page>
</OR>
Java Program to read or.xml file
package programs;
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 ORxml {
public static void main(String arg[]) {
try {
File inputfile = new File(System.getProperty("user.dir")
+ "\\OR.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docbuilder = dbFactory.newDocumentBuilder();
Document doc = docbuilder.parse(inputfile);
doc.getDocumentElement().normalize();
System.out.println("Root element :"
+ doc.getDocumentElement().getNodeName());
NodeList nodelist = doc.getElementsByTagName("Object");
System.out.println("##################################");
for (int tmp = 0; tmp < nodelist.getLength(); tmp++) {
Node nNode = nodelist.item(tmp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("object name : "
+ eElement.getAttribute("name"));
System.out.println("propertytype : "
+ eElement.getElementsByTagName("propertytype")
.item(0).getTextContent());
System.out.println("propertyvalue: "
+ eElement.getElementsByTagName("propertyvalue")
.item(0).getTextContent());
System.out.println("--------------------------------------");
}
}
} catch (Exception e) {
e.getMessage();
e.printStackTrace();
}
}
}
Back to selenium interview questions and Answers
Back to Java interview questions and Answers