怎么解析XML字符串? 访问一个serverlet并传递一个参数,该参数为XML格式, 如果在serverlet中解析?? 能否给个实例, 谢谢!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 xml4j jdom等都可以, 以下是使用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; }} 推荐用dom4j.. 非常容易上手.!对着API,搞出来时很容易的 好像上面的回复除了点问题,csdn报的错.!推荐用dom4j吧, 非常容易上手,对着API整出来还是蛮容易的.! http://developer.51cto.com/art/200903/117512.htm http://developer.51cto.com/art/200903/117512.htm 采用don4j是比较通用的方式,网上有很多解析的例子。 采用don4j是比较通用的方式,网上有很多解析的例子。 采用dom4j是比较通用的方式,网上有很多解析的例子。 这样写有什么错嘛 ?ObjectOutputStream的多次new Java调用AppleScript java万年历的问题 求助一个jdbc的问题 JUnit测试 Hibernate的save等操作,设置事务回滚的? singleton与静态类 java怎么打开excel文件呀 .例如:c:\demo.xls java在windows和linux环境下的不同。 请指教请指教! 关于jaon格式转换的问题 URLEncode出错,请大家帮忙看看. 用compareTo 和 equals比较getParameter返回值的用法????
<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;
}
}
对着API,搞出来时很容易的