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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With