I decided to implement the Abstract List<Node>
. here is a piece 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)
is this because of the Node root = null;
which makes to add a node to a null node?
then how can be fixed
You can't append to a com.sun.org.apache.xerces.internal.dom.NodeImpl
, you'll need to use com.sun.org.apache.xerces.internal.dom.ParentNode
.
appendChild
will call insertBefore
which only throws an Exception
for NodeImpl
Source code
Move one or more node(s) to our list of children. Note that this implicitly removes them from their previous parent.
By default we do not accept any children, ParentNode overrides this.
Take a look how Axis
implemented their : http://grepcode.com/file/repo1.maven.org/maven2/com.ning/metrics.collector/1.0.2/org/apache/axis/message/NodeListImpl.java
It seems you're trying to build a Node tree using the first Node as the Root, not a node list. Which is not possible has your nodes are NodeImpl
not ParentNode
.
If you want a tree, you'll have to create (or import) somehow a parent node.
If you just need a list, then use a List
.
You may need to create a fake custom parent to insert your nodes. Take a look here : HIERARCHY_REQUEST_ERR while trying to add elements to xml file in a for loop
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With