java – JDOM中的命名空间(默认)
发布时间:2020-05-24 17:57:35 所属栏目:Java 来源:互联网
导读:我正在尝试使用最新的JDOM包生成 XML文档.我遇到了根元素和命名空间的问题.我需要生成这个根元素: ManageBuildingsRequest xmlns=http://www.energystar.gov/manageBldgs/req xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:s
|
我正在尝试使用最新的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 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
