xml


Unable to get attribute value using JAXB ( For attribute in Parent class)


I am trying to read one xml using JAXB.
I am facing one weird issue where attribute of parent is not being read,but attributes of child classes are read.
I have referenced forums, but this seems to be a strange one.
Can anyone please let me know what is the mistake i am doing.
XML.
<?xml version="1.0" encoding="UTF-8"?>
<PhoneDirectory>
<Exchange exchangeName="ashfield2133">Ashfield</Exchange>
<PhoneNumber id="23" number="0489524401">
<FirstName>Test</FirstName>
<LastName>Test</LastName>
<Address>#34,rt road, State,Country,22344 </Address>
</PhoneNumber>
<PhoneNumber id="88" number="0409545401">
<FirstName>Testf2</FirstName>
<LastName>Testl2</LastName>
<Address>St 2 , State,Country,34555</Address>
</PhoneNumber>
<PhoneNumber id="88" number="0446775401">
<FirstName>Testf3</FirstName>
<LastName>Testl3</LastName>
<Address>St 3 , State,Country,546777</Address>
</PhoneNumber>
</PhoneDirectory>
PhoneDirectory Class
package com.test.phoneDirectory.dataclass;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
#XmlRootElement(name="PhoneDirectory")
public class PhoneDirectory {
private String exchange;
private String exchangeName;
#XmlElement(name="Exchange")
public String getExchange() {
return exchange;
}
public void setExchange(String exchange) {
this.exchange = exchange;
}
#XmlAttribute(name="exchangeName")
public String getExchangeName() {
return exchangeName;
}
public void setExchangeName(String exchangename) {
this.exchangeName = exchangename;
}
private List<PhoneNumber> phoneNumber;
#XmlElement(name="PhoneNumber")
public List<PhoneNumber> getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(List<PhoneNumber> phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
PhoneNumber Class
package com.test.phoneDirectory.dataclass;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name="PhoneNumber")
public class PhoneNumber {
private String id;
private String number;
private String firstName;
private String lastName;
private String address;
#XmlAttribute(name="id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
#XmlAttribute(name="number")
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
#XmlElement(name="FirstName")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
#XmlElement(name="LastName")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
#XmlElement(name="Address")
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Main class
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import com.test.phoneDirectory.dataclass.PhoneDirectory;
import com.test.phoneDirectory.dataclass.PhoneNumber;
public class GetAllPhoneData {
public static void main(String[] args) throws JAXBException {
// TODO Auto-generated method stub
JAXBContext jc = JAXBContext.newInstance(PhoneDirectory.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
PhoneDirectory phoneDirectory = (PhoneDirectory) unmarshaller.unmarshal(new File("src/config/PhoneDirectory.xml"));
System.out.println("Get all phone details");
System.out.println("Exchange:"+phoneDirectory.getExchange());
System.out.println("exchangeName:"+phoneDirectory.getExchangeName());
for (PhoneNumber phonedetails : phoneDirectory.getPhoneNumber()) {
System.out.println(phonedetails.getId());
System.out.println(phonedetails.getNumber());
System.out.println(phonedetails.getFirstName());
System.out.println(phonedetails.getLastName());
System.out.println(phonedetails.getAddress());
}
}
}
Output
Get all phone details
Exchange: Ashfield
****Get exchangeName :null****
23
0489524401
Test
Test
#34,rt road, State,Country,22344
As you can see the exchangeName is null despite mentioning XMLAttribute annotation for field.
Thanks,
Vishnu
You've declared the exchangeName attribute in the PhoneDirectory class but your XML has this attribute in the Exchange element.
So instead of
private String exchange;
private String exchangeName;
you'll need a class like Exchange with #XmlAttribute exchangeName and #XmlValue exchange.

Related Links

JSF 1.2 Mojarra View State encryption - GZip Error - java.io.IOException: Not in GZIP format
Error when changing mandatoriness constraint on XML column
xslt to rename nodes based on recursive position [closed]
cakephp how can I read data from a xml webservice?
What is meant by XML Validation?
XSLT to remove instances of element in XML
XML VB.net not finding property
grab data from xml file
Eclipse formatter for XML like Java
Error in Retrieving XML data into ArrayCollection through HTTPService in FLEX
Go xml unmarshalling
Convert Italic XML Tags into WordML Tags II
print XML/XSLT data via CGI
Need to parse a text file into a specific xml format
using xslt to sort xml via onclick function
xs:unique is matching multiple values when they are unique

Categories

HOME
cheerio
string
merge
angular2-material
saml-2.0
composite-primary-key
yandex
emscripten
nested-if
polling
trac
qpython
mod-security
project-intu
ycsb
cmdb
naivebayes
html5-appcache
sigma.js
asp.net-core-webapi
articulate-storyline
r-mice
postscript
draft-js-plugins
custom-post-type
syntax-highlighting
gpio
appirater
flooding
has-and-belongs-to-many
datastax-enterprise-graph
sinch
ceylon
nsurl
record
r-grid
jett
dsx-desktop
swagger-php
metawidget
skylink
dbnull
claims
strftime
nanoc
lampp
netbeans6.8
yii2-user
edit
shinobi
filenet
rsa-archer-grc
slackware
myspace
wso2developerstudio
i386
dbamp
nupic
contentcontrol
boomerang
dynatree
wiredep
vigenere
morton-number
vacuum
ansi
uvc
fail2ban
zxspectrum
qtwebengine
adehabitathr
dbaccess
linkedin-jsapi
mdichild
kissfft
embedded-fonts
code-duplication
gpars
chart-director
resource-management
redis-py
wiimote
android-holo-everywhere
automationelement
jqote
folding
application-verifier
google-wave
sifr3
localizable.strings
xslcompiledtransform

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