Question

public static void main(String[] args) throws Exception {

    ArrayList<StepAttributesDisplay> attrList = new ArrayList<StepAttributesDisplay>();

    //Get the DOM Builder Factory
    DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();

    //Get the DOM Builder
    DocumentBuilder builder = factory.newDocumentBuilder();

    //Load and Parse the XML document
    //document contains the complete XML as a Tree.
    Document document =
            builder.parse(
                    ClassLoader.getSystemResourceAsStream("xml/input.xml"));
    // for now lets keep one object

    //Iterating through the nodes and extracting the data.
    NodeList nodeList = document.getDocumentElement().getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node instanceof Element) {
            StepAttributesDisplay attributesDisplay = new StepAttributesDisplay();
            NodeList childNodes = node.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node cNode = childNodes.item(j);
                if (cNode instanceof Element) {
                    NodeList childNodes2 = cNode.getChildNodes();
                    for (int h = 0; h < childNodes2.getLength(); h++) {
                        Node dNode = childNodes2.item(h);
                        // System.out.println(cNode.getNodeName());

                        if (dNode.getNodeName().equals("function")) {
                            System.out.println(dNode.getLastChild().getNodeValue().trim());
                            attrList.add(attributesDisplay);
                            for (int x = 0; x < attrList.size(); x++) {

                                System.out.println("Function New " + attrList.get(x).getFunction());

                            }//attrList.add(attributesDisplay);

                        }
                    }
                }
            }
        }
    }
}

output that i get is :

0
Alloc
Alloc
Function New Alloc
Format
Format
Function New Format
Function New Format
Format Check
Format Check
Function New Format Check
Function New Format Check
Function New Format Check
Delete
Delete
Function New Delete
Function New Delete
Function New Delete
Function New Delete
4
0
FunctionDelete
1
FunctionDelete
2
FunctionDelete
3
FunctionDelete

Ideally it should be:

FunctionAlloc
FunctionFormat
FunctionFormat Check
FunctionDelete

No correct solution

OTHER TIPS

I think that XPath is a better tool for your task than your nested looping through a three deep nested loop.

I can't tell exactly from your code logic but I am guessing that an Xpath expression like ("//function/text()") would cleanly extract all of the function node values regardless of their place in the hierarchy.

For XPath basics in Java, See How to read XML using XPath in Javastrong text

Your inner loop appears to be where the problem is happening. Are you missing a condition check, you appear to be printing out every

if (dNode.getNodeName().equals("function")) {
    System.out.println(dNode.getLastChild().getNodeValue().trim());
    attrList.add(attributesDisplay);
    //==>> this loop below appears to print out all of the addributes every time  it is called
    for (int x = 0; x < attrList.size(); x++) {
      System.out.println("Function New "+ attrList.get(x).getFunction());
}// attrList.add(attributesDisplay);
}

Thank you guys, I was creating the object 'attributesDisplay' at wrong place. this is the modified code and its working :)

      NodeList nodeList = document.getDocumentElement().getChildNodes();

        for (int i = 0; i < nodeList.getLength(); i++) {
           Node node = nodeList.item(i);
           if (node instanceof Element) {


                  NodeList childNodes = node.getChildNodes();

                  for (int j = 0; j < childNodes.getLength(); j++) {
                      Node cNode = childNodes.item(j);
                      if (cNode instanceof Element) {
                         attributesDisplay= new StepAttributesDisplay();

                          NodeList childNodes2 = cNode.getChildNodes();
                          for (int h = 0; h < childNodes2.getLength(); h++) {
                                Node dNode = childNodes2.item(h);
                               // System.out.println(cNode.getNodeName());

                                if(dNode instanceof Element){
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top