Q: adding mutiple namespace for a element in xml file using dom4j The XML file which need to generate:
<?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>

Two namespaces were added for the root element "wrapper:MMSRMessage",It has no problem.
The following is the Java code for it:
Document document = DocumentHelper.createDocument();
Element wrapper = document.addElement("wrapper:MMSRMessage");  
wrapper.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
        .addNamespace("wrapper", "urn:iso:std:iso:20022:tech:xsd:head.003.001.01")
        .addAttribute("xsi:schemaLocation", "urn:iso:std:iso:20022:tech:xsd:head.003.001.01 MMSR_head.003.001.01_Wrapper.xsd");

However, when I add two namespaces for element "header:AppHdr", I get the error message:

Exception in thread "main" org.dom4j.IllegalAddException: No such namespace prefix

using java code:
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");

I also have tried so:
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");

in this way the error does not occur, but the namespace "xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" can not be added for the element "header:AppHdr".
That's my first question at Stackoverflow. I hope I can get an answer hier :-)

A: DOM4J generally offers too many ways to skin the cat. In this area, the confusion is increased because Document#addElement(String, String) and Element#addElement(String, String) do very different validations: In the first case you can add an element with a qualified name without having the prefix bound to a namespace and the element ends up having no namespace (this is a bug). In the second case you must have the prefix bound (correct).
All in all, I reccomend not using qualified element and attribute names (prefix:local-name) if you can avoid it. Instead, strictly separate the local name of the element or attribute and use properly declared Namespace and QName constructs. In your case:
    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));

