gpt4 book ai didi

javax.xml.bind.UnmarshalException : unexpected element (uri:

转载 作者:行者123 更新时间:2023-12-04 12:25:20 27 4
gpt4 key购买 nike

尝试使用 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();

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

最佳答案

您可以使用 @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;

更多信息
  • http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
  • 关于javax.xml.bind.UnmarshalException : unexpected element (uri:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12293134/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com