使用dom4j和XMLHTTP轻松解决多条记录操作

类别:Java 点击:0 评论:0 推荐:

/////////////////////////////
// XML Http Function      //
////////////////////////////
function postXmlDocument(sUri, xmlDoc) {
   var xmlHttp = XmlHttp.create();
   var async = true;
   xmlHttp.open("POST", sUri, async);
   xmlHttp.setRequestHeader("Content-Type","text/xml");
   xmlHttp.setRequestHeader("charset","UTF-8");
   xmlHttp.onreadystatechange = function () {
      if (xmlHttp.readyState == 4)
         postHandleCallback(xmlHttp.responseText); // responseXML : XmlDocument
   }
   xmlHttp.send(xmlDoc);
}
Servlet端代码,只需要把xml流读进来,然后直接进行解析处理就可以了

  SAXReader xmlReader = new SAXReader();
  InputStream in = ServletActionContext.getRequest().getInputStream();
  document = xmlReader.read(in);

下面是对xml文档的遍历

for (Iterator i = root.elementIterator(modelName); i.hasNext();) {
    Element element = (Element) i.next();
    Object entityIns = instanceModel(modelName);
    Iterator subnodes = element.elementIterator();
    while (subnodes.hasNext()) {
     Element subnode = (Element) subnodes.next();
     dynamicPopulate(subnode.getName(), subnode.getText(), entityIns);
    }

}

本文地址:http://com.8s8s.com/it/it16398.htm