- From: Luoh Ren-Shan <luors@iii.org.tw>
- Date: Thu, 24 Oct 2002 10:35:03 +0800
- To: <www-dom@w3.org>
Thank you for the information. I try to output <?xml version="1.0" encoding="UTF-8"?> <mypfx:abc xmlns:mypfx="urn:foo"/> with the following code using JAXP 1.2 (Java API for XML Processing) which supports DOM level 2. import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; public class XMLNS { // http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/ // glossary.html#dt-qualifiedname // // qualified name // A qualified name is the name of an element or attribute // defined as the concatenation of a local name // (as defined in this specification), optionally preceded // by a namespace prefix and colon character. // JAXP 1.2 private static Document newDoc() throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); return dbf.newDocumentBuilder().newDocument(); } // JAXP 1.2 private static void writeDoc(Document doc) throws Exception { Transformer t = TransformerFactory.newInstance().newTransformer(); t.transform(new DOMSource(doc), new StreamResult(System.out)); } // <?xml version="1.0" encoding="UTF-8"?> // <mypfx:abc xmlns:mypfx="urn:foo"/> public static void DOM2() throws Exception { Document doc = newDoc(); String uri = "urn:foo"; String prefix = "mypfx"; Element e = doc.createElementNS(uri, prefix + ":" + "abc"); e.setAttribute("xmlns:" + prefix, uri); // e.setAttributeNS("http://www.w3.org/2000/xmlns/", // "xmlns:" + prefix, uri); doc.appendChild(e); writeDoc(doc); } public static void main(String[] args) throws Exception { DOM2(); } } I'll try normalizeDocument() when I get a DOM level 3 implementaion in Java (which needs a special build of Apache Xalan from cvs). Thank you again! Luoh Ren-Shan ----- Original Message ----- From: "Gareth Reakes" <gareth@decisionsoft.com> To: "Luoh Ren-Shan" <luors@iii.org.tw> Cc: <www-dom@w3.org> Sent: Wednesday, October 23, 2002 4:39 PM Subject: Re: Questions about prefix and namespace > Hi, > > check out level 3 methods such as lookupPrefix. You do not need to > qualify the name as you describe unless you are hoping to serialize > without calling normalizeDocument. > > Gareth > > > On Wed, 23 Oct 2002, Luoh Ren-Shan wrote: > > > Hi all, > > > > Is there a function in the DOM spec to get namespace > > and prefix information from a given node? > > Do I have to write some function like > > "getPrefixList", "getNamespaceURIList", > > "getNamespaceURI2PrefixMap", etc. ? > > > > I have problem when using the "createElementNS" > > function which ask me to give a qualified name. > > I have to find which prefix to use (maybe the > > prefix has been defined before). > > I have to always write code like > > createElementNS("..", myPrefix + ":" + myLocalName) > > to concatenate the name. > > And when serializing the DOM tree, I have to > > add "xmlns:myPrefix=".."" attributes manually > > at some appropriate places. > > > > I'm not very familiar with the DOM API. > > Could some one tell me a better way to deal with it? > > > > Thank you, > > Luoh Ren-Shan > > > > > > > > > > > > -- > Gareth Reakes, Head of Product Development > DecisionSoft Ltd. http://www.decisionsoft.com > Office: +44 (0) 1865 203192 > > >
Received on Wednesday, 23 October 2002 22:33:52 UTC