How to get date from xml file using java-selenumn
First create XML file using File - New -File (Give the file name-example.xml)
We can store all id, name, xpath and css
<?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>
<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>
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 XMLFileReader
{ public static void main(String[] args)
{ try {
File inputfile = new File(System.getProperty("user.dir")+"\\example.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();
}
}
}
OutPut is :-
Root element :OR
##################################
object name : pg1obj1
propertytype : id
propertyvalue: //*[@id='pg1obj1']
--------------------------------------
object name : pg1obj2
propertytype : classname
propertyvalue: //*[@classname='pg1obj2']
-------------------------------------
object name : pg2obj1
propertytype : xpath
propertyvalue: //*[@xpath='pg2obj1']
--------------------------------------
object name : pg2obj2
propertytype : linktext
propertyvalue: //*[@linktext='pg2obj2']
--------------------------------------
Donate:
Please
Donate the some money (anything do you want) for my
blog
if you beneficial for this, I will provide more real example for the
latest
technique for whom who wants to make a carrier in IT field or solved
some
problem, My Name is – RITESH KUMAR SINGH A/C number- 913010044116345 AXIS
Bank
LTD :- Vaishali NCR, India-001
No comments:
Post a Comment