I am currently assembling a JAXB object in the output stream with the following code
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); ByteArrayOutputStream out = new ByteArrayOutputStream(); marshaller.marshal(new JAXBElement(new QName("hard_coded_namespace", clazz.getSimpleName()), clazz, obj), out);
I would like to replace "hard_coded_namespace" with the namespace contained in JAXB "obj" (or one of its attributes, they must currently have the same NS).
Any ideas on how to get NS information before marshaling? Namespaces appear in the output stream. Therefore, they are located somewhere in "obj".
[UPDATE] As indicated in the answers below, I do not need to set the JAXB_FRAGMENT property. I changed it to:
JAXB.marshal(new JAXBElement<T>(new QName("hard_coded_namespace", rootName), clazz, jaxbObject), out);
Chris source share