[jdom-interest] problem with JDOM / Namespaces

Brett McLaughlin brett.mclaughlin at lutris.com
Thu Aug 3 09:16:31 PDT 2000


Bernhard Boser wrote:
> 
> This XML document is produced with jdom (see code below).
> 
> <?xml version="1.0" encoding="UTF-8"?>
> 
> <root xmlns="http://ns1">
>   <ns2:ns2elementA xmlns:ns2="http://ns2" />
>   <ns2:ns2elementB />
> </root>

It is technically proper XML - the name of the first nested element is
ns2elementA in the http://ns2 namespace, the name of the second element
is ns2:ns2elementB. XML 1.0 allows names with colons, although it
clearly says that they may be used later on. In JDOM, we don't allow
names with colons (at least in Beta 4) - that's why you are getting the
error. But we need to fix this, so we'll take a look - are you in JDOM
Beta 4, or the latest CVS? Try the latest CVS if you aren't there, it
may be fixed by now (we've had this reported before).

-Brett

> 
> Note that the name space (xmlns:ns2) is specified only for the first element
> in that namespace (elementA), not for the second one (elementB). I'm not
> sure if that's proper xml ... but at least jdom doesn't think it is. When
> reading the document with the SAXBuilder, it gives the following error:
> 
> org.jdom.JDOMException: The name "" is not legal for JDOM/XML namespaces:
> Namespace URIs must be non-null and non-empty Strings..: The name "" is not
> legal for JDOM/XML namespaces: Namespace URIs must be non-null and non-empty
> Strings..
>         at org.jdom.input.SAXBuilder.build(SAXBuilder.java:231)
>         at org.jdom.input.SAXBuilder.build(SAXBuilder.java:250)
>         at JdomProblem.main(JdomProblem.java:32)
> Root cause: org.xml.sax.SAXException: The name "" is not legal for JDOM/XML
> namespaces: Namespace URIs must be non-null and non-empty Strings..
>         at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:878)
>         at org.jdom.input.SAXBuilder.build(SAXBuilder.java:220)
>         at org.jdom.input.SAXBuilder.build(SAXBuilder.java:250)
>         at JdomProblem.main(JdomProblem.java:32)
> 
> Many thanks for help/suggestions.
> Bernhard
> 
> Jdom code that produces the above xml document and error:
> 
> import java.io.*;
> import org.jdom.*;
> import org.jdom.output.*;
> import org.jdom.input.*;
> 
> public class JdomProblem {
> 
>         private static String fileName = "problem.txt";
>         private static Namespace ns1 = Namespace.getNamespace("http://ns1");
>         private static Namespace ns2 = Namespace.getNamespace("ns2",
> "http://ns2");
> 
>         public static void main(String args[]) {
>                 // Create a simple document
>                 Document doc = new Document(new Element("root", ns1)
>                                                 .addChild(new
> Element("ns2elementA", ns2))
>                                                 .addChild(new
> Element("ns2elementB", ns2)));
>                 // Output to file
>                 try {
>                         OutputStream out = new FileOutputStream(fileName);
>                         (new XMLOutputter()).output(doc, out);
>                         (new XMLOutputter()).output(doc, System.out);
>                         out.close();
>                 }
>                 catch (Exception eOut) {
>                         println();
>                         println("Error while trying to WRITE XML document");
>                         eOut.printStackTrace();
>                 }
>                 // Now read it back in
>                 try {
>                         InputStream in = new FileInputStream(fileName);
>                         Document read = (new SAXBuilder()).build(in);
>                         in.close();
>                 }
>                 catch (Exception eOut) {
>                         println();
>                         println("Error while trying to READ XML document");
>                         eOut.printStackTrace();
>                 }
>         }       // main
> 
>         public static void println(Object o) { System.out.println(o); }
> 
>         public static void println() { System.out.println(); }
> 
> }       // JdomProblem
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com

-- 
Brett McLaughlin, Enhydra Strategist
Lutris Technologies, Inc. 
1200 Pacific Avenue, Suite 300 
Santa Cruz, CA 95060 USA 
http://www.lutris.com
http://www.enhydra.org



More information about the jdom-interest mailing list