3

我收到了一条响应消息的 xsd,我正在尝试将 xml 响应解析为 jaxb 生成的类。我首先遇到了一个问题,即根 xml 元素被称为“response”,但还有一个名为“response”的嵌套类,因此出现编译错误。为了解决这个问题,我发现在 xsd 中我可以使用 jaxb:class 注释来更改嵌套类的名称,该类创建如下,嵌套类现在生成为“callReport7Response”而不是“response”。

<xs:element name="callReport7" minOccurs="0">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="request">
            <xs:annotation><xs:appinfo><jaxb:class name="callReport7Request"/></xs:appinfo></xs:annotation>
                <xs:complexType>
                    <xs:complexContent>
                        <xs:extension base="xs:anyType">
                            <xs:attribute name="time" type="xs:string"/>
                        </xs:extension>
                    </xs:complexContent>
                </xs:complexType>
            </xs:element>
            <xs:element name="response">
            <xs:annotation><xs:appinfo><jaxb:class name="callReport7Response"/></xs:appinfo></xs:annotation>
                <xs:complexType>
                    <xs:complexContent>
                        <xs:extension base="xs:anyType">
                            <xs:attribute name="time" type="xs:string"/>
                        </xs:extension>
                    </xs:complexContent>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

不幸的是,当我尝试解组响应时,我收到错误消息说它无法将“callReport7Response”解析为“响应”

java看起来像这样:

        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = {"any"})
        @XmlRootElement(name = "response")
        public static class CallReport7Response {

            @XmlAnyElement
            protected List<Element> any;
            @XmlAttribute
            protected String time;

似乎这就是它试图将我的嵌套对象转换为顶级对象。

09:28:34,608 ERROR [STDERR] java.lang.ClassCastException: uk.co.test.dashboard.dal.Response$Insurer$Subject$CallReport7$CallReport7Response cannot be cast to uk.co.test.dashboard.dal.Response

我正在使用此代码解组:

Response response = new Response();
        StringReader reader = new StringReader(resp);
        try {
            JAXBContext context = JAXBContext.newInstance(response.getClass());
            Unmarshaller unmarshaller = context.createUnmarshaller();
            Object o = unmarshaller.unmarshal(reader);
            response = (Response) o;
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
4

1 回答 1

2

架构.xsd

您应该将 JAXB 模式注释从嵌套response元素移至其对应的复杂类型。以下是基于您在问题中描述的简化 XML 架构。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="forum14582017" 
    xmlns="forum14582017" 
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    elementFormDefault="qualified" 
    jaxb:version="2.1">

    <xs:element name="response">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="callReport7">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="response">
                                <xs:complexType>
                                    <xs:annotation>
                                        <xs:appinfo>
                                            <jaxb:class name="callReport7Response" />
                                        </xs:appinfo>
                                    </xs:annotation>
                                    <xs:complexContent>
                                        <xs:extension base="xs:anyType">
                                            <xs:attribute name="time" type="xs:string" />
                                        </xs:extension>
                                    </xs:complexContent>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

回复

将生成如下所示的类(已删除注释和访问器以节省空间)。

package forum14582017;

import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;
import org.w3c.dom.Element;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"callReport7"})
@XmlRootElement(name = "response")
public class Response {

    @XmlElement(required = true)
    protected Response.CallReport7 callReport7;

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {"response"})
    public static class CallReport7 {

        @XmlElement(required = true)
        protected Response.CallReport7 .CallReport7Response response;

        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = {"any"})
        public static class CallReport7Response {

            @XmlAnyElement
            protected List<Element> any;
            @XmlAttribute(name = "time")
            protected String time;
            @XmlAnyAttribute
            private Map<QName, String> otherAttributes = new HashMap<QName, String>();

        }

    }

}

演示

使用下面的演示代码和从上面的 XML 模式生成的 JAXB 模型。

package forum14582017;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance("forum14582017");

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum14582017/input.xml");
        Response response = (Response) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(response, System.out);
    }

}

输入.xml/输出

可以生成/使用以下 XML。

<?xml version="1.0" encoding="UTF-8"?>
<response xmlns="forum14582017">
    <callReport7>
        <response time="07:47"/>
    </callReport7>
</response>
于 2013-01-30T11:14:48.717 回答