vendredi 31 juillet 2015

XML parsing in a function and return data in an array list - java [on hold]

I need to read an xml in a method and use that data to drive selenium webdriver script. I am able to read the xml data in to a Arraylist and print the contents in the console. I am also returning ArrayList in the method. I am just struggling to use that returned data in my selenium method for different browser actions(type, click etc). This is my read method which is main method of my class

public static ArrayList main(String[] args) {

    ArrayList<String> testData = null;
    try {
        testData = new ArrayList<>();

        File fXmlFile = new File("C://Javaseleniumworld/Book.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        //optional, but recommended
        //read this - http://ift.tt/IJO9S6
        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName("staff");

        System.out.println("----------------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);

            System.out.println("\nCurrent Element :" + nNode.getNodeName());

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                System.out.println("firstname : " + eElement.getAttribute("id"));
                System.out.println("server : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
                String FirstName = eElement.getElementsByTagName("firstname").item(0).getTextContent();
                testData.add(FirstName);
                System.out.println("server : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
                String LastName = eElement.getElementsByTagName("lastname").item(0).getTextContent();
                testData.add(LastName);
                System.out.println("Array List" + testData);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return testData;
}

Aucun commentaire:

Enregistrer un commentaire