In order to add a new Child node to a Document-doc, I try the following code.
 
doc.adoptNode(childNode);
doc.appendChild(childNode);
 
However this gives a ClassCastException as follows while executing doc.adoptNode();
 
Exception in thread "main" java.lang.ClassCastException: org.apache.xerces.dom.D
ocumentImpl
        at org.apache.xerces.dom.DeferredAttrNSImpl.synchronizeChildren(Deferred
AttrNSImpl.java:182)
        at org.apache.xerces.dom.AttrImpl.setOwnerDocument(AttrImpl.java:214)
        at org.apache.xerces.dom.NamedNodeMapImpl.setOwnerDocument(NamedNodeMapI
mpl.java:421)
        at org.apache.xerces.dom.ElementImpl.setOwnerDocument(ElementImpl.java:1
93)
        at org.apache.xerces.dom.ParentNode.setOwnerDocument(ParentNode.java:226
)
        at org.apache.xerces.dom.ElementImpl.setOwnerDocument(ElementImpl.java:1
91)
        at org.apache.xerces.dom.CoreDocumentImpl.adoptNode(CoreDocumentImpl.jav
a:1181)
        at Test.parse(Test.java:49)
        at Test.main(Test.java:11)
 
 
In case I use doc.importNode(childNode, true) a NullPointerException is thrown while trying to retrieve the Doc elements.
 
Is this approach correct or do we need to do something else?
 
Thanks,
Pranjal

Reply via email to