spring-oxm: могу ли я демаршировать подэлемент файла?
Это связано с моим предыдущим вопросом, который в большей степени был направлен на JAXB. Но этот вопрос в большей степени относится именно к spring-oxm
, Я смотрю, могу ли я использовать unmarshaller 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);
Однако, если я пытаюсь использовать концепцию unmarshaller 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-упаковщик? Есть ли способ явно объявить через unmarshaller Spring-Oxm объявленный тип?
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;
...
....
1 ответ
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));
Я надеюсь, что это может быть полезно!