[jdom-interest] Strange namespace problem

Marcel Stor marcel at frightanic.com
Mon Nov 24 06:05:25 PST 2003


Hi all,

I need to produce the following XML tag:
<IT-P_CDB xmlns="http://www.it-p.ch/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.it-p.ch/namespace
http://www.praut.com/CDB.xsd" type="XML">

I do this through:
Namespace schemaNS = Namespace.getNamespace("xsi",
"http://www.w3.org/2001/XMLSchema-instance");
Namespace defaultNS =
Namespace.getNamespace("http://www.it-p.ch/namespace");
Element root = new Element("IT-P_CDB");
root.setNamespace(defaultNS);
root.addNamespaceDeclaration(schemaNS);
root.setAttribute("schemaLocation", defaultNS.getURI() + "
http://www.praut.com/CDB.xsd", schemaNS);
root.setAttribute("type", "XML");

Then I add a complete customer structure to the root element and have it
printed:
Document doc = new Document(root);
root.addContent(customer.toXML());
new XMLOutputter("    ", true).output(doc, etc....

What I get is:
<?xml version="1.0" encoding="UTF-8"?>
<IT-P_CDB xmlns="http://www.it-p.ch/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.it-p.ch/namespace
http://www.praut.com/CDB.xsd" type="XML">
    <customer xmlns="">
        <name>

This is invalid XML because of the empty default namespace declaration
in <customer>. If I have customer.toXML() printed without the root
element, <customer> doesn't have a namespace attribute.

Where's the problem here???

Regareds,
Marcel




More information about the jdom-interest mailing list