我正在尝试创建解析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 Unmarshalling出现一些错误:
JAXB解组异常;嵌套的异常是javax.xml.bind.UnmarshalException:意外元素(uri:“ http://www.w3.org/2005/Atom”,本地:“ feed”)。预期元素为(无)
我正在尝试自己修复它,但找不到解决方法以及在此需要更改的方法...我将不胜感激。我尝试从xml文件中删除uri,但仍然给了我同样的错误。
请参阅上的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
我只想手动进行操作,以更好地实践它。将该名称空间添加到@XmlRootElement仍然无法获得相同的错误
好的。尝试添加
namespace
的@XmlType
。另一方面,请参见JaxB生成工具。因此,您只需为其提供http://www.w3.org/2005/Atom
XSD并返回POJO:stackoverflow.com/questions/11463231/…