gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:55:04 27 4
gpt4 key购买 nike

这与 my prior question 相关一般来说,这更针对 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 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 解码器显式声明声明的类型?

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

希望这对您有所帮助!

关于java - spring-oxm:我可以解码文件的子元素吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32788288/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com