Java and XML tutorial with StAX parser - writing cursor based and iterator based
package com.xml; import java.math.BigDecimal; import org.springframework.core.style.ToStringStyler; public class Employee { private String name; private String type; private int age; private BigDecimal salary; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public BigDecimal getSalary() { return salary; } public void setSalary(BigDecimal salary) { this.salary = salary; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("name=" + name); sb.append("\n"); if(type != null) { sb.append("type=" + type + "\n"); } sb.append("age=" + age); return sb.toString(); } }
Here is a simple example to write employee object to XML using simpler cursor based approach.
package com.xml; import java.io.StringWriter; import java.math.BigDecimal; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; public class StaxProcessing { public static void main(String[] args) { XMLOutputFactory factory = XMLOutputFactory.newInstance(); // construct employee object Employee emp = new Employee(); emp.setName("Peter"); emp.setType("first"); emp.setAge(25); emp.setSalary(BigDecimal.valueOf(35000.00)); StringWriter sw = new StringWriter(); try { XMLStreamWriter writer = factory.createXMLStreamWriter(sw); writer.writeStartDocument(); writer.writeStartElement("Employee"); writer.writeStartElement("name"); writer.writeAttribute("type", "first"); writer.writeCharacters(emp.getName()); writer.writeEndElement(); writer.writeStartElement("age"); writer.writeCharacters(new Integer(emp.getAge()).toString()); writer.writeEndElement(); writer.writeStartElement("salary"); writer.writeCharacters(emp.getSalary().toString()); writer.writeEndElement(); writer.writeEndElement(); System.out.println(sw.getBuffer().toString()); } catch (XMLStreamException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Output:
<?xml version="1.0" ?> <Employee><name type="first">Peter</name><age>25</age><salary>35000.0</salary></Employee>
Here is a simple example to write employee object to XML using simpler iterator based approach.
package com.xml; import java.io.StringWriter; import java.math.BigDecimal; import javax.xml.stream.XMLEventFactory; import javax.xml.stream.XMLEventWriter; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.XMLEvent; public class StaxProcessing { public static void main(String[] args) { XMLOutputFactory factory = XMLOutputFactory.newInstance(); XMLEventFactory eventFactory = XMLEventFactory.newInstance(); // construct employee object Employee emp = new Employee(); emp.setName("Peter"); emp.setType("first"); emp.setAge(25); emp.setSalary(BigDecimal.valueOf(35000.00)); StringWriter sw = new StringWriter(); try { XMLEventWriter writer = factory.createXMLEventWriter(sw); XMLEvent event = eventFactory.createStartDocument(); writer.add(event); // employee event = eventFactory.createStartElement("", "", "Employee"); writer.add(event); // name event = eventFactory.createStartElement("", "", "name"); writer.add(event); event = eventFactory.createAttribute("type", "first"); writer.add(event); event = eventFactory.createCharacters(emp.getName()); writer.add(event); event = eventFactory.createEndElement("", "", "name"); writer.add(event); // age event = eventFactory.createStartElement("", "", "age"); writer.add(event); event = eventFactory.createCharacters(new Integer(emp.getAge()).toString()); writer.add(event); event = eventFactory.createEndElement("", "", "age"); writer.add(event); // salary event = eventFactory.createStartElement("", "", "salary"); writer.add(event); event = eventFactory.createCharacters(emp.getSalary().toString()); writer.add(event); event = eventFactory.createEndElement("", "", "salary"); writer.add(event); // close employee event = eventFactory.createEndElement("", "", "Employee"); writer.add(event); writer.flush(); writer.close(); System.out.println(sw.getBuffer().toString()); } catch (XMLStreamException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Output:
<?xml version="1.0" ?> <Employee><name type="first">Peter</name><age>25</age><salary>35000.0</salary></Employee>
Labels: XML
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home