Namespaces (default) in JDOM

I am trying to create an XML document using the latest JDOM package. I'm having problems with the root element and namespaces. I need to create 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 am using 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 following element after ManageBuildingsRequest also has a default namespace that violates validation:

 <customer xmlns=""> 

Any help? Thank you for your time.

+6
source share
3 answers

The constructor that you use for the customer element creates it without a namespace. You must use the constructor with the Namespace as parameter. You can also reuse the same Namespace object for root and user 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 
+15
source

Here's an alternative approach that implements its own XMLOutputProcessor that skips issuing 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); } } } 
+1
source

I tried javanna code, but unfortunately it continued to generate empty namespaces in the contents of the document. After you tried the notontheroof code, the XML exported was just fine.

You will need to do something similar after creating a custom class:

 CustomXMLOutputProcessor output = new CustomXMLOutputProcessor(); output.process(new FileWriter("/path/to/folder/generatedXML.xml"), Format.getPrettyFormat(), document); 
0
source

Source: https://habr.com/ru/post/902942/


All Articles