Inclusion of list <Node>
I decided to implement Abstract List<Node> . here is part of it:
import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class myNodeList implements NodeList{ Node root = null; int length = 0; public myNodeList() {} public void addNode(Node node) { if(root == null) { root = node; } else root.appendChild(node); length++; System.out.println("this is the added node " +node); } } but when I try to add a node, it gives me the following exception:
Exception in thread "main" org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted. at com.sun.org.apache.xerces.internal.dom.NodeImpl.insertBefore(NodeImpl.java:478) at com.sun.org.apache.xerces.internal.dom.NodeImpl.appendChild(NodeImpl.java:235) at pageparsertest.myNodeList.addNode(myNodeList.java:27) this is because of Node root = null; which allows you to add a node to a null node? then how to fix
You cannot add to com.sun.org.apache.xerces.internal.dom.NodeImpl , you will need to use com.sun.org.apache.xerces.internal.dom.ParentNode .
appendChild will call insertBefore , which throws only Exception for NodeImpl
Move one or more node (s) to our list of children. Note that this implicitly removes them from the previous parent.
By default, we do not accept any children; ParentNode overrides this one .
See how Axis implemented them: http://grepcode.com/file/repo1.maven.org/maven2/com.ning/metrics.collector/1.0.2/org/apache/axis/message/NodeListImpl.java
It seems you are trying to build a Node tree using the first Node as Root, not Node. It is impossible for your nodes to be NodeImpl not ParentNode .
If you want a tree, you will have to create (or import) somehow the parent node. If you just need a list, use List .
You may need to create a fake custom parent to insert your nodes. Take a look here: HIERARCHY_REQUEST_ERR when trying to add elements to an xml file in a for loop