Search code examples
javaxmlxstream

Reading Element attribute value using XStream


I am not able lookup attribute value of an element. My XML is

<Person>
          <BirthDate>2008-01-04</BirthDate>
          <FirstName>Affo</FirstName>
          <Gender tc="200">Male</Gender>
          <LastName></LastName>
          <Occupation>false</Occupation>
          <Age>4</Age>
</Person>

I am interested in <Gender tc="200">Male</Gender>. My POJO looks like this:

    private String FirstName;
    private String LastName;
    private String Occupation;
    @XStreamAsAttribute
    @XStreamAlias("tc")
    private String genderTC;
    private String Gender;
    private String birthDate;
    private int age;

From XML is

            XStream stream = new XStream(new DomDriver());
        stream.processAnnotations(PersonType.class);
        PersonType person = (PersonType) stream.fromXML(file);

        System.out.println(person.getFirstName());
        System.out.println(person.getGenderTC());
        System.out.println(person.getGender());

Here for person.getGenderTC() I am getting null. Interesting part is when I reversed the process and generated the xml using same PersonType pojo, I got following XML:

<Person tc="111">
  <FirstName>Himanshu</FirstName>
  <Gender>M</Gender>
  <Age>28</Age>
</Person>

Solution

  • @BlaiseDoughan That's great and I appreciate your support. Can you please tell me how can I integrate EclipseLink MOXy with my project without using jaxb.properties? What are the libraries/JARs to include? In between I am aware of EclipseLink JAXB (MOXy) implementation to get attribute. My only hunch is jaxb.properties file.

    Person

    Below is how you could use MOXy's @XmlPath annotation to do the mapping you are looking for:

    package forum11417620;
    
    import javax.xml.bind.annotation.*;
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement(name="Person")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Person {
    
        @XmlElement(name="FirstName")
        private String firstName;
    
        @XmlElement(name="LastName")
        private String lastName;
    
        @XmlElement(name="Occupation")
        private String occupation;
    
        @XmlPath("Gender/@tc")
        private String genderTC;
    
        @XmlPath("Gender/text()")
        private String gender;
    
        @XmlElement(name="BirthDate")
        private String birthDate;
    
        @XmlElement(name="Age")
        private int age;
    
    }
    

    Demo

    Below is an example of how you can bootstrap a MOXy JAXBContext without a jaxb.properties file.

    package forum11417620;
    
    import java.io.File;
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.JAXBContextFactory;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContextFactory.createContext(new Class[] {Person.class}, null);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum11417620/input.xml");
            Person person = (Person) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(person, System.out);
        }
    
    }
    

    input.xml/Output

    <?xml version="1.0" encoding="UTF-8"?>
    <Person>
       <FirstName>Affo</FirstName>
       <LastName></LastName>
       <Occupation>false</Occupation>
       <Gender tc="200">Male</Gender>
       <BirthDate>2008-01-04</BirthDate>
       <Age>4</Age>
    </Person>
    

    Required Binaries (from http://www.eclipse.org/eclipselink/downloads/)

    Option #1 - EclipseLink JAR (from Installer Zip)

    • eclipselink.jar

    Option #2 - MOXy bundles (from OSGi Bundles Zip)

    • org.eclipse.persistence.moxy.jar
    • org.eclipse.persistence.core.jar
    • org.eclipse.persistence.asm.jar

    Maven

    I have example pom.xml files on Git Hub as part of some examples from my blog: