aaa ...">

How to get xml attribute using JAXB

this is my xml:

<?xml version="1.0" encoding="UTF-8" ?> <organization> <bank> <description>aaa</description> <externalkey>123</externalkey> <property name="pName" value="1234567890" /> </bank> </organization> 

I used JAXB and unmarshall for this xml and I can get the description and externalkey. But I can not get the name of the property with a value.

  • This is my java class for unmarshall:

     JAXBContext jb = JAXBContext.newInstance(Organization.class); Unmarshaller um = jb.createUnmarshaller(); Organization org = (Organization) um.unmarshal(new File("\\upload\\bank999999.xml")); System.out.println(org.getBank().getDescription()); System.out.println(org.getBank().getExternalkey()); 
  • Organization.java

     @XmlRootElement public class Organization { Bank bank = new Bank(); public Bank getBank() { return bank; } public void setBank(Bank bank) { this.bank = bank; } } 
  • Bank.java

     @XmlRootElement public class Bank { private String description; private String externalkey; private String property; //..GETTER and SETTER } 

    How to get the name and value of a property? Thanks u

+4
source share
1 answer

bank

You need to change the property property from a String object to a domain object.

 @XmlAccessorType(XmlAccessType.FIELD) public class Bank { private String description; private String externalkey; private Property property; } 

Property

Then your property object will look something like this:

 @XmlAccessorType(XmlAccessType.FIELD) public class Property { @XmlAttribute private String name; @XmlAtrribute private String value; } 
+5
source

All Articles