Convert Java object to XML string

Convert Java object to XML string

Asked on January 9, 2019 in XML.
Add Comment


  • 7 Answer(s)

    Converting Java object to XML string:     

         Use the Marshaler’s method for marshaling that takes a Writer as  the parameter:

    marshal(Object,Writer)

    and then it pass an Implementation that can build a String object.

    Direct Known Subclasses:

       BufferedWriter, CharArrayWriter, FilterWriter, OutputStreamWriter, PipedWriter, PrintWriter, StringWriter

    It will call its toString method to get the actual String value.

    Hence do the following below:

    StringWriter sw = new StringWriter();
    jaxbMarshaller.marshal(customer, sw);
    String xmlString = sw.toString();
    
    Answered on January 9, 2019.
    Add Comment

         Try the given code to convert java object to xml string using StringWriter,

    private static String jaxbObjectToXML(Customer customer) {
       String xmlString = "";
       try {
          JAXBContext context = JAXBContext.newInstance(Customer.class);
          Marshaller m = context.createMarshaller();
     
          m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // To format XML
     
          StringWriter sw = new StringWriter();
          m.marshal(customer, sw);
          xmlString = sw.toString();
     
       } catch (JAXBException e) {
          e.printStackTrace();
       }
     
       return xmlString;
    }
    
    Answered on January 9, 2019.
    Add Comment

         Use javax.xml.bind.JAXB for the conversion of java object to xml string,

    StringWriter sw = new StringWriter();
    JAXB.marshal(customer, sw);
    String xmlString = sw.toString();
    

         The reverse process will be,

    Customer customer = JAXB.unmarshal(new StringReader(xmlString), Customer.class);
    

         There is not necessary to deal with checked exceptions in this approach.

    Answered on January 9, 2019.
    Add Comment
    File file = new File("C:\\file.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    
    // output pretty printed
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    
    jaxbMarshaller.marshal(customer, file);
    jaxbMarshaller.marshal(customer, System.out);
    Answered on February 25, 2019.
    Add Comment
    StringWriter sw = new StringWriter();
    jaxbMarshaller.marshal(customer, sw);
    String xmlString = sw.toString();
    Answered on February 25, 2019.
    Add Comment
    private static String jaxbObjectToXML(Customer customer) {
        String xmlString = "";
        try {
            JAXBContext context = JAXBContext.newInstance(Customer.class);
            Marshaller m = context.createMarshaller();
    
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // To format XML
    
            StringWriter sw = new StringWriter();
            m.marshal(customer, sw);
            xmlString = sw.toString();
    
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    
        return xmlString;
    }
    Answered on February 25, 2019.
    Add Comment
    package com;
    
    import java.util.ArrayList;
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    /**
    *
    * @author ABsiddik
    */
    
    @XmlRootElement
    public class Customer {
    
    int id;
    String name;
    int age;
    
    String address;
    ArrayList<String> mobileNo;
    
    
     public int getId() {
        return id;
    }
    Answered on February 25, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.