I am trying to create application which parses xml file and also trying to do unmarshalling to create POJO's (objects) for this xml and print it to console (toString). This is xml I am working with its from stackoverflow:
<?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>
I have also made setters and getters for them:
@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 +
'}';
}
}
Also I have made configuration file for this to work and I am using spring integration for this.
<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"/>
But when I run code I get some errors with 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)
I was trying to fix it by myself but I cant find a way how to fix this and what do I need to change here... I would appreciate some help. I tried deleting uri from my xml file but it still gave me the same error.