File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Adding an attribute to an element Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Adding an attribute to an element" Watch "Adding an attribute to an element" New topic
Author

Adding an attribute to an element

Bala Krishna
Ranch Hand

Joined: Jan 26, 2001
Posts: 95
How do I add an attribute to an element that's already existing in my xml document? Here's how I'm trying to do this:


But, the above code is throwing a DOMException with HIERARCHY_REQUEST_ERR. I'm guessing that's because the Attribute is not strictly a child node of the 'service' element I'm trying to add it to. I'm making sure that 'serviceNode' is indeed an element, by displaying the nodetype.

Can some one please tell me how to do this? Thanks in advance.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Bala,

To quote from the API spec for appendChild(...) -

DOMException - HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to append is one of this node's ancestors or this node itself.


I did not spend a lot of time reading this, but it explains the behaviour that you observed. Please take a look at the Attr interface description in the API Spec. -


Attr objects inherit the Node interface, but since they are not actually child nodes of the element they describe, the DOM does not consider them part of the document tree. Thus, the Node attributes parentNode, previousSibling, and nextSibling have a null value for Attr objects. The DOM takes the view that attributes are properties of elements rather than having a separate identity from the elements they are associated with; this should make it more efficient to implement such features as default attributes associated with all elements of a given type. Furthermore, Attr nodes may not be immediate children of a DocumentFragment. However, they can be associated with Element nodes contained within a DocumentFragment. In short, users and implementors of the DOM need to be aware that Attr nodes have some things in common with other objects inheriting the Node interface, but they also are quite distinct.


Look for workarounds, this is expected behavior according to the API.

- m


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Swordfish
Greenhorn

Joined: Jun 13, 2005
Posts: 4
Hi Bala,

Here's a workaround ...

I would put your code in following manner ...



Let me know if it works ...

Regards,

Swordfish

Originally posted by Bala Krishna:
How do I add an attribute to an element that's already existing in my xml document? Here's how I'm trying to do this:


But, the above code is throwing a DOMException with HIERARCHY_REQUEST_ERR. I'm guessing that's because the Attribute is not strictly a child node of the 'service' element I'm trying to add it to. I'm making sure that 'serviceNode' is indeed an element, by displaying the nodetype.

Can some one please tell me how to do this? Thanks in advance.
Bala Krishna
Ranch Hand

Joined: Jan 26, 2001
Posts: 95
Madhav,
Thanks for your research.
Swordfish,
Yes, that helped. Thanks a lot. I could also find another work around. The 'service' node I'm trying to add the attribute to, is the document element of my xml document. That allowed me to do the following :


Thanks again both of you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding an attribute to an element
 
Similar Threads
Writing XML element
Difference root element/root node
New to Java. Can you help with whit this (XML) ?
Transformer - omitting the xml header (" ?xml encoding=.. ?" )
How to parse XML using Dom parser