Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding an attribute to an element

 
Bala Krishna
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Swordfish
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic