有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何解组单个字符串元素(jaxb)?

这是我的xsd方案:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://gov.com/oos/types/1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="id" type="xs:int"/>
  <xs:element name="sum" type="xs:int"/>
  <xs:element name="docPublishDate" type="xs:dateTime"/>
  <xs:element name="href" type="xs:anyURI"/>
  <xs:element name="printForm">
    <xs:complexType>
      <xs:sequence>
        <xs:element type="xs:anyURI" name="url"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

我怎样才能得到一个名为“id”的元素?()

//我已经从xsd生成了类

JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            JAXBElement<int> jaxbElement = unmarshaller.unmarshal(source, ...

我不知道下一步该怎么办?如何获取基元类型值。任何地方都给我一个指令如何得到复杂类型的类?但不是像int或string这样的单个元素。请帮忙


共 (1) 个答案

  1. # 1 楼答案

    ObjectFactory中,可能有idJAXBElement<Integer>或类似的映射

    所以你应该能够

    JAXBElement<Integer> idElement = unmarshaller.unmarshal(source, ...);
    

    然后

    Integer id = idElement.getValue();