2012-09-28 58 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 + "]"; 
     } 
    } 
} 

希望這會幫助你。