Java – namespace in JDOM (default)

I'm trying to generate XML documents using the latest JDOM package I encountered a problem with the root element and namespace I need to generate 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 also has a default namespace, which destroys validation:

<customer xmlns="">

Does it help? Thank you for your time

Solution

The constructor you use for the customer element does not have a namespace when it is created You should use a constructor with a namespace as an argument You can also reuse the same namespace object for the 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",XSI);

Element customer = new Element("customer",namespace);
root.addContent(customer);
doc.addContent(root); // doc jdom Document
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>