简体   繁体   English

spring-oxm:我可以解组文件的子元素吗?

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

This is related to my prior question which was more directed towards JAXB in general. 这与我先前的问题有关,该问题通常更针对于JAXB。 But this question is more related specifically to the unmarshaller in spring-oxm . 但是这个问题更具体地与spring-oxm的解组器有关。 I'm looking to see if I can use the spring-oxm unmarshaller to unmarshal only specific elements from my XML. 我正在寻找是否可以使用spring-oxm解组器来解组XML中的特定元素。

My XSD is: 我的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>

Using some preprocessing, I can find all Nodes of type "cwe": 使用一些预处理,我可以找到所有类型为“ cwe”的节点:

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

Using a JAXBUnmarshaller, I can manage to unmarshal my object: 使用JAXBUnmarshaller,我可以设法解组我的对象:

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

However, if I try to use the concept of spring-oxm unmarshaller, I get an error. 但是,如果尝试使用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 suggested in the comments to try XPath, but I have not feared any better - throwing the same error at unmarshal time: @ 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)));

What am I doing wrong? 我究竟做错了什么? Is there something wrong with the way I am creating my DOMSource()? 我创建DOMSource()的方式有问题吗? Why am I able to unmarshal using the JAXBUnmarshaller directly, but not using the Spring wrapper? 为什么我可以直接使用JAXBUnmarshaller而不是使用Spring包装器来解组? Is there anyway to explicitly declare via the spring-oxm unmarshaller the declaredType? 无论如何,通过spring-oxm unmarshaller明确声明了clarifiedType?

CweType.java: 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 this can be helpful ! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM