[jdom-interest] XmlOutputter - printNamespace - NO_NAMESPACE outputs xmlns="", bug?
John Jefferson
surfer97301 at yahoo.com
Wed Jul 2 14:31:00 PDT 2003
Hi,
My problem is that when outputing xml some of the
children of my document are given blank namespaces.
For instance <root> <child1 xmlns=""></child1></root>
The reason for this is in the method below and occurs
because the namespace is NO_NAMESPACE but
namespaces.getURI("") is not null.
This is bad because I set the default namespace of the
root element to a value - the value of
namespaces.getURI("") - but the children are then
defined to be using a different namespace the xmlns of
"".
When I proceed to validate against a schema I find
that it fails b/c it doesn't find the children
belonging to the default namespace.
Am I doing something wrong or is there a bug?
Thanks,
MG
Here is a code snippet
Namespace default =
Namespace.getNamespace("http://locahost/root");
Namespace xsi = Namespace.getNamespace("xsi",
"http://www.w3.org/2001/XMLSchema-instance");
Namespace custom =
Namespace.getNamespace("custom",
"http://www.custom.com/CUSTOM");
String value =
"http://locahost/root "+
SCHEMA_LOCATION + "root.xsd " +
"http://www.custom.com/CUSTOM "+
SCHEMA_LOCATION + "custom.xsd";
Attribute schemalocation = new
Attribute("schemaLocation", value);
schemalocation.setNamespace(xsi);
//add the following namespaces declarations
ele.addNamespaceDeclaration(xsi);
ele.addNamespaceDeclaration(custom);
//set the namespace
ele.setNamespace(default);
ele.setAttribute(schemalocation);
StringWriter sw = new StringWriter();
XMLOutputter xmlOutputter = new
XMLOutputter("\t", true);
xmlOutputter.setOmitDeclaration(true);
xmlOutputter.setLineSeparator("\n");
xmlOutputter.setTextNormalize(false);
xmlOutputter.setTextTrim(false);
String output =
xmlOutputter.outputString(ele);
sw.write(output);
sw.write("\n");
What I end up with is then:
<root xmlns="http://localhost/root"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:custom="http://www.custom.com/CUSTOM"
xsi:schemaLocation="http://localhost/root root.xsd
http://www.custom.com/CUSTOM custom.xsd">
<child1 xmlns="">
</child1>
</root>
where the child1 element above is the problem.
<xmloutputter method jdom 9>
private void printElementNamespace(Element
element, Writer out, NamespaceStack namespaces)
throws IOException {
// Add namespace decl only if it's not the XML
namespace and it's
// not the NO_NAMESPACE with the prefix "" not
yet mapped
// (we do output xmlns="" if the "" prefix was
already used and we
// need to reclaim it for the NO_NAMESPACE)
Namespace ns = element.getNamespace();
if (ns == Namespace.XML_NAMESPACE) {
return;
}
if ( !((ns == Namespace.NO_NAMESPACE) &&
(namespaces.getURI("") == null))) {
printNamespace(ns, out, namespaces);
}
}
__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com
More information about the jdom-interest
mailing list