jse5.0下的Dom解析


<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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值