2012-09-28 57 views
0

我需要将Java对象转换为XML对象并将其从服务器发送到客户端浏览器。将Java对象转换为XML并使用DOM/SAX解析它

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

哪一个适合上述?他们中的任何一个能帮我解决这个问题吗?

回答

2

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

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

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

你可以找到JAXB的简单例子:here

+0

谢谢。它会将Java转换为XML文件还是XML对象?因为我需要将它转换为XML对象,因为我需要将它传递给客户端,在客户端我需要解析XML内容并在UI中显示它 – Viswa

+0

这将是XML内容。它的基础是要么将XML内容放入XML文件中,要么将其作为XML字符串传递给客户端。 –

+0

是否有可能将静态Java类转换为xmlstring? – Viswa

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

希望这会帮助你。