簡體   English   中英

spring-oxm:我可以解組文件的子元素嗎?

[英]spring-oxm: Can I unmarshall a subelement of a file?

這與我先前的問題有關,該問題通常更針對於JAXB。 但是這個問題更具體地與spring-oxm的解組器有關。 我正在尋找是否可以使用spring-oxm解組器來解組XML中的特定元素。

我的XSD是:

<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>

使用一些預處理,我可以找到所有類型為“ cwe”的節點:

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder(); 
    Document doc = db.parse(IOUtils.toInputStream(xml));
    NodeList nodeList = doc.getElementsByTagName("cwe");

使用JAXBUnmarshaller,我可以設法解組我的對象:

    JAXBContext jc = JAXBContext.newInstance( CweType.class );
    Unmarshaller u = jc.createUnmarshaller();
    u.unmarshal(new DOMSource(nodeList.item(0)),  CweType.class);

但是,如果嘗試使用spring-oxm解組器的概念,則會出現錯誤。

    Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
    jaxb2Marshaller.setClassesToBeBound(CweType.class);
    jaxb2Marshaller.unmarshal(new DOMSource(nodeList.item(0)));



org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"cwe"). Expected elements are (none)
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.convertJaxbException(Jaxb2Marshaller.java:911)
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:784)
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:753)

@ M.Deinum在評論中建議嘗試XPath,但我並不擔心會更好-在解組時拋出相同的錯誤:

   XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList xpnl = (NodeList)xPath.compile("//cwe").evaluate(doc, XPathConstants.NODESET);
    jaxb2Marshaller.unmarshal(new DOMSource(xpnl.item(0)));

我究竟做錯了什么? 我創建DOMSource()的方式有問題嗎? 為什么我可以直接使用JAXBUnmarshaller而不是使用Spring包裝器來解組? 無論如何,通過spring-oxm unmarshaller明確聲明了clarifiedType?

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));

希望對您有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM