将Java对象转换为XML并使用DOM / SAX解析它

时间:2012-09-28 06:15:42

标签: xml json jaxb marshalling xstream

我需要将Java对象转换为XML对象,并将其从服务器发送到客户端浏览器。

在客户端浏览器中,我需要使用DOM / SAX或任何适合在UI中显示它的东西来解析获得的XML对象。

哪一个适合上述?他们中的任何一个可以帮助我吗?

3 个答案:

答案 0 :(得分:2)

您可以使用 JAXB API来解决您的问题。 JAXB(用于XML绑定的Java体系结构)使用注释将Java对象转换为XML内容或从XML内容转换Java对象。 JAXB解决了以下问题:

编组 - 将Java对象转换为XML内容。

解组 - 将XML内容转换为Java对象。

您可以在 here 找到JAXB的简单示例。

答案 1 :(得分:1)

下面是一个示例JAVA类使用JAXB将JAVA对象转换为XML内容

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 + "]";
        }
    }
}

希望这会对你有所帮助。

答案 2 :(得分:0)

查看simplexml,它使用注释在xml和java字段之间进行映射,非常方便。