Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用dom4j为xml文件中的元素添加多个命名空间_Java_Xml_Namespaces_Dom4j - Fatal编程技术网

Java 使用dom4j为xml文件中的元素添加多个命名空间

Java 使用dom4j为xml文件中的元素添加多个命名空间,java,xml,namespaces,dom4j,Java,Xml,Namespaces,Dom4j,需要生成的XML文件: <?xml version="1.0" encoding="UTF-8" ?> <wrapper:MMSRMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wrapper="urn:iso:std:iso:20022:tech:xsd:head.003.001.01" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd

需要生成的XML文件:

<?xml version="1.0" encoding="UTF-8" ?> 
<wrapper:MMSRMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wrapper="urn:iso:std:iso:20022:tech:xsd:head.003.001.01" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:head.003.001.01 MMSR_head.003.001.01_Wrapper.xsd">
  <header:AppHdr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:header="urn:iso:std:iso:20022:tech:xsd:head.001.001.01">
    <header:Fr>
        ...
     </header:Fr>
  </header:AppHdr>
</wrapper:MMSRMessage>
但是,当我为元素“header:AppHdr”添加两个名称空间时,我得到错误消息:

线程“main”org.dom4j.IllegalAddException中的异常:没有这样的名称空间前缀

使用java代码:

Element headerApp = wrapper.addElement("header:AppHdr");
    headerApp.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
             .addNamespace("header", "urn:iso:std:iso:20022:tech:xsd:head.001.001.01");
我也试过这样做:

Element headerApp = wrapper.addElement("header:AppHdr","urn:iso:std:iso:20022:tech:xsd:head.001.001.01")
            .addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
这样就不会发生错误,但名称空间“xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance无法为元素“header:AppHdr”添加“”


这是我在Stackoverflow的第一个问题。我希望我能得到一个更高层次的答案:-)

DOM4J通常提供了太多剥猫皮的方法。在这方面,由于
Document#addElement(String,String)
Element#addElement(String,String)增加了混淆
执行完全不同的验证:在第一种情况下,您可以添加具有限定名称的元素,而无需将前缀绑定到命名空间,并且元素最终没有命名空间(这是一个错误)。在第二种情况下,您必须将前缀绑定(正确)

总之,如果可以避免的话,我建议不要使用限定的元素和属性名称(
前缀:local name
)。相反,请严格分隔元素或属性的本地名称,并使用正确声明的
名称空间
QName
构造。在您的情况下:

    Document document = DocumentHelper.createDocument();

    Namespace xsi = Namespace.get("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    Namespace wrapper = Namespace.get("wrapper", "urn:iso:std:iso:20022:tech:xsd:head.003.001.01");
    Namespace header = Namespace.get("header", "urn:iso:std:iso:20022:tech:xsd:head.001.001.01");


    Element wrapperElement = document
            .addElement(new QName("MMSRMessage", wrapper))
            .addAttribute(new QName("schemaLocation", xsi), "urn:iso:std:iso:20022:tech:xsd:head.003.001.01 MMSR_head.003.001.01_Wrapper.xsd");
    Element headerApp = wrapperElement.addElement(new QName("AppHdr", header));
    headerApp.addElement(new QName("Fr", header));

最后一次尝试似乎可以在名称空间中创建元素。至于名称空间声明
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“
在内部元素上,您是在尝试添加它时出错,还是在序列化/写出文档时根本不显示?显然,由于父元素已经具有相同的名称空间声明,序列化程序可能不会写出它,这不应该改变所创建XML的语义。
    Document document = DocumentHelper.createDocument();

    Namespace xsi = Namespace.get("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    Namespace wrapper = Namespace.get("wrapper", "urn:iso:std:iso:20022:tech:xsd:head.003.001.01");
    Namespace header = Namespace.get("header", "urn:iso:std:iso:20022:tech:xsd:head.001.001.01");


    Element wrapperElement = document
            .addElement(new QName("MMSRMessage", wrapper))
            .addAttribute(new QName("schemaLocation", xsi), "urn:iso:std:iso:20022:tech:xsd:head.003.001.01 MMSR_head.003.001.01_Wrapper.xsd");
    Element headerApp = wrapperElement.addElement(new QName("AppHdr", header));
    headerApp.addElement(new QName("Fr", header));