6

尝试使用 jaxb 将 xml 响应转换为 java 对象时出现以下错误

       javax.xml.bind.UnmarshalException: unexpected element (uri:"http://SOMETHING/doc/2006-03-01/", local:"Name"). Expected elements are <{}Name>,<{}IsTruncated>,<{}MaxKeys>,<{}Contents>,<{}Prefix>,<{}Marker>

这是我的 XML:

       <ListBucketResult xmlns="http://something/doc/2006-03-01/">
           <Name>test2</Name>
            <Prefix/>
            <Marker/>
            <MaxKeys>3</MaxKeys>
            <IsTruncated>false</IsTruncated>
            <Contents>
                  <Key>metadata.xml</Key>
                  <LastModified>2012-09-04T08:29:36.000Z</LastModified>
                    <ETag>6b836fd43c402681506926b2248ec418</ETag>
                    <Size>3258</Size>
                   <StorageClass>STANDARD</StorageClass>
             </Contents>
          </ListBucketResult>

我的java对象类是这样的

            @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "name",
        "prefix",
        "marker",
        "maxKeys",
        "isTruncated",
        "contents"
    })
    @XmlRootElement(name = "ListBucketResult")
    public class ListBucketResult {

@XmlElement(name = "Name", required = true)
protected String name;
@XmlElement(name = "Prefix", required = true)
protected String prefix;
@XmlElement(name = "Marker", required = true)
protected String marker;
@XmlElement(name = "MaxKeys")
protected int maxKeys;
@XmlElement(name = "IsTruncated")
protected boolean isTruncated;
@XmlElement(name = "Contents", required = true)
protected ListBucketResult.Contents contents;







         @XmlAccessorType(XmlAccessType.FIELD)
         @XmlType(name = "", propOrder = {
                "key",
                "lastModified",
                   "eTag",
                   "size",
                 "storageClass"
           })
          public static class Contents {

    @XmlElement(name = "Key", required = true)
    protected String key;
    @XmlElement(name = "LastModified", required = true)
    @XmlSchemaType(name = "dateTime")
    protected XMLGregorianCalendar lastModified;
    @XmlElement(name = "ETag", required = true)
    protected String eTag;
    @XmlElement(name = "Size")
    protected int size;
    @XmlElement(name = "StorageClass", required = true)
    protected String storageClass;

最后我的解组代码是:

                          JAXBContext jc = JAXBContext.newInstance(ListBucketResult.class);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
               JAXBElement element = (JAXBElement) unmarshaller.unmarshal (inputStream);
            ListBucketResult customer = (ListBucketResult) element.getValue();

有人可以让我知道我做错了什么吗?

4

3 回答 3

5

您可以使用类@XmlSchema上的注释package-info来控制命名空间限定。如果您已经编写了 package-info 类,请确保它正在被编译(某些版本的 ant 与 package-info 类有问题)。

包信息

@XmlSchema( 
    namespace = "http://something/doc/2006-03-01/", 
    elementFormDefault = XmlNsForm.QUALIFIED) 
package example;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

了解更多信息

于 2012-09-06T09:02:22.567 回答
3

XML 文档包含属于“http://something/doc/2006-03-01/”命名空间的元素,但带注释的 java 类与该命名空间无关。尝试将 @XmlType 注释更改为:

@XmlType(name = "", namespace="http://something/doc/2006-03-01/", propOrder = { ...
于 2012-09-06T04:57:15.420 回答
0

我收到了类似的错误org.codehaus.jackson.JsonParseExceptionjavax.xml.bind.UnmarshalException:带有链接异常:[ javax.xml.bind.UnmarshalException:意外元素(uri:“sometext”,local:“elementA”)。预期的元素是.

这是将RESTEasySpring与我使用JAXB生成的 DTO集成时。我正在使用杰克逊进行转换。

为了解决这个问题,我在 Maven 依赖项中引入了 Jackson 库,它解决了我的问题。特定于此的杰克逊依赖项是

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-xc</artifactId>
        <version>1.9.9</version>
    </dependency>

我还需要其他杰克逊罐子,所以我的 POM 基本上如下所示。

   //The jackson-core-asl jar contains streaming JSON parser and generator interfaces and implementations
<dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.9</version>
    </dependency>
//Ability to use JAXB annotations containing classes needed to add XML compatibility support.
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-xc</artifactId>
        <version>1.9.9</version>
    </dependency>
    //Mapper jar contains functionality for data binding: 
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.9</version>
   </dependency> //This dependency makes a JAX-RS implementation like Jersey,RESTEasy use Jackson for binding of JSON to-from Java objects
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-jaxrs</artifactId>
        <version>1.9.9</version>
    </dependency>
于 2012-12-26T04:38:54.680 回答