First of all, let's get started with the java DTO with some fields and annotations we will complete the object.
You can copy the employee.xml file to your C:\Temp\employee.xml and your file should look something like this.
Now, let's write the core logic which will convert the xml into the java object. In new file object I've mentioned the location for the employee.xml file. Make sure you have the file at the same location with same content otherwise you may have to change the coding.
Once after we run XmlToJava class we are going to get the following output into the console.
Employee [firstName=James, lastName=Bond, ssn=123456789, eId=7]
package com.java.example;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Employee {
private String firstName;
private String lastName;
private long ssn;
private int eId;
@XmlElement
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@XmlElement
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@XmlElement
public long getSsn() {
return ssn;
}
public void setSsn(long ssn) {
this.ssn = ssn;
}
@XmlAttribute
public int geteId() {
return eId;
}
public void seteId(int eId) {
this.eId = eId;
}
@Override
public String toString() {
return "Employee [firstName=" + firstName + ", lastName=" + lastName
+ ", ssn=" + ssn + ", eId=" + eId + "]";
}
}
You can copy the employee.xml file to your C:\Temp\employee.xml and your file should look something like this.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee eId="7">
<firstName>James</firstName>
<lastName>Bond</lastName>
<ssn>123456789</ssn>
</employee>
Now, let's write the core logic which will convert the xml into the java object. In new file object I've mentioned the location for the employee.xml file. Make sure you have the file at the same location with same content otherwise you may have to change the coding.
package com.java.example;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class XmlToJava {
public static void main(String[] args) {
try {
File file = new File("C:\\Temp\\employee.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Employee employee = (Employee) jaxbUnmarshaller.unmarshal(file);
System.out.println(employee);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
Once after we run XmlToJava class we are going to get the following output into the console.
Employee [firstName=James, lastName=Bond, ssn=123456789, eId=7]
Thanks it was helpfull...
ReplyDeleteyurtdışı kargo
ReplyDeleteresimli magnet
instagram takipçi satın al
yurtdışı kargo
sms onay
dijital kartvizit
dijital kartvizit
https://nobetci-eczane.org/
3SBEVA
Litvanya yurtdışı kargo
ReplyDeleteLüksemburg yurtdışı kargo
Macaristan yurtdışı kargo
Malta yurtdışı kargo
Polonya yurtdışı kargo
JHNSG8
Portekiz yurtdışı kargo
ReplyDeleteRomanya yurtdışı kargo
Slovakya yurtdışı kargo
Slovenya yurtdışı kargo
İngiltere yurtdışı kargo
CS3İ0G
Yunanistan yurtdışı kargo
ReplyDeleteAfganistan yurtdışı kargo
Amerika Birleşik Devletleri yurtdışı kargo
Amerika Samoası yurtdışı kargo
Angola yurtdışı kargo
F1TJ5
افران الغاز N3kysP5qIr
ReplyDeleteشركة مكافحة حشرات بخميس مشيط dySA3uGUKd
ReplyDeleteشركة رش حشرات بالاحساء 6oNJ3YqttD
ReplyDelete