Viswa
Viswa

Reputation: 1377

Conversion of Java object into XML and parse it using DOM/SAX

I need to convert Java object into XML object and send it across to client browser from server.

And in client browser, i need to parse the obtained XML object using DOM/SAX or anything that suits and display it in UI.

Which one suits the above? Could any of them help me out in this?

Upvotes: 0

Views: 4726

Answers (3)

Arun Kumar
Arun Kumar

Reputation: 6804

You can use the JAXB API for your problem. JAXB(Java Architecture for XML Binding) uses annotations to convert Java object to / from XML Content. JAXB solves the following puposes:

Marshalling – Convert a Java object into a XML Content.

Unmarshalling – Convert XML content into a Java Object.

You can find the simple example of JAXB at here.

Upvotes: 2

Arun Kumar
Arun Kumar

Reputation: 6804

Here is a sample JAVA Class uses JAXB to convert JAVA Object into XML Content

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;

class JavaToXMLContent {
    public static void main(String[] args) throws Throwable {
        // =============================================================================================================
        // Setup JAXB
        // =============================================================================================================

        // Create a JAXB context passing in the class of the object we want to marshal/unmarshal
        final JAXBContext context = JAXBContext.newInstance(JavaObject.class);

        // =============================================================================================================
        // Marshalling OBJECT to XML
        // =============================================================================================================

        // Create the marshaller, this is the nifty little thing that will actually transform the object into XML
        final Marshaller marshaller = context.createMarshaller();

        // Create a stringWriter to hold the XML
        final StringWriter stringWriter = new StringWriter();

        // Create the sample object we wish to transform into XML
        final JavaObject javaObject = new JavaObject();
        javaObject.setName("Json");
        javaObject.setRole("Moderator");
        javaObject.setAge(28);

        // Marshal the javaObject and write the XML to the stringWriter
        marshaller.marshal(javaObject, stringWriter);

        // Print out the contents of the stringWriter
        System.out.println(stringWriter.toString());

        // =============================================================================================================
        // Unmarshalling XML to OBJECT
        // =============================================================================================================

        // Create the unmarshaller, this is the nifty little thing that will actually transform the XML back into an object
        final Unmarshaller unmarshaller = context.createUnmarshaller();

        // Unmarshal the XML in the stringWriter back into an object
        final JavaObject javaObject2 = (JavaObject) unmarshaller.unmarshal(new StringReader(stringWriter.toString()));

        // Print out the contents of the JavaObject we just unmarshalled from the XML
        System.out.println(javaObject2.toString());
    }

    /**
     * JavaObject is the sample object we've created to use for marshalling to and from XML.
     * Make sure you have the @XmlRootElement annotation at the top there as well or JAXB
     * might moan.
     */
    @XmlRootElement
    private static class JavaObject {

        private String name;

        private String role;

        private int age;

        public JavaObject() {

        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getRole() {
            return role;
        }

        public void setRole(String role) {
            this.role = role;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "Name [" + this.name + "], Role [" + this.role + "], Age [" + this.age + "]";
        }
    }
}

Hope this will help you.

Upvotes: 1

turtledove
turtledove

Reputation: 25914

have a look at simplexml, it use annotation for mapping between xml and java field, very handy.

Upvotes: 0

Related Questions