0

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

在客户端浏览器中,我需要使用 DOM/SAX 或任何适合的方式解析获得的 XML 对象并将其显示在 UI 中。

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

4

3 回答 3

2

您可以使用JAXB API 来解决您的问题。JAXB(Java Architecture for XML Binding)使用注解将 Java 对象转换为 XML 内容/从 XML 内容转换。JAXB 解决了以下目的:

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

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

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

于 2012-09-28T06:24:59.387 回答
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 + "]";
        }
    }
}

希望这会帮助你。

于 2012-09-28T06:44:38.817 回答
0

看看simplexml,它使用注解来映射 xml 和 java 字段,非常方便。

于 2012-09-28T06:20:31.003 回答