Lorant Krausz
Lorant Krausz

Reputation: 21

How to get node name in the XML file using Java?

I want to ask second level(node) name for example failure, condNotMeet. I have an xml file, like this:

<testcase classname="name1" name="description1" time="19.274">
   <failure type="toBeMsg" message="error1" capture="_201601021755993.png">
   </failure>
</testcase>

<testcase classname="name2" name="description2" time="19.274">
   <failure type="toBeMsg" message="error2" capture="_20132143993.png">
   </failure>
</testcase>

<testcase classname="name3" name="description3" time="19.274">
   <condNotMeet type="toBeMsg" message="error3" capture="_20160123412.png">
   </condNotMeet>
</testcase>

but, how to ask for "testcase"-s subnode name, for exemple: failure, condNotMeet...

I have this code to start:

try {
            File fXmlFile = new File("././full_1_tests.xml"); 
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            org.w3c.dom.Document doc = dBuilder.parse(fXmlFile);

            doc.getDocumentElement().normalize();

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

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

                //System.out.println("\nCurrent Element :" + nNode.getNodeName() + " Node.ELEMENT_NODE::"+  Node.ELEMENT_NODE);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    System.out.println("Test description: " + eElement.getAttribute("name") + " ");

                    // here it is the problem, how to get firstChildNode name???????? ...
                    System.out.println("|| Status: "+ nNode.getNodeName() ???? ); // Status: failure, condNotMee t

                }

            }

        } catch (Exception e) {
            System.out.println("Error : " + e);
        }

Upvotes: 2

Views: 6545

Answers (2)

Musaddique S
Musaddique S

Reputation: 1567

If you want to get second level node directly suppose you want condNotMeet

so for that you can use below code:

 XPath xPath = (XPath) XPathFactory.newInstance().newXPath();


 String condNotMeetExpression = "//testcase/condNotMeet";
 Node node = (Node) xPath.compile(condNotMeetExpression).evaluate(doc,
    XPathConstants.NODE);

here you will get condNotMeet node. also same for other node also.

String failure Expression = "//testcase/failure";
Node node = (Node) xPath.compile(failureExpression).evaluate(doc,
        XPathConstants.NODE);

                   OR

if you want list of all failure node then you can use below code:

String failure Expression = "//testcase/failure";
NodeList list= (NodeList)xPath.compile(failureExpression).evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < list.getLength(); i++) {
    Node node = list.item(i);
    //further code foe getting attributes
}

Upvotes: 1

wero
wero

Reputation: 33010

Once you got the element node for a testcase element:

Element eElement = (Element) nNode;
System.out.println("Test description: " + eElement.getAttribute("name") + " ");

you can obtain the children of that element and iterate over this list:

NodeList children = eElement.getChildNodes();
for (...

or you can obtain the first child element (if that is the only one that interests you):

Element child = (Element)eElement.getFirstChild();

(this should succeed since you normalized the document).

Upvotes: 1

Related Questions