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 Christian Geuer-Pollmann <ge...@nue.et-inf.uni-siegen.de> on 2001/04/02 17:50:16 UTC
How to create attributes ?!?
I have a problem: I can't create attributes in Xerces: all things other
than attr are null. Any tip?
Thanks in advance,
Christian
---------------------------------
attr0 (attr0="val0"): prefix(null) localName(null) nsURI(null) attr00
(attr0="val0"): prefix(null) localName(null) nsURI(null) attr10
(attr1="val1"): prefix(null) localName(null) nsURI(null)
---------------------------------
import org.apache.xerces.dom.DocumentImpl;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Attr;
public class attributeGenerator {
public static void main(String[] args) {
Document doc = new DocumentImpl();
Element root = doc.createElement("documentElement");
doc.appendChild(root);
// way 1 to generate the attribute
Attr attr0 = doc.createAttribute("attr0");
attr0.appendChild(doc.createTextNode("val0"));
root.setAttributeNode(attr0);
// way 2 to generate the attribute
root.setAttribute("attr1", "val1");
NamedNodeMap nnm = root.getAttributes();
if (nnm.getLength() != 2) {
System.err.println("More than two attributes: " + nnm.getLength());
System.exit(1);
}
Attr attr00 = (Attr) nnm.item(0);
Attr attr10 = (Attr) nnm.item(1);
printAttr("attr0", attr0 );
printAttr("attr00", attr00 );
printAttr("attr10", attr10 );
}
public static void printAttr(String description, Attr attr0) {
String prefix0 = attr0.getPrefix();
String localName0 = attr0.getLocalName();
String namespaceURI0 = attr0.getNamespaceURI();
System.out.println(description + " (" + attr0 + "): prefix(" + prefix0
+ ") localName(" + localName0 + ") nsURI("
+ namespaceURI0 + ")");
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org