jaxb与xml解组

l7wslrjt  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(339)

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

<?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 stackoverflow.com</subtitle>
<updated>2020-11-29T17:18:01Z</updated>
<id>https://stackoverflow.com/feeds/question/49479712</id>
<entry>
    <id>https://stackoverflow.com/q/49479712</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>https://stackoverflow.com/questions/49479712/-/49479747#49479747</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>

我也为他们做了二传手和传手:

@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解组错误:
jaxb解编异常;嵌套异常为javax.xml.bind.unmarshalexception:意外元素(uri:)http://www.w3.org/2005/atom,本地:“feed”)。预期元素为(无)
我试图解决它自己,但我找不到一个方法来解决这个问题,我需要改变什么在这里。。。我会很感激你的帮助。我试图从我的xml文件中删除uri,但它仍然给我同样的错误。

svmlkihl

svmlkihl1#

namespace 上的选项 @XmlRootElement .
所以,可能是这样的:

@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

相关问题