You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Pranjal Bhatnagar <pr...@liqwidkrystal.com> on 2001/10/15 17:09:54 UTC

Adding New Child node to a Document

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

RE: Adding New Child node to a Document

Posted by Saurabh Shukla <sa...@cysphere.com>.

firstNode  = doc.getDocumentElement(), returns you the root node(firstNode).
You do not have to append it to the Document again.
doc.appendChild(firstNode) is not needed.

firstNode.appendChild(childNode) will suffice.

HTH,
Shuklix .

-----Original Message-----
From: Pranjal Bhatnagar [mailto:pranjal@liqwidkrystal.com]
Sent: Tuesday, October 16, 2001 9:51 AM
To: xerces-j-user@xml.apache.org; ishabtay@netvision.net.il
Subject: Re: Adding New Child node to a Document


Guess I am doing something wrong as just appendChild() also is not working.

I do the following:

 Element firstNode = doc.getDocumentElement();
firstNode.appendChild(childNode);
/*At this stage if I print firstNode, I see that childNode has been added
properly)
doc.appendChild(firstNode); // Here I get  a HierarchyRequestError */

Exception in thread "main" org.w3c.dom.DOMException: DOM006 Hierarchy
request e
ror
        at
org.apache.xerces.dom.CoreDocumentImpl.insertBefore(CoreDocumentImpl
java:381)
        at org.apache.xerces.dom.NodeImpl.appendChild(NodeImpl.java:224)
        at Test.parse(Test.java:53)
        at Test.main(Test.java:11)

I even tried to removeFirstNode from the doc and then add it again after
appending tempChild. It doesnt work. Any suggestions?
Thanks
Pranjal

----- Original Message -----
From: Isaac Shabtay
To: xerces-j-user@xml.apache.org ; Pranjal Bhatnagar
Sent: Monday, October 15, 2001 9:42 PM
Subject: Re: Adding New Child node to a Document


adoptNode() is experimental, as far as I remember.
appendChild() should be enough. Works for me.
----- Original Message -----
From: Pranjal Bhatnagar
To: xerces-j-user@xml.apache.org
Sent: Monday, October 15, 2001 5:09 PM
Subject: Adding New Child node to a Document


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


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: Adding New Child node to a Document

Posted by Pranjal Bhatnagar <pr...@liqwidkrystal.com>.
Guess I am doing something wrong as just appendChild() also is not working.

I do the following:

 Element firstNode = doc.getDocumentElement();
firstNode.appendChild(childNode);
/*At this stage if I print firstNode, I see that childNode has been added properly)
doc.appendChild(firstNode); // Here I get  a HierarchyRequestError */

Exception in thread "main" org.w3c.dom.DOMException: DOM006 Hierarchy request e
ror
        at org.apache.xerces.dom.CoreDocumentImpl.insertBefore(CoreDocumentImpl
java:381)
        at org.apache.xerces.dom.NodeImpl.appendChild(NodeImpl.java:224)
        at Test.parse(Test.java:53)
        at Test.main(Test.java:11)

I even tried to removeFirstNode from the doc and then add it again after appending tempChild. It doesnt work. Any suggestions?
Thanks
Pranjal

  ----- Original Message ----- 
  From: Isaac Shabtay 
  To: xerces-j-user@xml.apache.org ; Pranjal Bhatnagar 
  Sent: Monday, October 15, 2001 9:42 PM
  Subject: Re: Adding New Child node to a Document


  adoptNode() is experimental, as far as I remember.
  appendChild() should be enough. Works for me.
    ----- Original Message ----- 
    From: Pranjal Bhatnagar 
    To: xerces-j-user@xml.apache.org 
    Sent: Monday, October 15, 2001 5:09 PM
    Subject: Adding New Child node to a Document


    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

Re: Adding New Child node to a Document

Posted by Isaac Shabtay <is...@netvision.net.il>.
adoptNode() is experimental, as far as I remember.
appendChild() should be enough. Works for me.
  ----- Original Message ----- 
  From: Pranjal Bhatnagar 
  To: xerces-j-user@xml.apache.org 
  Sent: Monday, October 15, 2001 5:09 PM
  Subject: Adding New Child node to a Document


  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