Question

I've been trying for hours to append three NodeLists and failing. Can you please help me?

I've made following code:

NodeList nList = root.getElementsByTagName("res");
NodeList nList2 = root.getElementsByTagName("agr");
NodeList nList3 = root.getElementsByTagName("rag");

NodeList allNodes = appendNodeLists(nList, nList2, nList3);

and the appendNodeLists method:

   public static NodeList appendNodeLists(NodeList a, NodeList b, NodeList c)
   {
     NodeList nList;
       int aSize = a.getLength();
       int bSize = b.getLength();
       int cSize = c.getLength();
       if(aSize>0)
       {
           for (int i = 0; i < aSize; i++) 
               nList.item(i) = a.item(i);  //Error in this line
       }
       if(bSize>0)
       {
           for (int i = 0; i < bSize; i++) 
               nList.item(aSize+1+i) = b.item(i); //Error in this line                    
       }
       if(cSize>0)
       {
           for (int i = 0; i < bSize; i++)
               nList.item(aSize+bSize+1+i) = c.item(i);  //Error in this line            
       }

       return nList;
   }

What am I doing wrong? I can't append the 3 NodeLists and I can't seem to figure out why.

Btw, I have the same error in 3 lines: "Unexpected type - required variable - found value". Why is this happening?

Was it helpful?

Solution

One issue is that:

nList.item(i)

gets a value where as it looks like you are trying to set a value to it. You can not assign anything to nList.item(i). The NodeList interface itself does not have operations for appending new items into the list. You might want to look at the underlying type of NodeList and see if you can add items to that. Also, you are not constructing the nList variable. NodeList is an interface. What were you going to create that as?

If you don't want that as a NodeList, you could very easily do it as a regular List<Node> instead. Something like this:

   List<Node> nodes = new ArrayList<Node>();
   int aSize = a.getLength();
   int bSize = b.getLength();
   int cSize = c.getLength();
   if(aSize>0)
   {
       for (int i = 0; i < aSize; i++) 
           nodes.add(a.item(i));
   }
   ...

OTHER TIPS

If you really want to use NodeList, you can do this:

nList.item(i).setNodeValue(a.item(i).getNodeValue());

The better way to do it is just with a List as cmbaxter suggested, though.

List<Node> nodes = new ArrayList<Node>();

Since NodeList doesn't expose any methods to mutate its state, you can implement your own version to concatenate the lists:

  public static NodeList join(final NodeList... lists) {
    class JoinedNodeList implements NodeList {
      @Override
      public int getLength() {
        int len = 0;
        for (NodeList list : lists) {
          len += list.getLength();
        }
        return len;
      }

      @Override
      public Node item(int index) {
        for (NodeList list : lists) {
          if (list.getLength() > index) {
            return list.item(index);
          } else {
            index -= list.getLength();
          }
        }
        return null;
      }
    }

    return new JoinedNodeList();
  }

NodeList by itself is pretty basic and is used just to iterate over the Nodes. To create a new NodeList use an Element after passing the root Document object to your method as follows.

public static NodeList appendNodeLists(Document root,
                                       NodeList a, NodeList b, NodeList c)
{
    Element allNodes = root.createElement("allNodes");

    for (int i = 0; i < a.getLength(); i++)
        allNodes.appendChild(a.item(i));

    for (int i = 0; i < b.getLength(); i++) 
        allNodes.appendChild(b.item(i));

    for (int i = 0; i < c.getLength(); i++)
        allNodes.appendChild(c.item(i));

    return allNodes.getChildNodes();
}

I just did this - note that as you append the child, you're actually moving it from one element to another, so the index number stays the same.

public void appendNodeList(Element TargetElement, NodeList nl) {

    int cnt = nl.getLength();

    for (int i=0; i<cnt; i++) {
        TargetElement.appendChild(nl.item(0)); 
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top