Xml data load with java

JAXB Read XML to Java Object Example

Java example to read XML to Objects. XML can be supplied via some .xml file or simply an string representation. The populated Java objects then can be used in application for further processing or workflow.

Java provides many approaches to read an XML file and use the XL content to either print, use in application or populate data in Java objects to further use in application lifecycle. The three main APIs used for this purpose are Simple API for XML ( SAX ), the Document Object Model ( DOM ) and Java Architecture for XML Binding ( JAXB ).

  • SAX or DOM parser use the JAXP API to parse an XML document. Both scan the document and logically break it up into discrete pieces (e.g. nodes, text and comment etc).
  • SAX parser starts at the beginning of the document and passes each piece of the document to the application in the sequence it finds it. Nothing is saved in memory so it can’t do any in-memory manipulation.
  • DOM parser creates a tree of objects that represents the content and organization of data in the Document object in memory. Here, application can then navigate through the tree to access the data it needs, and if appropriate, manipulate it.
  • While JAXB unmarshal the document into Java content objects. The Java content objects represent the content and organization of the XML document, and are directly available to your program.

2) Convert XML String to Java Object

To read XML, first get the JAXBContext . It is entry point to the JAXB API and provides methods to unmarshal, marshal and validate operations.

Читайте также:  Php exist object property

Now get the Unmarshaller instance from JAXBContext . It’s unmarshal() method unmarshal XML data from the specified XML and return the resulting content tree.

String xmlString = "" + " " + " 101" + " IT" + " " + " Lokesh" + " 1" + " Gupta" + ""; JAXBContext jaxbContext; try < jaxbContext = JAXBContext.newInstance(Employee.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Employee employee = (Employee) jaxbUnmarshaller.unmarshal(new StringReader(xmlString)); System.out.println(employee); >catch (JAXBException e) < e.printStackTrace(); >Output: Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]]

3) Convert XML File to Java Object

Reading XML from file is very similar to above example. You only need to pass File object in place of StringReader object. File will have reference to ‘.xml’ file to be read in file system.

File xmlFile = new File("employee.xml"); JAXBContext jaxbContext; try < jaxbContext = JAXBContext.newInstance(Employee.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Employee employee = (Employee) jaxbUnmarshaller.unmarshal(xmlFile); System.out.println(employee); >catch (JAXBException e) < e.printStackTrace(); >Output: Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]]
import java.io.Serializable; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "employee") @XmlAccessorType(XmlAccessType.PROPERTY) public class Employee implements Serializable < private static final long serialVersionUID = 1L; private Integer id; private String firstName; private String lastName; private Department department; public Employee() < super(); >public Employee(int id, String fName, String lName, Department department) < super(); this.id = id; this.firstName = fName; this.lastName = lName; this.department = department; >//Setters and Getters @Override public String toString() < return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", department="+ department + "]"; >> @XmlRootElement(name = "department") @XmlAccessorType(XmlAccessType.PROPERTY) public class Department implements Serializable < private static final long serialVersionUID = 1L; Integer id; String name; public Department() < super(); >public Department(Integer id, String name) < super(); this.id = id; this.name = name; >//Setters and Getters @Override public String toString() < return "Department [id=" + id + ", name=" + name + "]"; >>

Content of xml file is same as first example.

Читайте также:  Вставить внешнюю ссылку html

Drop me your questions in comments section.

Источник

Оцените статью