访问一个serverlet并传递一个参数,该参数为XML格式, 如果在serverlet中解析?? 能否给个实例, 谢谢!!

解决方案 »

  1.   

    xml4j   jdom等都可以,
      

  2.   

    以下是使用dom4j读取xml配置文件的类,配置文件格式为:
    <config>
     <cti-config use="">
      <js>js file path</js>
      <ocx>ocx id</ocx>
     </cti_config>
    </config>
    使用CtiConfigParser类的getValue("js")方法,得到use属性配置为“true”的
    对应的<js>项的内容,这里就是"js file path"呵呵,不知道对你有没有帮助~~,下面是类源码:package com.tools;import org.dom4j.io.SAXReader;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.Attribute;import java.io.File;
    import java.util.List;
    import java.util.Iterator;
    /**
     * Created by IntelliJ IDEA.
     * User: Administrator
     * Date: 2006-9-14
     * Time: 8:26:58
     * To change this template use File | Settings | File Templates.
     */
    public class CtiConfigParser {
        private String configFileName = null;
        private Element parentElement = null;    public CtiConfigParser(String configFileName) {
            this.configFileName = configFileName;
        }    private void getParent(){
            if(configFileName != null){
                SAXReader reader = new SAXReader();
                Document document;
                try{
                    document = reader.read(new File(configFileName));
                }
                catch(DocumentException de){
                    de.printStackTrace();
                    return;
                }
                List list =document.selectNodes("/config/cti-config/@use");
                Iterator it = list.iterator();
                while(it.hasNext()){
                    Attribute attr = (Attribute)it.next();
                    String result = attr.getValue();
                    if(result.compareToIgnoreCase("true") == 0){
                        parentElement = attr.getParent();
                        break;
                    }
                    it = (Iterator)it.next();
                }
            }
        }
        public String getValue(String name){
            String value = "";
            if(parentElement == null){
                getParent();
            }
            if(parentElement != null){
                Iterator it = parentElement.elementIterator(name);
                if(it.hasNext()){
                    Element titleElement = (Element)it.next();
                    value = titleElement.getText();
                }
            }
            return value;
        }
    }
      

  3.   

    推荐用dom4j..  非常容易上手.!
    对着API,搞出来时很容易的
      

  4.   

    好像上面的回复除了点问题,csdn报的错.!推荐用dom4j吧, 非常容易上手,对着API整出来还是蛮容易的.!
      

  5.   

    http://developer.51cto.com/art/200903/117512.htm
      

  6.   

    http://developer.51cto.com/art/200903/117512.htm
      

  7.   

    采用don4j是比较通用的方式,网上有很多解析的例子。
      

  8.   

    采用don4j是比较通用的方式,网上有很多解析的例子。
      

  9.   

    采用dom4j是比较通用的方式,网上有很多解析的例子。