【问题标题】:JAXB: Is there a way to unmarshal only specific paths in an XML?JAXB:有没有办法只解组 XML 中的特定路径?
【发布时间】:2025-12-13 15:50:02
【问题描述】:

我有一个 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:complexType name="FlawType">
  <xs:sequence>
    <xs:element name="mitigations" minOccurs="0" maxOccurs="1" type="tns:MitigationListType" />
    <xs:element name="exploit_desc" type="tns:LongTextType" minOccurs="0" maxOccurs="1"/>
  </xs:sequence>
</xs:complexType>


<xs:complexType name="MitigationListType">
  <xs:sequence>
    <xs:element name="mitigation" minOccurs="0" maxOccurs="unbounded" type="tns:MitigationType"/>
  </xs:sequence>
</xs:complexType>

<xs:complexType name="MitigationType">
  <xs:attribute name="action" type="xs:string" use="required"/>
  <xs:attribute name="description" type="xs:string" use="required"/>
  <xs:attribute name="user" type="xs:string" use="required"/>
  <xs:attribute name="date" type="xs:string" use="required"/>
</xs:complexType>

</xs:schema>

我希望仅将 complexType FlawType 导入列表。我想我可能可以使用 Apache Digester 来执行此操作,但想知道是否有某种方法可以使用 JAXB 来执行此操作。直接解组到 detailedreport 对象,然后使用循环提取 FlawType 是可行的,但似乎需要做很多额外的工作。

从本质上讲,我希望能够提出一个解决方案,例如:

   String xml = FileUtils.readFileToString( XML_File );
   unmarshaller = JAXBContext.createUnmarshaller();
   // only unmarhsal nodes of FlawType.class from the xml file.
   List<FlawType> flawTypes = unmarshaller.unmarshal( xml, FlawType.class );

我可以将整个 XML 文件加载到 DOM 对象中,然后使用 XPath 之类的东西来定位所有单独的 FlawType 节点,对于每个节点,使用 Unmarshaller 为每个节点执行此操作,但不知道如果有更简单的方法。我想我也可以使用某种形式的 SAX 解析器(我从未使用过它们),但希望能更直接一些。

我实际上是在使用带有 spring-oxm 包的 Spring 4 框架来为我处理大量的 JAXB 工作,因此我很想找到一个易于理解和可维护的简单解决方案。使用 Digester 之类的东西只会为我的堆栈添加更多技术,而我宁愿避免这样做。

有没有一种简单的方法可以用 JAXB 做到这一点,或者这超出了 JAXB 的范围?

【问题讨论】:

  • 这些真的是彼此的孩子吗?我没有看到 extension 使用的属性。
  • @Keith 是的——你可以看到结构是:detailedreport.severity.category.cwe.staticflaws.flaw
  • 当然——我想我在组合与继承的语义上分心了。让我想一想,但我不确定你是否能如愿以偿。您能否在您的问题中添加一些伪代码来帮助可视化您希望看到的情况?
  • @Keith 我已经更新了这个问题,提供了更多详细信息,表明我想要实现的目标。

标签: java xml spring jaxb spring-oxm


【解决方案1】:

我已经设法找到以下解决方案,但不认为这是最漂亮的:

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

JAXBContext jc = JAXBContext.newInstance( CweType.class );
Unmarshaller u = jc.createUnmarshaller();

List<CweType> cwes = new ArrayList<>();
for( int i = 0; i < nodeList.getLength(); i++ )
    cwes.add( u.unmarshal(nodeList.item(i),  CweType.class);

我希望有一些更整洁的东西。对于初学者,我不喜欢必须手动搜索名为 cwe 的元素的想法。至少,我希望能够从生成的 CweType 类或 CategoryType 类中获取元素名称,但我能看到的唯一方法是反射。这是唯一的方法吗?

【讨论】:

    最近更新 更多