尝试使用jaxb解组xml

时间:2013-01-29 11:38:14

标签: java eclipse xsd jaxb

我已经提供了一个响应消息的xsd,并且我试图将xml响应解析为jaxb生成的类。我首先遇到的问题是根xml元素被称为“响应”,但是还有一个名为“响应”的嵌套类,因此会出现编译错误。为了解决这个问题,我发现在xsd中我可以使用jaxb:class annotation来更改嵌套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 个答案:

答案 0 :(得分: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);
    }

}

<强> input.xml中/输出

可以生成/使用以下XML。

<?xml version="1.0" encoding="UTF-8"?>
<response xmlns="forum14582017">
    <callReport7>
        <response time="07:47"/>
    </callReport7>
</response>