これは、一般的にJAXBに向けられた私の以前の質問に関連しています。しかし、この質問は、特に のアンマーシャラーに関連していspring-oxm
ます。spring-oxm unmarshaller を使用して、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 unmarshaller の概念を使用しようとすると、エラーが発生します。
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 を介して definedType を明示的に宣言する方法はありますか?
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;
...
....