繁体   English   中英

如何解决XML转换问题?

[英]How to solve XML conversion Issue?

大家好,我在我的项目中使用Ajax在一个选择框中加载产品名称,而在选择框onchange事件中使用存储名称的onselect事件。 在这里,我使用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。

detailList包含数据库中与商店相关的产品列表。

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();

}

我已经在动作类中导入了以下软件包。

导入org.w3c.dom.Document;

导入org.w3c.dom.Element;

导入org.w3c.dom.Text;

导入com.sun.org.apache.xml.internal.serialize.OutputFormat;

导入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:\\ Users \\ Desktop \\ Updated \\ Project \\ src \\ main \\ java \\ com \\ action \\ AjaxAction.java:[199,32] com.sun.org.apache.xml.internal.serialize.XMLSerialize是Sun专有的API并可能在将来的版本中删除

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

任何人都可以帮助我解决此问题。 提前致谢!!!。

使用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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM