2012-03-14 87 views
1

大家好我在我的項目中使用Ajax在一個選擇框中加載產品名稱與選擇框onchange事件中的storename onselect事件。在這裏,我使用Ajax獲取從java動作類到jsp的列表。我在Jsp和Action類中的代碼如下。如何解決XML轉換問題?

<s:label value="Store Name : *" />                   
<s:select name="storeName" list="storeList" onchange="loadProduct(this.value)" listKey="storeId" listValue="storeName" headerKey="-1" headerValue="Select the Store" /> 

<s:label value="Product Name : *" /> 
<s:select name="productName" list="productList" listKey="productId" listValue="productName" /> 


function loadProduct(id){ 
var URL = "AjaxPopMyCycle.action?storeName="+id; 
ajaxEditFunctionCall(URL); 

           } 

     function ajaxEditFunctionCall(URL){ 
       try{ 
        xmlHttp=new XMLHttpRequest(); 
       }catch (e){ 
       try{ 
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 
       }catch (e){ 
        try{ 
         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
        }catch (e){ 
         alert("Your browser does not support AJAX!"); 
         return false; 
        } 
       } 
      } 
      xmlHttp.onreadystatechange=function(){ 
       if(xmlHttp.readyState==4){ 
        if (xmlHttp.status == 200) { 
         if(xmlHttp.responseXML != null){      
           showMessage(xmlHttp.responseXML); 
         } 
        } 
       } 
      } 
      xmlHttp.open("GET", URL, true); 
      xmlHttp.send(URL); 
     } 
     function showMessage(errorDisplayXML){ 
     var checklist=document.Add.productName; 
      checklist.options.length=0; 
      if(xmlHttp.readyState==4){ 

      if(errorDisplayXML.getElementsByTagName("rootElement")[0]!=null){ 
       var rootElement = errorDisplayXML.getElementsByTagName("rootElement")[0]; 
       var location = rootElement.getElementsByTagName("Message"); 
       var locArr = location[0]; 
        var locArr = " "; 
        var tempArr; 
        var tempArr1; 
        for(var i=0; i<location.length; i++){ 
         tempArr = ""; 
         tempArr1 = ""; 
         locArr = location[i]; 
         tempArr = locArr.getElementsByTagName("productId")[0].firstChild.nodeValue; 
         tempArr1 = locArr.getElementsByTagName("productnName")[0].firstChild.nodeValue; 
         checklist.options[i]= new Option(tempArr1,tempArr);       
        }  
       }else{ 
        alert("errorDisplayXML Contains NULL"); 
       } 
      } 
      } 

以下代碼在Action類中用於獲取結果並加載到XML中,如下所示。

detailedList包含與從數據庫存儲相關的產品列表。

public String getDetails(List detailedList)throws Exception{ 

    Element tempElem = null, 
    rootElem = null; 
    Text textElem = null; 
    document=new org.dom4j.dom.DOMDocument(); 
    rootElem = document.createElement("rootElement"); 
    Element errorElement = null; 
    List saveList = new ArrayList(); 
    saveList = detailedList; 
    System.out.println("DetailedList:"+saveList.size()); 
    if(saveList.size()>0){ 
     try {    
      for(int i=0;i<saveList.size();i++){ 

       Product aproduct = (Product)saveList.get(i); 
       errorElement = document.createElement("Message"); 

       tempElem = document.createElement("productId"); 
       textElem = document.createTextNode(aproduct .getProductId());     
       tempElem.appendChild(textElem); 
       errorElement.appendChild(tempElem); 

       tempElem = document.createElement("productName"); 
       textElem = document.createTextNode(aproduct.getProductName()); 
       tempElem.appendChild(textElem); 
       errorElement.appendChild(tempElem); 

       rootElem.appendChild(errorElement);     
      }    
     }catch (Exception e) { 
      tempElem = document.createElement("Message"); 
      return parseToString(tempElem); 
     } 


    return parseToString(rootElem); 
} 

public String parseToString(Element node) throws Exception { 

    OutputFormat format = new OutputFormat(); 
    StringWriter stringOut = new StringWriter();  
    XMLSerializer serial = new XMLSerializer(stringOut,format); 

    serial.asDOMSerializer();       
    serial.serialize(node); 
    return stringOut.toString(); 

} 

我在我的操作類中導入了以下包。

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Text;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;

import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

它在過去3周內正常工作,可以正常工作。

但它沒有得到編譯,並在我的服務器中顯示以下錯誤消息。

C:\ Users \ Desktop \ Updated \ Project \ src \ main \ java \ com \ action \ AjaxAction.java:[199,5] com.sun.org.apache.xml.internal.serialize.XMLSerializer 是Sun的專有API,並可能在將來的版本中刪除

C:\用戶\桌面\更新\項目的\ src \主\ java中的\ com \行動 \ AjaxAction.java:[199,32]融爲一體。 sun.org.apache.xml.internal.serialize.XMLSerialize r是Sun專有的API,可能會在將來的版本中刪除

我的項目使用Struts2,Jsp,Hibernate3作爲前端,Mysql服務器作爲後端。我不知道解決這個問題。

任何人都請幫我解決這個問題。提前致謝!!!。

+0

只是一個側面不是你爲什麼不起訴JSON格式更輕盈,更flexible.S2具有建立JSON和DIN支持其易於使用。另外使用靈活和易於使用的XStream – 2012-03-14 06:26:48

+0

感謝UmeshAwasthi。你可以發送任何URL來使用JSON格式。我不知道這個技術。 – shiva 2012-03-14 07:00:01

+0

這裏是你的鏈接,你需要在你的classpath中擁有插件,並且你準備好了。 https://cwiki.apache.org/confluence/display/WW/JSON%20Plugin – 2012-03-14 07:03:08

回答

0

使用org.apache.xml.serialize.XMLSerializer也可以避免該錯誤。從xercesImpl-2.7.1.jar導入這個類,並在parseToString()像這樣使用:

import org.apache.xml.serialize.OutputFormat; 
import org.apache.xml.serialize.XMLSerializer; 

public String parseToString(Element node) throws Exception { 
    OutputFormat format = new OutputFormat(); 
    java.io.Writer stringOut = new StringWriter();  
    XMLSerializer serial = new XMLSerializer(stringOut,format);      
    serial.serialize(node); 
    return stringOut.toString(); 
}