JDOM 中的命名空间(默认)
我正在尝试使用最新的 JDOM 包生成 XML 文档。我在根元素和命名空间方面遇到问题。我需要生成此根元素:
<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">
我使用此代码:
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
但是,ManageBuildingsRequest 之后的下一个元素也具有默认名称空间,这会破坏验证:
<customer xmlns="">
有帮助吗?感谢您抽出时间。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的构造函数用于
customer
元素会创建没有命名空间的元素。您应该使用以Namespace
作为参数的构造函数。您还可以为根元素和客户元素重复使用相同的Namespace
对象。The constructor you're using for the
customer
element creates it with no namespace. You should use the constructor with theNamespace
as parameter. You can also reuse the sameNamespace
object for both root and customer elements.这是实现自定义 XMLOutputProcessor 的替代方法,该方法会跳过发出空名称空间声明:
Here's an alternate approach that implements a custom XMLOutputProcessor that skips emitting empty namespace declarations:
我尝试了 javaanna 的代码,但不幸的是它继续在文档内容中生成空名称空间。在尝试了 bearontheroof 的代码后,XML 导出得很好。
创建自定义类后,您必须执行以下操作:
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: