简体   繁体   中英

Remove ns2 prefix from xml using jaxb while marshalling

I am performing marshalling using jaxB and xml is generated, but the issue is that there is "ns2" prefix in the root element of xml. I want to remove this prefix "ns2" from the generated xml while marshalling using jaxb.

I have made changes in package-info.java by putting the prefix as "" for the particular namespaceURI, so in that case instead of ns2 it becomes ns3, and my issue remained same.

I have also tried using NamespacePrefixMapper and same thing is happening with that as well (instead of ns2 it becomes ns3).

I want xml as below-

<?xml version="1.0" encoding="UTF-8"?>
<Document
    xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02 pacs.008.001.02.xsd">
<FIToFICstmrCdtTrf>
        .....
</FIToFICstmrCdtTrf>
</Document>

But this is wat I am getting -:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <ns3:Document xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02 
 pacs.008.001.02.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:ns3="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02">
 <ns3:FIToFICstmrCdtTrf>
 .....
 </ns3:FIToFICstmrCdtTrf>
 </ns3:Document>

Please have a look at my code-

  1. Demo Code

     jaxbContext = JAXBContext.newInstance(/*my class*/); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); StringWriter stringWriter = new StringWriter(); marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02 pacs.008.001.02.xsd"); marshaller.marshal(document, stringWriter);
  2. package-info.java

     @javax.xml.bind.annotation.XmlSchema (namespace = "urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, xmlns = { @javax.xml.bind.annotation.XmlNs(prefix="xsi", namespaceURI="http://www.w3.org/2001/XMLSchema-instance"), @javax.xml.bind.annotation.XmlNs(prefix="", namespaceURI="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02") } )
  3. Root element

    @XmlRootElement(name="Document") @XmlAccessorType(XmlAccessType.NONE) @XmlType(name = "Document", propOrder = { "fiToFICstmrCdtTrf" }) @XmlSeeAlso({ DocumentEPC12216SCTINSTIB2019V10 .class }) @ToString public class Document extends ISODocument { @XmlElement(name = "FIToFICstmrCdtTrf", required = true) protected FIToFICustomerCreditTransferV02 fiToFICstmrCdtTrf; public FIToFICustomerCreditTransferV02 getFIToFICstmrCdtTrf() { return fiToFICstmrCdtTrf; } public void setFIToFICstmrCdtTrf(FIToFICustomerCreditTransferV02 value) { this.fiToFICstmrCdtTrf = value; } }

I tried various solutions for this but unfortunately none of them worked. Please let me know if anyone knows the solution, it would be very helpful for me.

Thanks in advance.!

I am performing marshalling using jaxB and xml is generated, but the issue is that there is "ns2" prefix in the root element of xml. I want to remove this prefix "ns2" from the generated xml while marshalling using jaxb.

I have made changes in package-info.java by putting the prefix as "" for the particular namespaceURI, so in that case instead of ns2 it becomes ns3, and my issue remained same.

I have also tried using NamespacePrefixMapper and same thing is happening with that as well (instead of ns2 it becomes ns3).

I want xml as below-

<?xml version="1.0" encoding="UTF-8"?>
<Document
    xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02 pacs.008.001.02.xsd">
<FIToFICstmrCdtTrf>
        .....
</FIToFICstmrCdtTrf>
</Document>

But this is wat I am getting -:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <ns3:Document xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02 
 pacs.008.001.02.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:ns3="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02">
 <ns3:FIToFICstmrCdtTrf>
 .....
 </ns3:FIToFICstmrCdtTrf>
 </ns3:Document>

Please have a look at my code-

  1. Demo Code

     jaxbContext = JAXBContext.newInstance(/*my class*/); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); StringWriter stringWriter = new StringWriter(); marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02 pacs.008.001.02.xsd"); marshaller.marshal(document, stringWriter);
  2. package-info.java

     @javax.xml.bind.annotation.XmlSchema (namespace = "urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, xmlns = { @javax.xml.bind.annotation.XmlNs(prefix="xsi", namespaceURI="http://www.w3.org/2001/XMLSchema-instance"), @javax.xml.bind.annotation.XmlNs(prefix="", namespaceURI="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02") } )
  3. Root element

    @XmlRootElement(name="Document") @XmlAccessorType(XmlAccessType.NONE) @XmlType(name = "Document", propOrder = { "fiToFICstmrCdtTrf" }) @XmlSeeAlso({ DocumentEPC12216SCTINSTIB2019V10 .class }) @ToString public class Document extends ISODocument { @XmlElement(name = "FIToFICstmrCdtTrf", required = true) protected FIToFICustomerCreditTransferV02 fiToFICstmrCdtTrf; public FIToFICustomerCreditTransferV02 getFIToFICstmrCdtTrf() { return fiToFICstmrCdtTrf; } public void setFIToFICstmrCdtTrf(FIToFICustomerCreditTransferV02 value) { this.fiToFICstmrCdtTrf = value; } }

I tried various solutions for this but unfortunately none of them worked. Please let me know if anyone knows the solution, it would be very helpful for me.

Thanks in advance.!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM