Reading an attribute value of an element using XStream

I cannot evaluate the value of an attribute of an element. My xml

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

I'm interested in <Gender tc="200">Male</Gender>. My POJO is as follows:

    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

            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 get null. The interesting part is when I canceled the process and generated xml using the same PersonType pojo, I got the following XML:

<Person tc="111">
  <FirstName>Himanshu</FirstName>
  <Gender>M</Gender>
  <Age>28</Age>
</Person>
+2
source share
2 answers

@BlaiseDoughan , . , , EclipseLink MOXy jaxb.properties? /JAR ? EclipseLink JAXB (MOXy), . - jaxb.properties.

Person

MOXy @XmlPath , , :

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

, MOXy JAXBContext jaxb.properties.

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/

<?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>

( http://www.eclipse.org/eclipselink/downloads/)

№1 - JAR EclipseLink ( )

  • eclipselink.jar

№2 - MOXy bundles ( OSGi Bundles Zip)

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

Maven

pom.xml Git Hub :

+3

, :

@XStreamAsAttribute 
@XStreamAlias("tc") 
private String genderTC;

, tc Person node XML.

, pojo Gender, genderTC, Gender.

+2

All Articles