ekitru
ekitru

Reputation: 181

Jaxb XML marshaling add prefix to root element

I have follow problem, whem I am generating XML file Jaxb add prefix to namespace of root element and I don't know how to skip it.

I have package-info.java file

    @XmlSchema(namespace = "urn:iso:std:iso:20022:tech:xsd:pain.001.001.03", xmlns = {
        @XmlNs(namespaceURI = "urn:iso:std:iso:20022:tech:xsd:pain.001.001.03", prefix = "")
}, elementFormDefault = XmlNsForm.QUALIFIED)

I have root element

    @XmlType(name = "Document",
        propOrder = {
                "cstmrCdtTrfInitn"
        })
@XmlRootElement(name = "Document")
public class Document {

        @XmlElement(name = "CstmrCdtTrfInitn", required = true)
        protected CustomerCreditTransferInitiationV03 cstmrCdtTrfInitn;
    }

And as result I got

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Document xmlns:ns2="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
    <ns2:CstmrCdtTrfInitn>
        <ns2:GrpHdr/>
    </ns2:CstmrCdtTrfInitn>
</ns2:Document>

without @XmlNs it work in same way, adding namespace to @XmlRoolElement also doen't help. What can be wrong with it?

Upvotes: 0

Views: 1103

Answers (1)

Zielu
Zielu

Reputation: 8552

Not sure what you are trying achieve, your xml is matching your code perfectly fine.

If you don't want namespace at all:

<Document>
 <CstmrCdtTrfInitn>
  <GrpHdr>ala</GrpHdr>
 </CstmrCdtTrfInitn>
</Document

simply change package info to

@XmlSchema()

If you want to use target namespace (so no prefix), like

<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
 <CstmrCdtTrfInitn>
  <GrpHdr>ala</GrpHdr>
 </CstmrCdtTrfInitn>
</Document>

then

@XmlSchema(
    namespace = "urn:iso:std:iso:20022:tech:xsd:pain.001.001.03",
    elementFormDefault = XmlNsForm.QUALIFIED)

all above tested with standard Java7 jaxb

Upvotes: 1

Related Questions