使用 spring 集成对 xml 进行 JAXB 解组

JAXB unmarshalling with xml using spring integration

我正在尝试创建解析 xml 文件的应用程序,还尝试进行解组以为此 xml 创建 POJO(对象)并将其打印到控制台 (toString)。这是 xml 我正在使用来自 Whosebug 的它:

<?xml version="1.0" encoding="utf-8"?>
<feed feedid="5151"
      xmlns="http://www.w3.org/2005/Atom">
<title type="text">How to get the feed URL(s) from a website? - Stack Overflow</title>
<subtitle>most recent 30 from whosebug.com</subtitle>
<updated>2020-11-29T17:18:01Z</updated>
<id>https://whosebug.com/feeds/question/49479712</id>
<entry>
    <id>
    <title type="text">How to get the feed URL(s) from a website?</title>
    <name>yPhil</name>
    <published>2018-03-25T18:58:26Z</published>
    <updated>2018-05-21T19:39:17Z</updated>
</entry>
<entry>
    <id>
    <title type="text">Answer by Quentin for How to get the feed URL(s) from a website?</title>
    <name>Quentin</name>
    <published>2018-03-25T19:01:00Z</published>
    <updated>2018-03-25T19:01:00Z</updated>
</entry>
</feed>

我也为他们制作了setter和getter:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "id", "title", "subtitle", "updated", "entry" })
@XmlRootElement(name="feed")
public class Feed {

    @XmlElement(required = true)
    protected int id;
    @XmlElement(required = true)
    protected String title;
    @XmlElement(required = true)
    protected String subtitle;
    @XmlElement(required = true)
    protected String updated;
    @XmlElement(required = true)
    protected List<Entry> entry;

   ...Setters and getters...

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = { "id", "title", "name", "published", "updated" })
    public static class Entry{

        @XmlElement(required = true)
        protected String id;
        @XmlElement(required = true)
        protected String title;
        @XmlElement(required = true)
        protected String name;
        @XmlElement(required = true)
        protected String published;
        @XmlElement(required = true)
        protected String updated;

        ...Setters and getters...

        @Override
        public String toString() {
            return "Entry{" +
                    "entryId='" + id + '\'' +
                    ", title='" + title + '\'' +
                    ", name='" + name + '\'' +
                    ", published='" + published + '\'' +
                    ", entryUpdated='" + updated + '\'' +
                    '}';
        }
    }
    @Override
    public String toString() {
       return "Feed{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", subtitle='" + subtitle + '\'' +
                ", updated='" + updated + '\'' +
                ", entry=" + entry +
                '}';
    }
}

我还为此制作了配置文件,我正在为此使用 spring 集成。

 <int-file:inbound-channel-adapter id="file-producer" channel="inboundChannel"
                                      directory="src/main/resources/xmlfeed" prevent-duplicates="true">
        <int:poller fixed-rate="5000"/>
    </int-file:inbound-channel-adapter>

    <int:channel id="inboundChannel"/>


    <int-file:file-to-string-transformer id="file-2-string" input-channel="inboundChannel"
                                         output-channel="xml-inboundChannel" charset="UTF-8"/>

    <int:channel id="xml-inboundChannel"/>
    
    <int-xml:unmarshalling-transformer id="xml-2-object" input-channel="xml-inboundChannel"
                                       output-channel="outboundChannel" unmarshaller="jaxbMarshaller"/>

    <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="contextPath" value="com.xml.domain" />
     </bean>

    <int:channel id="outboundChannel"/>

    <int:service-activator id="printing" input-channel="outboundChannel"
                           ref="serviceActivator"/>
    <bean id="serviceActivator" class="com.xml.Dispatcher"/>

但是当我 运行 编码时,我在使用 JAXB Unmarshalling 时遇到了一些错误:

JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.w3.org/2005/Atom", local:"feed"). Expected elements are (none)

我试图自己修复它,但我找不到解决此问题的方法以及我需要在此处更改什么...我将不胜感激。我尝试从我的 xml 文件中删除 uri,但它仍然给我同样的错误。

@XmlRootElement 上看到一个 namespace 选项。

所以,它可能看起来像这样:

@XmlRootElement(name="feed", namespace="http://www.w3.org/2005/Atom")

另一方面,我们已经有一个 spring-integration-feed 模块来完成您试图手动完成的工作:https://docs.spring.io/spring-integration/docs/current/reference/html/feed.html#feed