N
N
netrox2018-04-19 21:08:42
Java
netrox, 2018-04-19 21:08:42

How to convert XML to class object?

There is a table in Mysql, in one of the Varchar type columns information about the user is stored in the form of XML. How can I convert the given XML into a normal .

public class Employee 
{
  private int id;

  private String firtsName;

  private String lastName;

  private String email;

  private String address;
  private int positionId;
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirtsName() {
    return firtsName;
  }
  public void setFirtsName(String _firtsName) {
    this.firtsName = _firtsName;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String _lastName) {
    this.lastName = _lastName;
  }
  public String getEmail() {
    return this.email;
  }
  public void setEmail(String info) {
    this.email = info;
  }
  public int getPositionId() {
    return this.positionId;
  }
  public void setPositionId(int positionId) {
    this.positionId = positionId;
  }
  public String getAddress() {
    return this.address;
  }
  public void setAddress(String _address) {
    this.address = _address;
  }
  public String toString()
  {
    return String.format("Id:"+this.id+"First name:"+this.firtsName+"Last name"+this.lastName);
  }
}

<Employee>
      <firstName>Colin</firstName>
      <lastName>Smith</lastName>
      <email>[email protected]</Email>
      <address>Chicago</Address>
  </Employee>

5ad8db194a09e336050894.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-04-19
@netrox

Using Jackson

XmlMapper xmlMapper = new XmlMapper();
SomeClass obj = xmlMapper.readValue(xmlStr, SimpleBean.class);

or using JAXB
JAXBContext jaxbContext = JAXBContext.newInstance(SomeClass.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StreamSource source = new StreamSource(new StringReader(xmlStr));
JAXBElement<SomeClass> element = unmarshaller.unmarshal(source, SomeClass.class);
SomeClass someClass = element.getValue();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question