1

I have used the following code snippet

 marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
            Boolean.TRUE);
        marshaller.setSchema(getSchema(xsdSchema));
        marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper",
            new NamespacePrefixMapper() {
                @Override
                public String getPreferredPrefix(String arg0, String arg1,
                    boolean arg2) {
                    return "tf";
                }
            });
        marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
            "http://www.xyz.com/tf " + xsdSchema);

        marshaller.marshal(obj, new StreamResult(xml));

The output xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tf:abc xmlns:tf="http://www.xyz.com/tf" xmlns:ns2="http://www.w3.org/2001/XMLSchema-instance" ns2:schemaLocation="http://www.xyz.com/tf schema/myxsd.xsd">

As you see i'm getting "ns2" in the place of "xsi".

What i need would be xsi in ns2's place.

Thanks in advance.

2 Answers 2

1

Your namespaceprefixmapper should return the value for each namespace declared:

package mapdemo;

import com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

public class JAXBMarshallerExample {

    public static NamespacePrefixMapper val = new NamespacePrefixMapper() {

        private static final String XSI_URI = "http://www.w3.org/2001/XMLSchema-instance";
        private static final String TF_URI = "http://www.xyz.com/tf/whatever.xsd";

        @Override
        public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
            if(XSI_URI.equals(namespaceUri)) {
                return "xsi";
            } else if(TF_URI.equals(namespaceUri)) {
                return "tf";
            }
            return suggestion;
        }

        @Override
        public String[] getPreDeclaredNamespaceUris() {
            return new String[] { XSI_URI, TF_URI};
        }
    };

    public static void main(String[] args) throws Exception {

        JAXBContext context = JAXBContext.newInstance(Output.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", val);

        Output output = new Output();
        output.setId("1");

        marshaller.marshal(output, System.out);
    }

    @XmlRootElement(namespace="http://www.xyz.com/tf/whatever.xsd")
    public static class Output {
        String id;

        public String getId() {
            return id;
        }

        @XmlElement
        public void setId(String id) {
            this.id = id;
        }
    }
}
2
  • Thank you @SorenD, tried your approach. But it is still ns2 in place of xsi.
    – Pavan Sky
    Sep 16, 2013 at 7:13
  • Hi @PavanSky. I have updated example with full code. Otherwise, please update with information regarding the setSchema(...) and the object you're trying to marshal.
    – SorenD
    Sep 16, 2013 at 15:01
0

You could extend your implementation of NamespacePrefixMapper to do this instead of always returning tf from getPreferredPrefix

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.