JDOM中的命名空间(默认)

jn1kk:

我正在尝试使用最新的JDOM包生成XML文档。我在使用root元素和名称空间时遇到麻烦。我需要产生这个根元素:

<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="">

有什么帮助吗?感谢您的时间。

javanna:

您用于元素构造函数不会customer创建任何名称空间。您应该将构造函数与Namespaceas参数一起使用。您还可以将同一Namespace对象用于根元素和客户元素。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章