使用JAXB的具有名称空间绑定的Java feed XML

时间:2019-06-25 10:57:43

标签: java spring xml-parsing jaxb resttemplate

我正在尝试在下面的示例xml文件中绑定电子邮件元素,但无法正确绑定它。我变得空了。

示例XML

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:api="http://www.example.com/publications/api">
<api:schema-version>5.3</api:schema-version>
<category scheme="http://www.example.com/publications/atom/feeds/" term="item" label="Item" />
<id>tag:elements@abcd,5.15:/proted-api/v5.5/feeds/users/12312</id>
  <entry>
   <id>tag:elements@abcd,5.15:/proted-api/v5.5/users/12312</id>
   <category scheme="http://www.example.com/publications/atom/entries/" term="item" label="Item" />
   <content type="xhtml">
   <api:object category="user" id="12312" proprietary-id="abcd123">
      <api:last-name>Jo</api:last-name>
      <api:first-name>Deo</api:first-name>
      <api:email-address>jode@example.com</api:email-address>
    </api:object>
  </entry>
</feed>

Feed.java

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Feed {

@XmlElement(name="entry")
private Entry entry;

public Entry getEntry() {
    return entry;
}

public void setEntry(Entry entry) {
    this.entry = entry;
}
}

Entry.java

@XmlAccessorType(XmlAccessType.FIELD)
public class Entry {

@XmlElement(name="object", namespace = "http://www.example.com/publications/api")
private Object object;

public Object getObject() {
    return object;
}

public void setObject(Object object) {
    this.object = object;
}
}

Object.java

@XmlAccessorType(XmlAccessType.FIELD)
public class Object {

@XmlElement(name="email-address",namespace = "http://www.example.com/publications/api")
private String email;

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

}

测试代码

//code here to read
Feed feed = response.getBody();
System.out.println("object email = " +feed.getEntry().getObject().getEmail());

完整的XML文件

    <feed xmlns="http://www.w3.org/2005/Atom" xmlns:api="http://www.example.com/publications/api">
    <api:schema-version>5.5</api:schema-version>
    <category scheme="http://www.example.com/publications/atom/feeds/" term="item" label="Item" />
    <id>tag:elements@Test,5.15:/secure-api/v5.5/feeds/users/676</id>
    <updated>2019-06-17T09:04:39.87+01:00</updated>
    <generator uri="https://test.com/" version="5.15">Example Elements</generator>
    <icon>https://test.com:8091/secure-api/v5.5/Example.ico</icon>
    <rights>This data is the property of the Organisation, and can only be used with permission.</rights>
    <subtitle>This feed represents a single user.</subtitle>
    <link type="application/atom+xml" rel="self" href="https://test.com:8091/secure-api/v5.5/users/676" />
    <title>John deo</title>
    <author>
    <name>Example Elements at Test PROD</name>
    </author>
    <entry>
    <id>tag:elements@Test,5.15:/secure-api/v5.5/users/676</id>
    <category scheme="http://www.example.com/publications/atom/entries/" term="item" label="Item" />
    <updated>2019-06-17T09:04:39.87+01:00</updated>
    <link type="application/atom+xml" rel="alternate" href="https://test.com:8091/secure-api/v5.5/users/676" />
    <title>John deo</title>
    <content type="xhtml">
    <div xmlns="http://www.w3.org/1999/xhtml">
    <p>User</p>
    <a href="https://test.com:8091/secure-api/v5.5/users/676/photo?type=profile">Photo</a>
    <p>
    <a href="https://test.com:8091/secure-api/v5.5/users/676/relationships">Relationships</a>
    with other data
    </p>
    </div>
    </content>
    <api:object category="user" id="676" proprietary-id="abcd1247" username="abcd1247" last-affected-when="2019-06-17T09:04:39.87+01:00" last-modified-when="2018-11-29T10:28:43.403+00:00" href="https://test.com:8091/secure-api/v5.5/users/676" created-when="2010-05-04T09:49:46.507+01:00" type-id="1" type="person">
    <!-- User type 1 is "person" -->
    <api:ever-approved>true</api:ever-approved>
    <api:is-public>false</api:is-public>
    <api:is-login-allowed>true</api:is-login-allowed>
    <api:title>Prof</api:title>
    <api:initials>Jo</api:initials>
    <api:last-name>John</api:last-name>
    <api:first-name>Deo</api:first-name>
    <api:email-address>John.deo@example.com</api:email-address>
    <api:known-as>Liz</api:known-as>
    <api:primary-group-descriptor>ABCS</api:primary-group-descriptor>
    <api:arrive-date>2009-10-05</api:arrive-date>
    <api:user-search-settings>
    <api:default>
    </api:default>
    </api:user-search-settings>
    <api:records>
    <api:record format="native" id="3245433412" source-id="1" source-name="manual" source-display-name="Manual">
    <api:native />
    </api:record>
    </api:records>
    <api:fields />
    <api:relationships href="https://test.com:8091/secure-api/v5.5/users/676/relationships" />
    <api:user-identifier-associations user-id="676" proprietary-id="abcd1247" username="abcd1247">
    </api:user-identifier-associations>
    </api:object>
    </entry>
    </feed>

1 个答案:

答案 0 :(得分:2)

问题在于您的POJOs结构与xml不匹配(并且正如我们最后发现的那样,RestTemplate没有利用JAXB注释,请参见下面的Edit 4)。

请修复此问题,以便您可以正确地解组编组。

大多数字段都使用http://www.w3.org/2005/Atom命名空间。我们将使用package-info而不是将其添加到每个元素中。因此,在包含内容的类的包中创建一个文件package-info.java:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.w3.org/2005/Atom", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package the.package.of.your.classes;

然后让我们修复提要。您的xml具有您的类没有的元素,这是一个问题,因为它遇到了意外的元素。提要是:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Feed {

    @XmlElement(name="schema-version", namespace = "http://www.example.com/publications/api")
    private String schemaVersion;
    @XmlElement
    private String category;
    @XmlElement
    private String id;

    @XmlElement(name="entry")
    private Entry entry;

}

请注意,架构版本的名称空间与package-info中的名称空间不同,因此我们在注释中显式覆盖它。

现在,这已简化。例如,类别具有xml属性,例如术语和标签。如果要获取此信息,则应创建一个类来表示Category而不是String。但是,如果您不这样做,则像这样的Feed可以使我们继续进行编组。接下来是条目:

@XmlAccessorType(XmlAccessType.FIELD)
public class Entry {


    @XmlElement
    private String id;
    @XmlElement
    private String category;
    @XmlElement
    private String content;

    @XmlElement(name="object", namespace = "http://www.example.com/publications/api")
    private ApiObject object;

}

此处的使用String再次忽略属性。我将Object更改为名为ApiObject的类,该类具有与xml匹配的结构。看起来像这样:

@XmlAccessorType(XmlAccessType.FIELD)
public class ApiObject {

    @XmlElement(name= "last-name", namespace = "http://www.example.com/publications/api")
    private String lastName;
    @XmlElement(name = "first-name", namespace = "http://www.example.com/publications/api")
    private String firstName;
    @XmlElement(name = "email-address", namespace = "http://www.example.com/publications/api")
    private String emailAddress;
}

最后是xml。这是无效的xml,在粘贴的示例中未关闭标签“ content”。我将其更改为<content type="xhtml" />以测试我的解决方案。

实际上,它可以正常工作,并且可以进行编组:)

编辑以回复:

对于问题1,您应该在每个现在不直接指定名称空间的元素中添加名称空间。

对于问题2,您可以使用此处建议的JAXB Ignore 'extra' elements from Response XML

忽略未知元素。

例如,饲料看起来像:

@XmlRootElement(namespace = "http://www.w3.org/2005/Atom")
@XmlAccessorType(XmlAccessType.FIELD)
public class Feed {

    @XmlAnyElement(lax = true)
    private List<Object> anything;

    @XmlElement(name="entry", namespace = "http://www.w3.org/2005/Atom")
    private Entry entry;


}

编辑2:

以您尝试解组的xml格式执行以下操作:

try {
            File file = new File("/path/to/your/file.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(YouRootClass.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            jaxbUnmarshaller.setEventHandler(
                    new ValidationEventHandler() {
                        public boolean handleEvent(ValidationEvent event ) {
                            throw new RuntimeException(event.getMessage(),
                                    event.getLinkedException());
                        }
                    });
            YouRootClass pojo = (YouRootClass) jaxbUnmarshaller.unmarshal(file);
        } catch (JAXBException e) {
            e.printStackTrace();
        }

无论您的xml与POJO之间存在什么不匹配,都将作为异常抛出,您可以尝试对其进行纠正

编辑3:

Feed

@XmlRootElement(namespace = "http://www.w3.org/2005/Atom")
@XmlAccessorType(XmlAccessType.FIELD)
public class Feed {

    @XmlAnyElement(lax = true)
    private List<Object> anything;

    @XmlElement(name="entry", namespace = "http://www.w3.org/2005/Atom")
    private Entry entry;

}

条目

@XmlAccessorType(XmlAccessType.FIELD)
public class Entry {


    @XmlAnyElement(lax = true)
    private List<Object> anything;

    @XmlElement(name="object", namespace = "http://www.example.com/publications/api")
    private ApiObject object;

}

ApiObject:

@XmlAccessorType(XmlAccessType.FIELD)
public class ApiObject {

    @XmlAnyElement(lax = true)
    private List<Object> anything;

    @XmlElement(name= "last-name", namespace = "http://www.example.com/publications/api")
    private String lastName;
    @XmlElement(name = "first-name", namespace = "http://www.example.com/publications/api")
    private String firstName;
    @XmlElement(name = "email-address", namespace = "http://www.example.com/publications/api")
    private String emailAddress;
}

编辑4:

在您的github项目中,创建RestTemplate之后的Test类(第31行)添加:

List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
Jaxb2RootElementHttpMessageConverter jaxb2RootElementHttpMessageConverter = new Jaxb2RootElementHttpMessageConverter();
messageConverters.add(jaxb2RootElementHttpMessageConverter);
rest.setMessageConverters(messageConverters);