java


JAXB ClassCastException due to root element having the same tag as its child element


I am unmarshalling an XML file taken from the World Bank web service. The root element and children elements have the same tag, as shown below. I am getting a ClassCastException when unmarshalling. This error goes away when I change the root element tag so that it is not the same as its children.
Is JAXB unable to handle this scenario or am I not using JAXB properly?
<data>
<data>
</data>
......
<data>
</data>
</data>
Here is my Java code for reference:
XML with the tag issue: http://api.worldbank.org/countries/all/indicators/SP.POP.TOTL?format=xml
main class
public class CountryPopParse {
public List<CountryPop> parse() throws JAXBException, MalformedURLException, IOException{
JAXBContext jc = JAXBContext.newInstance(CountryPops.class);
Unmarshaller u = jc.createUnmarshaller();
URL url = new URL("http://api.worldbank.org/countries/all/indicators/SP.POP.TOTL?format=xml");
CountryPops countryPops = (CountryPops) u.unmarshal(url);
return countryPops.getCountryPop();
}
public static void main(String[] args) throws JAXBException, IOException, SQLException{
CountryPopParse p = new CountryPopParse();
List<CountryPop> popList= p.parse();
System.out.println(popList.get(0).getDate());
}
}
root element class
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "data")
public class CountryPops {
#XmlElement(name = "data", type = CountryPop.class)
private List<CountryPop> countryPops = new ArrayList<>();
public CountryPops(){
}
public CountryPops(List<CountryPop> countryPops) {
this.countryPops = countryPops;
}
public List<CountryPop> getCountryPop() {
return countryPops;
}
}
child element class
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "data")
public class CountryPop {
#XmlElement(name="date")
private int date;
public int getDate() {
return date;
}
}
Just remove the #XmlRootElement(name = "data") from the CountryPop class like the following:
#XmlAccessorType(XmlAccessType.FIELD)
public class CountryPop {
#XmlElement(name="date")
private int date;
public int getDate() {
return date;
}
}
And if you are handling the namespace wb should work fine.

Related Links

UTF-8 character not supporting
Java deletes my resource files
Connecting to web server with java socket class
Java won't write UTF8 characters in MySQL through a JNDI datasource
Concurrency : Designing a REST API using Play Framework/Akka
how to change Mybean in “Type type = new TypeToken<List<Mybean>>(){}.getType()” to be a parameter?
How to convert XML to Object in Groovy and insert into MongoDB collection? [closed]
Spring Boot Jersey - Missing EmbeddedServletContainerFactory when running java -jar command
Is there such a thing as too many chromecast devices on one network?
Cloud service to upload files from java app
multiple threads accessing methods
Unable to play video in JPanel using vlcj in ubuntu 13.04
How to shutdown dropwizard application?
Order ascending/descending methods on double linked list not working
Java - out of bounds exception
Why does JavaMail BodyPart.getInputStream() return an empty stream when using IMAP, but not when using POP3?

Categories

HOME
xamarin
google-chrome-extension
osgi
hook
gerrit
webpack
omnet++
magnific-popup
view
raspberry-pi
kalman-filter
sqlite3
swagger-ui
paradox
applepay
gorm
metatrader4
vault
midi
handsontable
collectd
iolanguage
django-simple-history
footer
scichart
visjs
cx-freeze
one-to-many
url-scheme
spring-mybatis
buck
atlassian-crucible
c++-amp
newline
unboundid
binary-data
data-manipulation
google-qpx-express-api
windows-dev-center
subset-sum
rotational-matrices
mmenu
file-format
simplexml
vapor
dism
wptoolkit
executenonquery
long-polling
flickr-api
rainbowtable
upstart
powercli
jspdf-autotable
webdriverjs
cubes
wdf
azure-sdk
radtreelist
slicknav
azure-virtual-network
url-masking
transmitfile
ready-api
livequery
itextpdf
jfugue
t4mvc
cannon.js
retina
emailrelay
map-projections
nsmutabledictionary
relocation
html-helper
openexr
heisenbug
tld
reactfx
uv-mapping
aqtime
odata4j
flash-builder4.5
cos
eclipse-memory-analyzer
jelly
mhtml
unc
chuck
cisco-jtapi
gdata-api
pysimplesoap
multipage
reddot
spring-portlet-mvc
punbb
hgsubversion
adk
ecl
gamma
webkit.net
sef
photoshop-cs4
simpletest
text-coloring
defensive-programming
wsdl.exe

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App