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 "Becker, Matthias" <ma...@sap.com> on 2001/02/12 15:28:26 UTC
Append Attribute to each Element-Node
Hi,
I try to append one Attribute to each Element in an document.
If I try something like this:
NodeList Elements = document.getElementByTagName("*");
for (int i=0; i<Elements.getLength(); i++){
Attr a = xmldoc.createAttribute("id");
a.setNodeValue(i);
Elements.item(i).append.appendChild((Node) a);
}
I get the following error.
org.w3c.dom.DOMException: DOM006 Hierarchy request error
at org.apache.xerces.dom.ChildAndParentNode.internalInsertBefore
(ChildAndParentNode.java:379)
at org.apache.xerces.dom.ChildAndParentNode.insertBefore
(ChildAndParentNode.java:296)
at org.apache.xerces.dom.NodeImpl.appendChild
(NodeImpl.java:212)
at parsertest.<init>(parsertest.java:36)
at parsertest.main(parsertest.java:44)
Exception in thread "main"
Where do I make the mistake? I think this one should be done easy.
It would be easy if it get an List of Elements instead of a NodeList,
because an Element as the Method setAttribute(). But a Node doesn't have
this Methode.
How can I do this anyway.
Thanks in advance
Matthias
Re: Append Attribute to each Element-Node
Posted by Arnaud Le Hors <le...@us.ibm.com>.
If all you have as the value is a String it is better to use
setAttribute(String name, String value) because the DOM implementation
may optimize the storage (ours does :-).
Otherwise, simply changing appendChild to setAttributeNode would solve
your problem.
--
Arnaud Le Hors - IBM Cupertino, XML Strategy Group
RE: Append Attribute to each Element-Node
Posted by Fernando Padilla <fe...@interdimensions.com>.
Sorry, but an attribute can not be a child of an node!
You have to use the NamedNodeMap attributes of the node.
NamedNodeMap attrs = node.getAttributes();
Though, what was previously suggested is very good too, if you have an
element object.
element.setAttribute( "name", "value" );
> -----Original Message-----
> From: Becker, Matthias [mailto:ma.becker@sap.com]
> Sent: Monday, February 12, 2001 09:28
> To: 'xerces-j-user@xml.apache.org'
> Subject: Append Attribute to each Element-Node
>
>
> Hi,
>
> I try to append one Attribute to each Element in an document.
>
> If I try something like this:
>
> NodeList Elements = document.getElementByTagName("*");
> for (int i=0; i<Elements.getLength(); i++){
> Attr a = xmldoc.createAttribute("id");
> a.setNodeValue(i);
> Elements.item(i).append.appendChild((Node) a);
> }
>
> I get the following error.
>
> org.w3c.dom.DOMException: DOM006 Hierarchy request error
> at org.apache.xerces.dom.ChildAndParentNode.internalInsertBefore
> (ChildAndParentNode.java:379)
> at org.apache.xerces.dom.ChildAndParentNode.insertBefore
> (ChildAndParentNode.java:296)
> at org.apache.xerces.dom.NodeImpl.appendChild
> (NodeImpl.java:212)
> at parsertest.<init>(parsertest.java:36)
> at parsertest.main(parsertest.java:44)
> Exception in thread "main"
>
> Where do I make the mistake? I think this one should be done easy.
>
> It would be easy if it get an List of Elements instead of a NodeList,
> because an Element as the Method setAttribute(). But a Node doesn't have
> this Methode.
>
> How can I do this anyway.
>
> Thanks in advance
>
> Matthias
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>
RE: Append Attribute to each Element-Node
Posted by Rick Bullotta <Ri...@lighthammer.com>.
I would suggest using Element.setAttribute(java.lang.String name,
java.lang.String value) as an alternative.
- Rick Bullotta
Lighthammer Software (www.lighthammer.com)
-----Original Message-----
From: Becker, Matthias [mailto:ma.becker@sap.com]
Sent: Monday, February 12, 2001 09:28
To: 'xerces-j-user@xml.apache.org'
Subject: Append Attribute to each Element-Node
Hi,
I try to append one Attribute to each Element in an document.
If I try something like this:
NodeList Elements = document.getElementByTagName("*");
for (int i=0; i<Elements.getLength(); i++){
Attr a = xmldoc.createAttribute("id");
a.setNodeValue(i);
Elements.item(i).append.appendChild((Node) a);
}
I get the following error.
org.w3c.dom.DOMException: DOM006 Hierarchy request error
at org.apache.xerces.dom.ChildAndParentNode.internalInsertBefore
(ChildAndParentNode.java:379)
at org.apache.xerces.dom.ChildAndParentNode.insertBefore
(ChildAndParentNode.java:296)
at org.apache.xerces.dom.NodeImpl.appendChild
(NodeImpl.java:212)
at parsertest.<init>(parsertest.java:36)
at parsertest.main(parsertest.java:44)
Exception in thread "main"
Where do I make the mistake? I think this one should be done easy.
It would be easy if it get an List of Elements instead of a NodeList,
because an Element as the Method setAttribute(). But a Node doesn't have
this Methode.
How can I do this anyway.
Thanks in advance
Matthias
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org