java - spring-oxm: Can I unmarshall a subelement of a file? -


this related my prior question more directed towards jaxb in general. question more related unmarshaller in spring-oxm. i'm looking see if can use spring-oxm unmarshaller unmarshal specific elements xml.

my xsd is:

<xs:schema version="1.3"   targetnamespace="https://www.domain.com/schema/reports/export/1.0"   xmlns:tns="https://www.domain.com/schema/reports/export/1.0"   xmlns:xs="http://www.w3.org/2001/xmlschema"   elementformdefault="qualified">  <xs:element name="detailedreport">   <xs:complextype>     <xs:sequence>       <xs:element name="severity" minoccurs="6" maxoccurs="6" type="tns:severitytype" />     </xs:sequence>     </xs:complextype> </xs:element>  <xs:complextype name="severitytype">   <xs:sequence>     <xs:element name="category" minoccurs="0" maxoccurs="unbounded" type="tns:categorytype"/>   </xs:sequence> </xs:complextype>  <xs:complextype name="categorytype">   <xs:sequence>     <xs:element name="cwe" maxoccurs="unbounded" type="tns:cwetype"/>   </xs:sequence> </xs:complextype>  <xs:complextype name="cwetype">   <xs:sequence>     <xs:element name="staticflaws" type="tns:flawlisttype" minoccurs="0"/>   </xs:sequence> </xs:complextype>  <xs:complextype name="flawlisttype">   <xs:sequence>     <xs:element name="flaw" minoccurs="0" maxoccurs="unbounded" type="tns:flawtype" />   </xs:sequence> </xs:complextype> </xs:schema> 

using preprocessing, can find nodes of type "cwe":

    documentbuilderfactory dbf = documentbuilderfactory.newinstance();     documentbuilder db = dbf.newdocumentbuilder();      document doc = db.parse(ioutils.toinputstream(xml));     nodelist nodelist = doc.getelementsbytagname("cwe"); 

using jaxbunmarshaller, can manage unmarshal object:

    jaxbcontext jc = jaxbcontext.newinstance( cwetype.class );     unmarshaller u = jc.createunmarshaller();     u.unmarshal(new domsource(nodelist.item(0)),  cwetype.class); 

however, if try use concept of spring-oxm unmarshaller, error.

    jaxb2marshaller jaxb2marshaller = new jaxb2marshaller();     jaxb2marshaller.setclassestobebound(cwetype.class);     jaxb2marshaller.unmarshal(new domsource(nodelist.item(0)));    org.springframework.oxm.unmarshallingfailureexception: jaxb unmarshalling exception; nested exception javax.xml.bind.unmarshalexception: unexpected element (uri:"", local:"cwe"). expected elements (none)     @ org.springframework.oxm.jaxb.jaxb2marshaller.convertjaxbexception(jaxb2marshaller.java:911)     @ org.springframework.oxm.jaxb.jaxb2marshaller.unmarshal(jaxb2marshaller.java:784)     @ org.springframework.oxm.jaxb.jaxb2marshaller.unmarshal(jaxb2marshaller.java:753) 

@m.deinum suggested in comments try xpath, have not feared better - throwing same error @ unmarshal time:

   xpath xpath = xpathfactory.newinstance().newxpath();     nodelist xpnl = (nodelist)xpath.compile("//cwe").evaluate(doc, xpathconstants.nodeset);     jaxb2marshaller.unmarshal(new domsource(xpnl.item(0))); 

what doing wrong? there wrong way creating domsource()? why able unmarshal using jaxbunmarshaller directly, not using spring wrapper? there anyway explicitly declare via spring-oxm unmarshaller declaredtype?

cwetype.java:

@xmlaccessortype(xmlaccesstype.field) @xmltype(name = "cwetype", proporder = {     "description",     "staticflaws",     "dynamicflaws",     "manualflaws" }) public class cwetype {      @xmlelement(required = true)     protected cwetype.description description;     protected flawlisttype staticflaws;     protected flawlisttype dynamicflaws;     protected flawlisttype manualflaws;     @xmlattribute(name = "cweid", required = true)     @xmlschematype(name = "positiveinteger")     protected biginteger cweid;     ...     .... 

 public static cwetype unmarshal(domsource node) throws jaxbexception {     jaxbcontext jaxbcontext = jaxbcontext.newinstance(cwetype.class);     unmarshaller jaxbunmarshaller = jaxbcontext.createunmarshaller();      jaxbelement<cwetype> root = jaxbunmarshaller.unmarshal(node, cwetype.class);     cwetype cwetype= root.getvalue();      logger.info(cwetype.tostring());     return cwetype; }  documentbuilderfactory dbf = documentbuilderfactory.newinstance(); documentbuilder db = dbf.newdocumentbuilder();  document doc = db.parse(ioutils.toinputstream(xml)); nodelist nodelist = doc.getelementsbytagname("cwe"); cwetype type = unmarshal(new domsource(nodelist.item(0)); 

i hope can helpful !


Comments