• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extracting XML file values randomly using Java

 
Siva Kum
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Good morning. I am very new to Java/Junit (I was an Automation tester earlier), I need a help on the below scenario.

my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<dataFile>
<category1>Images/categorieslist.png</category1>
<cat1>Images/cat1.png</cat1>
<cat2>Images/cat2.png</cat2>
<cat3>Images/cat2.png</cat3>

<category1.1>Images/cat1.1sub.png</category1.1>
<category1.2>Images/cat1.2sub.png</category1.2>
<category1.3>Images/cat1.3sub.png</category1.3>
<category1.4>Images/cat1.4sub.png</category1.4>
<category1.5>Images/cat1.5sub.png</category1.5>
<category1.6>Images/cat1.6sub.png</category1.6>
<category1.7>Images/cat1.7sub.png</category1.7>
<category1.8>Images/cat1.8sub.png</category1.8>
<category1.9>Images/cat1.9sub.png</category1.9>
<category1.10>Images/cat1.10sub.png</category1.10>

</maincateg>
</dataFile>

I need to click on any item randomly and I need to click on any item for a particular category

Scenario:
Categorieslist page has to be clicked
In that page, there will be around 10 sub categories. I need to click on them randomly using Java
I need to validate that I have clicked particular image

when I clicked on categorieslist.png on the 1st screen, I will see the 2nd screen, I need to click on it randomly, it should open the products page.

currently I am storing the data in an xml file and through the below Java code, we are calling this function

public String loadXML1(String sTagname, String sAttribute) {
try{

// DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// DocumentBuilder builder = factory.newDocumentBuilder();
// doc = builder.parse(new File("CommonProperties.xml"));

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("CommonProperties.xml"));

//
// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());

//
doc.getDocumentElement ().normalize ();

// System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());

Element rootElem = doc.getDocumentElement();

NodeList list = rootElem.getElementsByTagName(sTagname);
System.out.println ("Node list length is " + list.getLength());
int i;
for (i = 0; i < list.getLength(); i++) {
// i = (int)(Math.random()*10);

// Node firstPersonNode = list.item(i);
Node firstPersonNode = list.item(i);
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){

Element firstPersonElement = (Element)firstPersonNode;

NodeList category1List = firstPersonElement.getElementsByTagName(sAttribute);
Element category1Element = (Element)category1List.item(0);

NodeList textFNList = category1Element.getChildNodes();

dataValue2 = ((Node)textFNList.item(0)).getNodeValue().trim();

System.out.println("The value of your data is : " +dataValue2);
}
}
}

catch(Exception ex){
System.out.println(ex);
}
return dataValue2;

}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic