XML : 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 :-)

No comments:

Post a Comment