试图使用JAXB解组XML

3

我收到了一个响应消息的xsd,正在尝试将xml响应解析成jaxb生成的类。一开始我遇到了一个问题,根元素被称为"response",但也有一个嵌套类名为"response",这导致编译错误。为了解决这个问题,我发现在xsd中可以使用jaxb:class注释来更改创建的嵌套java类的名称。下面是嵌套类现在生成为“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" 解析为 "response"。

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();
        }
1个回答

2

schema.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 version="1.0" encoding="UTF-8"?>
<response xmlns="forum14582017">
    <callReport7>
        <response time="07:47"/>
    </callReport7>
</response>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接