如何解决XML转换问题?

时间:2012-03-14 06:17:07

标签: java jsp struts2 xml-parsing dom4j

大家好我在我的项目中使用Ajax在一个选择框中加载产品名称,并在选择框onchange事件中使用onselect事件storename。在这里,我使用Ajax从java动作类到jsp的列表。我在Jsp和Action类中的代码如下。

 <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 \用户桌面\更新\项目\ SRC \主\的java \ COM \动作 \ AjaxAction.java:[199,5] com.sun.org.apache.xml.internal.serialize.XMLSerializer  是Sun专有API,可能会在将来的版本中删除

C:\ Users \用户桌面\更新\项目\ SRC \主\的java \ COM \动作 \ AjaxAction.java:[199,32] com.sun.org.apache.xml.internal.serialize.XMLSerialize r是Sun专有API,可能会在将来的版本中删除

我的项目使用Struts2,Jsp,Hibernate3作为前端,使用Mysql服务器作为后端。我不知道解决这个问题。

任何人都可以帮我解决这个问题。在此先感谢!!!。

1 个答案:

答案 0 :(得分: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();
}
相关问题