Namespaces (Default) in JDOM
Asked Answered
P

3

6

I am trying to produce a XML document using the newest JDOM package. I'm having trouble with the root element and the namespaces. I need to produce this root element:

<ManageBuildingsRequest 
    xmlns="http://www.energystar.gov/manageBldgs/req" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.energystar.gov/manageBldgs/req 
                        http://estar8.energystar.gov/ESES/ABS20/Schemas/ManageBuildingsRequest.xsd">

I use this code:

Element root = new Element("ManageBuildingsRequest");
root.setNamespace(Namespace.getNamespace("http://www.energystar.gov/manageBldgs/req"));
Namespace XSI = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
root.addNamespaceDeclaration(XSI);
root.setAttribute("schemaLocation", "http://www.energystar.gov/manageBldgs/req http://estar8.energystar.gov/ESES/ABS20/Schemas/ManageBuildingsRequest.xsd", XSI);

Element customer = new Element("customer");
root.addContent(customer);
doc.addContent(root); // doc jdom Document

However, the next element after ManageBuildingsRequest has the default namespace as well, which breaks the validation:

<customer xmlns="">

Any help? Thank you for your time.

Proleg answered 2/12, 2011 at 16:8 Comment(1)
Can you post the code generating your xml please?Lowe
M
17

The constructor you're using for the customer element creates it with no namespace. You should use the constructor with the Namespace as parameter. You can also reuse the same Namespace object for both root and customer elements.

Namespace namespace = Namespace.getNamespace("http://www.energystar.gov/manageBldgs/req");
Element root = new Element("ManageBuildingsRequest", namespace);
Namespace XSI = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
root.addNamespaceDeclaration(XSI);
root.setAttribute("schemaLocation", "http://www.energystar.gov/manageBldgs/req http://estar8.energystar.gov/ESES/ABS20/Schemas/ManageBuildingsRequest.xsd", XSI);

Element customer = new Element("customer", namespace);
root.addContent(customer);
doc.addContent(root); // doc jdom Document
Melodeemelodeon answered 2/12, 2011 at 19:13 Comment(3)
You're right, but I am not quite sure why. I have to pass the namespace to every child which I had a lot of - became quite a pain, but this fixed it. Thanks.Proleg
@jsn Ran into the same issue and completely agree with you. This is a terrible API. Was hoping to find a better solution.Inconsiderate
A detailed explanation is here: #26675424Pliable
N
1

Here's an alternate approach that implements a custom XMLOutputProcessor that skips emitting empty namespace declarations:

public class CustomXMLOutputProcessor extends AbstractXMLOutputProcessor {
    protected void printNamespace(Writer out, FormatStack fstack, Namespace ns)
            throws java.io.IOException {
        System.out.println("namespace is " + ns);
        if (ns == Namespace.NO_NAMESPACE) {
            System.out.println("refusing to print empty namespace");
            return;
        } else {
            super.printNamespace(out, fstack, ns);
        }
    }
}
Nabala answered 13/4, 2012 at 19:46 Comment(1)
The post may be old but this was a rather elegant workaround to a troubled API. Thanks!Conjoin
W
0

I tried javanna's code but unfortunately it kept on generating the empty namespaces in the document's contents. After trying bearontheroof's code the XML exported just fine.

You would have to do something like this after creating the custom class:

CustomXMLOutputProcessor output = new CustomXMLOutputProcessor();
output.process(new FileWriter("/path/to/folder/generatedXML.xml"), Format.getPrettyFormat(), document);
Whippet answered 30/10, 2014 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.