Open Aravinda93 opened 9 months ago
@Aravinda93
Regards, Antonio.
@antoniosanct Thanks a lot for the response. For your convience I have added the sample code repo here: https://github.com/Aravinda93/test/tree/main
I was trying couple of things thats the reason I added the org.glassfish.jaxb.runtime.marshaller.NamespacePrefixMapper
but I have tried the org.glassfish.jaxb.namespacePrefixMapper
and that did not work either for me.
As you mentioned I have removed the Unmarshaller
part although I was not using it and kept only the Marshaller
but still I get the same error:
Exception in thread "main" jakarta.xml.bind.JAXBException: property "org.glassfish.jaxb.namespacePrefixMapper" is not supported
Following is my main class updated:
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.Marshaller;
import java.util.HashMap;
import java.util.Map;
public class MainXML {
public static void main(String[] args) throws Exception {
final Map<String, String> myNamespaces = new HashMap<>();
myNamespaces.put("test", "https://test.com");
myNamespaces.put("test2", "https://test2.com");
final JAXBContext jaxbContext = JAXBContext.newInstance("io.test.convert", Thread.currentThread().getContextClassLoader(),
new HashMap<>() {
{
put(
"org.glassfish.jaxb.namespacePrefixMapper",
new CustomNamespacePrefixMapper());
}
});
//final JAXBContext jaxbContext = JAXBContext.newInstance("io.test.convert", Thread.currentThread().getContextClassLoader());
final Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty("org.glassfish.jaxb.namespacePrefixMapper", new CustomNamespacePrefixMapper());
final Child1 child1 = new Child1();
child1.setName("Batman");
child1.setAge("30");
child1.setType("Superhero");
child1.setOriginalName("Bruce Wayne");
marshaller.marshal(child1, System.out);
}
}
@Aravinda93 You can not set this property at context initialization. See constants defined here to check what properties you can initialize. Lines 23 and 28 of your class are correct (delete hashmap initialization in 15 and uncomment 23 to run fine).
Regards, Antonio.
For convience I have added the sample code repo here: https://github.com/Aravinda93/test/tree/main
I am using JAXB RI to marshall the Java objects into XML but during the marshalling I need to provide some of the custom namespaces to my
Marshaller
andJAXBContext
. I referred the documentation and answer here to do the same but its resulting in the error:Following is the completed code I have:
Child1.class
:Parent.class
:CustomNamespacePrefixMapper.class
:MainXML.class
:Following is my
sampleXML.xml
:ObjectFactory.class
:When I run I get the error:
How to provide the custom namespaces to the
JAXBContext/Marshaller
so it can be used during the marshalling of the Java objects to XML?Following are my dependencies: