XSD:java对象的anyType

时间:2016-10-31 08:29:47

标签: java xsd

我有一个xsd如下:

<xs:complexType name="exampleDataType">
    <xs:sequence>
      <xs:element name="payload" type="xs:anyType" minOccurs="0"/>
      <xs:element name="message" type="tns:message" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

但是,当我尝试从java获取有效负载值时,它返回null。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

我最近也遇到了这个问题,下面是解决方法:

XSD:

Java代码:

ClassLoader classLoader = new XmlToObject().getClass().getClassLoader();

File file = new File(classLoader.getResource("myXmlFile.xml").getFile());

JAXBContext jaxbContext = JAXBContext.newInstance(MyPojo.class);

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

MyPojo myPojo = (MyPojo) jaxbUnmarshaller.unmarshal(file);

Object check = myPojo.getCheck();

if (check instanceof Node) {
    ElementNSImpl checkElement = (ElementNSImpl) check;
    System.out.println("check=" + checkElement.getTextContent());
}