Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Modifying generated java classes, I don't agree. Not allowing all possible xsd format, I don't agree.</p> <p>Thanx to all your explanations an links, this is the code I've have written so as to take care of both cases, using Annotation Introspection. It works for output as well as input, and is <em>(in my taste)</em> more generic:</p> <pre><code>public class JaxbWrapper { private static boolean isXmlRootElement(Class classT){ Annotation[] annotations = classT.getAnnotations(); for(Annotation annotation : annotations){ if(annotation instanceof XmlRootElement){ return true; } } return false; } public static Object unmarshall(Class classObjectFactory, Class classObject, XMLStreamReader xmlStreamReader){ Package pack = classObjectFactory.getPackage(); String strPackageName = pack.getName(); Object returnObject = null; try { JAXBContext jc = JAXBContext.newInstance(strPackageName, classObjectFactory.getClassLoader()); Unmarshaller unmarshaller = jc.createUnmarshaller(); returnObject = unmarshaller.unmarshal( xmlStreamReader ); boolean bIsRootedElement = isXmlRootElement(classObject); if(!bIsRootedElement) { JAXBElement jaxbElement = (JAXBElement) returnObject; returnObject = jaxbElement.getValue(); } } catch (JAXBException e) { /*...*/ } return returnObject; } private static void writeToXml(Class classObjectFactory, Object obj, XMLStreamWriter xmlStreamWriter){ Package pack = classObjectFactory.getPackage(); String strPackageName = pack.getName(); try { JAXBContext jc = JAXBContext.newInstance(strPackageName, classObjectFactory.getClassLoader()); Marshaller marshaller = jc.createMarshaller(); marshaller.marshal(obj, xmlStreamWriter); } catch(JAXBException e) { /*...*/ } } public static String marshall(Class classObjectFactory, Class classObject, Object obj){ Object objectToMarshall = obj; boolean bIsRootedElement = isXmlRootElement(classObject); if(!bIsRootedElement) { Package pack = classObjectFactory.getPackage(); String strPackageName = pack.getName(); String strClassName = classObject.getName(); QName qName = new QName(strPackageName, strClassName); JAXBElement jaxbElement = new JAXBElement(qName, classObject, null, obj); objectToMarshall = jaxbElement; } StringWriter sw = new StringWriter(); XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance(); XMLStreamWriter xmlStreamWriter = null; try { xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(sw); writeToXml(classObjectFactory, objectToMarshall, xmlStreamWriter); xmlStreamWriter.flush(); xmlStreamWriter.close(); } catch (XMLStreamException e) { /*...*/ } return sw.toString(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload