<script type="text/javascript"> google_ad_client = "pub-5033576919944123"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_type = "text_image"; //2007-10-24: csdn.blog google_ad_channel = "8548491739"; </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
package tgystc.tool.dom;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
/**
* <p>
* @author wujy <br>
* @date 2007-10-22 <br>
* @descript: TODO <br>
* <p>
* 备注:于jse5.0下操作Dom,包括xpath<br>
* 1. <br>
* 2. <br>
* 3. <br>
* </p>
*
*/
public class Dom {
private Document doc = null;
private XPath xpath = null;
private DocumentBuilder db = null;
/**
*
* @param f
* @throws Exception
*
* <p>
* 说明:根据文件建立Document<br>
* 1. <br>
* 2. <br>
* 3. <br>
* </p>
*/
public void makeDoc(File f) throws Exception{
if (db == null){
db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
}
doc = db.parse(f);
}
/**
*
* @param is
* @throws Exception
*
* <p>
* 说明:根据输入流建立Document<br>
* 1. <br>
* 2. <br>
* 3. <br>
* </p>
*/
public void makeDoc(InputStream is) throws Exception{
if (db == null){
db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
}
doc = db.parse(is);
}
/**
*
* @param likeXML 遵循xml规范的字符串
* @throws Exception
*
* <p>
* 说明:根据字符串建立Document<br>
* 1. <br>
* 2. <br>
* 3. <br>
* </p>
*/
public void makeDoc(String likeXML) throws Exception{
// StringReader sr = new StringReader(likeXML);
ByteArrayInputStream bais = new ByteArrayInputStream(likeXML.getBytes());
makeDoc(bais);
}
/**
*
* @param uri
* @throws Exception
*
* <p>
* 说明:根据uri指定路径建立Document<br>
* 1. <br>
* 2. <br>
* 3. <br>
* </p>
*/
public void makeDocFromURI(String uri) throws Exception{
if (db == null){
db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
}
doc = db.parse(uri);
}
/**
*
* @param path
* @param type
* @return
* @throws Exception
*
* <p>
* 说明:从根节点查找<br>
* 1. <br>
* 2. <br>
* 3. <br>
* </p>
*/
public Object select(String path, QName type) throws Exception{
if (doc == null){
throw new Exception("请先建立Document");
}
Object obj = select(path, doc.getDocumentElement(), type);
return obj;
}
public NodeList select(String path) throws Exception{
return (NodeList)select(path, XPathConstants.NODESET);
}
/**
*
* @param path
* @param root
* @param type
* @return
* @throws Exception
*
* <p>
* 说明:从root参数指定的节点查找,返回对象类型由type参数指定<br>
* 1. <br>
* 2. <br>
* 3. <br>
* </p>
*/
public Object select(String path, Object root, QName type) throws Exception{
if (xpath == null){
xpath = XPathFactory.newInstance().newXPath();
}
Object obj = xpath.evaluate(path, root, type);
return obj;
}
public NodeList select(String path, Object root) throws Exception{
return (NodeList)select(path, root, XPathConstants.NODESET);
}
}
补充:
若自己找jdom包,则可以写成如下:
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
SAXReader reader = new SAXReader();
Document doc = null;
try {
doc = reader.read(fileName);
Element ele = doc.getRootElement();
List list = node.selectNodes(".//a/b/c");
} catch (Exception e) {
e.printStackTrace();
}
可惜jdom不在jse5.0默认包中,要另外下载.
<script type="text/javascript"> google_ad_client = "pub-5033576919944123"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_type = "text_image"; //2007-10-24: csdn.blog google_ad_channel = "8548491739"; </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>