[jdom-interest] problem with JDOM / Namespaces

Bernhard Boser boser at eecs.berkeley.edu
Thu Aug 3 08:35:45 PDT 2000


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>

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



More information about the jdom-interest mailing list