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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- java – 为什么添加throws InterruptedException
- java多线程之wait(),notify(),notifyAll()的详解
- 详解Maven环境的搭建与idea配置
- 避免Java中的多个If语句
- java – Hibernate,一个具有不同列数的UserTyp
- java – 带有自定义安全过滤器的Spring Boot OAu
- java新特型
- java – 如何使用Spring 4在我的webSocket服务器
- java – PropertyNotFoundException:Target Unr
- Java Regex:matches(pattern,value)返回true,但
热点阅读