java解析XML字符串
字符串是
<?xml version="1.0" encoding="GBK"?>
<resp><code>1000</code><data count="5">
<item msgid="2070105" from="ID1" create="2011-03-14 10:55:56" content="444444444"/>
<item msgid="2070106" from="ID2" create="2011-03-14 10:58:51" content="333333333"/>
<item msgid="2070107" from="ID3" create="2011-03-14 11:01:26" content="222222222"/>
<item msgid="2070108" from="ID4" create="2011-03-16 14:02:13" content="111111111"/></data></resp>
java怎么解析,我要code中的1000、循环取出item 的 msgid、 from、 create、 content的值
[解决办法]
基于xpath的读取方式,需要jaxen-1.1-beta-7.jar、dom4j-1.6.1.jar,理论上说,这个类可以读取任何格式的xml,但存在读大文件效率问题:
package org.sl.xml;import java.io.File;import java.io.FileInputStream;import java.net.URL;import java.util.List;import org.apache.log4j.Logger;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.Node;import org.dom4j.io.SAXReader;/** * 以xpath方式读取xml * @author Shanl * */public class XPathHelper { private File file = null; private boolean cache = false; private static Logger logger = Logger.getLogger(XPathHelper.class); /** * 构造器<br> * 相当于XPathHelper(res, true); * @param res 资源名 */ public XPathHelper(String res){ this(res, true); } /** * 构造器<br> * 注:这个方法会在系统中查找与res匹配的资源,如果找不到则抛出RunntimeException * @param res 资源名 * @param cahce 是否缓存XML */ public XPathHelper(String res, boolean cache){ this.cache = cache; URL url = ClassLoader.getSystemResource(res); if(null == url){ throw new RuntimeException(res + " not found."); } String protocol = url.getProtocol(); String path = url.toString(); path = path.replaceFirst(protocol + ":/", ""); file = new File(path); if(!file.exists()){ throw new RuntimeException(res + " not found."); } } /** * 返回xpath所表示的结点数 * @param xpath * @return */ public int countNode(String xpath){ Document fileDocument = readDocument(); if(!findXPath(xpath)){ return 0; } List es = fileDocument.selectNodes(xpath); if(null == es){ return 0; }else{ return es.size(); } } /** * 以xpath方式得到结果的文本 * @param xpath * @return */ public String getText(String xpath){ String text = ""; Document fileDocument = readDocument(); if(!findXPath(xpath)){ return ""; } Node node = fileDocument.selectSingleNode(xpath); text = node.getText(); return text; } /** * 以xpath方式得到结点某属性的值 * @param xpath * @param attrName * @return */ public String getAttribute(String xpath, String attrName){ String attr = ""; Document fileDocument = readDocument(); if(!findXPath(xpath)){ return ""; } List es = fileDocument.selectNodes(xpath); attr = ((Element)es.get(0)).attributeValue(attrName); return attr; } /** * 查找xpath是否存在 * @param xpath * @return 存在返回true,否则返回false; */ public boolean findXPath(String xpath){ Document fileDocument = readDocument(); try{ List es = fileDocument.selectNodes(xpath); if(0 == es.size()){ return false; }else{ return true; } }catch(Exception ex){ return false; } } /** * 读取配置文件 * * @return 返回Document对象 */ private Document readDocument() { SAXReader reader = new SAXReader();// Document fileDocument = null; try { if(cache){ if(null == fileDocument){ fileDocument = reader.read(new FileInputStream(file)); } }else{ fileDocument = reader.read(new FileInputStream(file)); } } catch(Exception ex){ logger.error(ex.getMessage(), ex); } return fileDocument; } private Document fileDocument = null;}