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

XML Read to Array in Autoit
Count number of children of the child with more children
how to read data from an XML site and put it in a var in vb.net
XQuery request opens whole file
getting parent node according two child node text XPATH
identity constraint key matches more than one value within the scope of its selector
Where to find/how to extract image hyperlinks from zipped docx document?
How do you echo a question mark in a shell script?
While transforming a xml to xml using xsl 1.0. xsl:attribute: Cannot add attributes to an element if children have been already added to the element
Can you declare and use a variable inside an XML without using XSL to transform/parse the XML
lxml and xml namespaces - Using find and findall to get XML Tag Value
cvc-complex-type.2.4.d: Invalid content was found starting with element 'testResources'. No child element is expected at this point
Using a webservice in Delphi by manually building XML (i.e. bypassing WSDL)
Validate Xml with XSd without using XSD physical path
How do I add line breaks after each tag in MSXML?
writing groovy script for REST service in SOAP UI

Categories

HOME
azure-stream-analytics
google-cloud-platform
oracle12c
hyperlink
polymer
pagination
sitecore
socket.io
xbee
entity-framework-core
nfs
title
elasticsearch-5
arangodb
pega
atlassian
3nf
screen-readers
internationalization
android-7.0-nougat
shortest-path
sigma.js
connector
gmm
swagger-2.0
systemtime
parcelable
fat
tizen-tv
jaws
failover
nsurl
dotspatial
scatter3d
turfjs
jbutton
laravel-eloquent
github3.py
twos-complement
least-squares
recurrent-neural-network
preloader
drupal-theming
optimizely
mesosphere
magento-2.0.7
cnc
page-replacement
arq
colorbar
android-testing
type-theory
structuremap4
choice
angular2-testing
grails-plugin
omxplayer
fuzzywuzzy
np-spring
blitline
jaunt-api
maybe
inf
poppler
cgi-bin
mod-proxy
finalbuilder
dronekit-android
grouping-sets
exceldatareader
crowd
easyrtc
teaspoon
appserver
encryption-asymmetric
cancellation
buffering
decoupling
high-resolution
c++builder-xe5
diem
twisted.web
paginator
infobright
relative
umra
openfaces
duplex
echo3
glui
fdf
java1.4
ifilter

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