WEB开发网
开发学院软件开发Java Spring原理自我实践之IOC的模拟实现 阅读

Spring原理自我实践之IOC的模拟实现

 2009-09-18 00:00:00 来源:WEB开发网   
核心提示: Class_2:辅助解析XML文件的工具类 Java代码packagecom.java.util;importjava.io.File;importjava.util.Iterator;importjava.util.List;importorg.dom4j.Attribute;importor

Class_2:辅助解析XML文件的工具类

Java代码   

package com.java.util; 
 
import java.io.File; 
 
import java.util.Iterator; 
import java.util.List; 
 
import org.dom4j.Attribute; 
import org.dom4j.Document; 
import org.dom4j.Element; 
import org.dom4j.io.SAXReader; 
 
/**辅助解析XML文件的工具类 
 * @author citizen 
 * 
 */ 
public class AnalyzeXMLUtil { 
 
 /** 
 * 获得指定元素的值 
 * 
 * @param filename 
 *      xml文件的路径 
 * @param elementPath 
 *      元素的xpath路径(例如"/aaa/bbb") 
 * @return 
 */ 
 @SuppressWarnings("unchecked") 
 public static String[] getElementValue(String filename, String elementPath) { 
 try { 
  File file = new File(filename); 
  SAXReader saxReader = new SAXReader(); 
  Document doc = saxReader.read(file); 
 
  List<Element> list = doc.selectNodes(elementPath); 
  String[] ret = new String[list.size()]; 
  Iterator<Element> i = list.iterator(); 
  int count = 0; 
 
  while (i.hasNext()) { 
  Element element = i.next(); 
  ret[count++] = element.getText(); 
  } 
 
  return ret; 
 } catch (Exception e) { 
  return new String[0]; 
 } 
 } 
 
 /** 
 * 获得指定元素的值 
 * 
 * @param filename 
 *      xml文件的路径 
 * @param elementPath 
 *      元素的xpath路径(例如"/aaa/bbb") 
 * @return 
 */ 
 public static String getSingleElementValue(String filename, 
  String elementPath) { 
 try { 
  File file = new File(filename); 
  SAXReader saxReader = new SAXReader(); 
  Document doc = saxReader.read(file); 
 
  Element element = (Element) doc.selectSingleNode(elementPath); 
  return element.getText(); 
 } catch (Exception e) { 
  return ""; 
 } 
 } 
 
 /** 
 * 获得指定属性的值 
 * 
 * @param filename 
 *      xml文件的路径 
 * @param attributePath 
 *      属性的Xpath路径(例如"//@attr_name") 
 * @return 
 * @throws Exception 
 */ 
 @SuppressWarnings("unchecked") 
 public static String[] getAttributeValue(String filename, 
  String attributePath) { 
 try { 
  File file = new File(filename); 
  SAXReader saxReader = new SAXReader(); 
  Document doc = saxReader.read(file); 
 
  List<Attribute> list = doc.selectNodes(attributePath); 
  String[] ret = new String[list.size()]; 
  Iterator<Attribute> i = list.iterator(); 
  int count = 0; 
 
  while (i.hasNext()) { 
  Attribute attribute = i.next(); 
  ret[count++] = attribute.getText(); 
  } 
 
  return ret; 
 } catch (Exception e) { 
  return new String[0]; 
 } 
 } 
 
 /** 
 * 获得指定属性的值 
 * 
 * @param filename 
 *      xml文件的路径 
 * @param attributePath 
 *      属性的Xpath路径(例如"//@attr_name") 
 * @return 
 * @throws Exception 
 */ 
 public static String getSingleAttributeValue(String filename, 
  String attributePath) { 
 try { 
  File file = new File(filename); 
  SAXReader saxReader = new SAXReader(); 
  Document doc = saxReader.read(file); 
 
  Attribute attribute = (Attribute) doc 
   .selectSingleNode(attributePath); 
 
  return attribute.getText(); 
 } catch (Exception e) { 
  return ""; 
 } 
 } 
 
 /** 
 * 获得指定属性的值 
 * 
 * @param filename 
 *      xml文件的路径 
 * @param elementPath 
 *      属性所在元素的xpath路径(例如"/aaa/bbb") 
 * @param attributeName 
 *      属性的名称 
 * @return 
 * @throws Exception 
 */ 
 @SuppressWarnings("unchecked") 
 public static String[] getAttributeValue(String filename, 
  String elementPath, String attributeName) { 
 try { 
  File file = new File(filename); 
  SAXReader saxReader = new SAXReader(); 
  Document doc = saxReader.read(file); 
 // System.out.println(elementPath + "[@" + attributeName + "]"); 
  List<Element> list = doc.selectNodes(elementPath + "[@" 
   + attributeName + "]"); 
  String[] ret = new String[list.size()]; 
  Iterator<Element> i = list.iterator(); 
  int count = 0; 
 
  while (i.hasNext()) { 
  Element element = i.next(); 
  ret[count++] = element.attribute(attributeName).getText(); 
  } 
 
  return ret; 
 } catch (Exception e) { 
  return new String[0]; 
 } 
 } 
 
 /** 
 * 获得指定属性的值 
 * 
 * @param filename 
 *      xml文件的路径 
 * @param elementPath 
 *      属性所在元素的xpath路径(例如"/aaa/bbb") 
 * @param attributeName 
 *      属性的名称 
 * @return 
 * @throws Exception 
 */ 
 public static String getSingleAttributeValue(String filename, 
  String elementPath, String attributeName) { 
 try { 
  File file = new File(filename); 
  SAXReader saxReader = new SAXReader(); 
  Document doc = saxReader.read(file); 
 
  Element element = (Element) doc.selectSingleNode(elementPath + "[@" 
   + attributeName + "]"); 
  return element.attribute(attributeName).getText(); 
 } catch (Exception e) { 
  return ""; 
 } 
 } 
} 

上一页  1 2 3 4 5  下一页

Tags:Spring 原理 自我

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接