加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – JDOM中的命名空间(默认)

发布时间:2020-12-14 23:57:33 所属栏目:Java 来源:网络整理
导读:我正在尝试使用最新的JDOM包生成 XML文档.我遇到了根元素和命名空间的问题.我需要生成这个根元素: ManageBuildingsRequest xmlns="http://www.energystar.gov/manageBldgs/req" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=
我正在尝试使用最新的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="">

有帮助吗?感谢您的时间.

解决方法

您用于customer元素的 constructor创建它时没有命名空间.您应该使用带有Namespace的构造函数作为参数.您还可以为root和customer元素重用相同的Namespace对象.
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读