How to Read Value from XML document and store it in a variable using Selenium Java -
i'm new automation using selenium java.
i have problem need go through xml file find node , read value in node. , need compare value input string.
can please me how read xml file , fetch value xml , store in variable.
this have in xml :
in image 01 need read value in chassismoduleoptionrequest partner_item=
, store value in array.
below code tried.
documentbuilderfactory factory = documentbuilderfactory.newinstance(); documentbuilder builder = factory.newdocumentbuilder(); //parsing of xml done here document doc = builder.parse(new file("c:\\users\\satish_d1\\documents\\my received files\\pdsl_abm.xml")); //here root element of xml , print out doc.getdocumentelement().normalize(); system.out.println ("root element of doc " + doc.getdocumentelement().getnodename()); nodelist list = doc.getelementsbytagname("chassismoduleoptionrequest"); int totalpartneritem =list.getlength(); system.out.println("total no of partner item : " + totalpartneritem); //traversing elements list , printing out data (int = 0; < list.getlength(); i++) { //getting 1 node list. node childnode = list.item(i); system.out.println("partner item : " + childnode.gettextcontent()); }
we can use java inbuilt libraries create regex pattern , search in string(data in file). below code might give idea.
public static void main(string[] args) throws filenotfoundexception { //change path file string content = new scanner(new file("/home/santhoshkumar/desktop/sample.xml")).usedelimiter("\\z").next(); //system.out.println(content); //arraylist store values arraylist<string> values = new arraylist<>(); //change pattern. pattern pattern = pattern.compile("<cchassismoduleoptionrequest partner_item=\"(.*)\">"); matcher matcher = pattern.matcher(content); while (matcher.find()) { system.out.println(matcher.group(1)); values.add(matcher.group(1)); } }
hope helps. thanks.
Comments
Post a Comment