[Config]Code-xmlreader

  package com.common.cfg; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class XMLReader { private Document doc; public synchronized boolean load(String filename){ return reload(filename); } public synchronized boolean reload(String filename){ try { DocumentBuilder builder = DocumentBuilderFactory.newInstance(). newDocumentBuilder(); doc=builder.parse(new File(filename)); return true; } catch (Exception ex) { ex.printStackTrace(); } return false; } public Document getDocument(){ return doc; } public Element getRootElement(){ NodeList nl=doc.getChildNodes(); for(int i=0;i<nl.getLength();i++){ if(Node.ELEMENT_NODE==nl.item(i).getNodeType()){ return (Element)nl.item(i); } } return null; } } package com.common.cfg; import java.util.*; import org.w3c.dom.*; /** * * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author zzf * @version 1.0 */ public class XMLNode { private static final XMLNode[] EMPTY_NODELIST=new XMLNode[0]; private static final List EMPTY_ATTRIBUTELIST=new ArrayList(0); private String name; private String value; private XMLNode[] childNodes=EMPTY_NODELIST; private List attrNames=EMPTY_ATTRIBUTELIST; private List attrValues=EMPTY_ATTRIBUTELIST; public XMLNode() { } public XMLNode(String name,String value) { this.name=name; this.value=value; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public XMLNode getChildNode(int index) { return childNodes[index]; } //只返回匹配的第一个 public XMLNode getChildNode(String nodeName){ for(int i=0;i<childNodes.length;i++){ if(childNodes[i].getName().equals(nodeName)){ return childNodes[i]; } } return null; } /** * 返回节点名所有匹配 */ public XMLNode[] getChildNodes(String nodeName){ List list=new ArrayList(); for(int i=0;i<childNodes.length;i++){ if(childNodes[i].getName().equals(nodeName)){ list.add(childNodes[i]); } } XMLNode[] nodes=new XMLNode[list.size()]; list.toArray(nodes); return nodes; } public int getChildNodeCount() { return childNodes.length; } public void addChildNodes(XMLNode child) { if(childNodes.length==0){ childNodes=new XMLNode[1]; childNodes[0]=child; }else{ int len=childNodes.length; XMLNode[] buf=new XMLNode[len+1]; System.arraycopy(childNodes,0,buf,0,len); buf[len]=child; childNodes=buf; } } public String[] getAttributeNames(){ String[] names=new String[attrNames.size()]; attrNames.toArray(names); return names; } public String getAttribute(String key){ int pos=attrNames.indexOf(key); if(pos!=-1){ return (String)attrValues.get(pos); } return null; } public void setAttribute(String key,String value){ if(attrNames.size()==0){ attrNames=new ArrayList(); attrValues=new ArrayList(); } int pos=attrNames.indexOf(key); if(pos!=-1){ attrValues.set(pos,value); }else{ attrNames.add(key); attrValues.add(value); } } //查询节点 public XMLNode searchNode(String path){ String[] paths=path.split("/"); List list=new ArrayList(); for(int i=0;i<paths.length;i++){ if(paths[i].length()>0){ list.add(paths[i]); } } paths=new String[list.size()]; list.toArray(paths); return searchNode(this,0,paths); } private XMLNode searchNode(XMLNode root,int index,String[] paths) { if (root.getName().equals(paths[index])) { if (paths.length == index+1) {//到了终点(找到) return root; }else{ //查询子节点 XMLNode node=null; for(int i=0;i<root.getChildNodeCount();i++){ node=searchNode(root.getChildNode(i),index+1,paths); if(node!=null) return node; } } } return null; } public static XMLNode parseFrom(Element root){ XMLNode node=new XMLNode(); node.setName(root.getNodeName()); if(root.getFirstChild()!=null){ String value=root.getFirstChild().getNodeValue(); if(value==null){ value = ""; }else{ value=value.replace('/r',' ').replace('/n',' ').replaceAll(" ",""); } node.setValue(value); }else{ node.setValue(""); } // System.out.println(node.getName()+"="+node.getValue()); setAttributes(node,root); //递归,设置子节点 if(root.hasChildNodes()){ Node child; for(int i=0;i<root.getChildNodes().getLength();i++){ child=root.getChildNodes().item(i); if(child.getNodeType()==Node.ELEMENT_NODE){//节点 node.addChildNodes(parseFrom((Element)child)); } } } return node; } private static void setAttributes(XMLNode node,Element element){ NamedNodeMap map=element.getAttributes(); if(map!=null){ for (int i = 0; i < map.getLength(); i++) { node.setAttribute(map.item(i).getNodeName(), map.item(i).getNodeValue()); // System.out.println("/t#" + map.item(i).getNodeName() + "=" + // map.item(i).getNodeValue()); } } } } /** * FSD * @author 猪神(redvalley) */ package com.common.cfg; public class XMLConfig { private XMLReader xmlFile=new XMLReader(); private XMLNode root=null; public XMLConfig() { } public synchronized void loadConfig(String filename){ xmlFile.reload(filename); root=XMLNode.parseFrom(xmlFile.getRootElement()); } public XMLNode getRoot(){ return root; } public XMLNode getNode(String path){ return root.searchNode(path); } public String getValue(String path){ XMLNode node=root.searchNode(path); if(node!=null){ return node.getValue(); } return null; } public String getValue(String path,String defaultValue){ XMLNode node=root.searchNode(path); if(node!=null){ return node.getValue(); } return defaultValue; } public String getAttribute(String path,String attribute){ return root.searchNode(path).getAttribute(attribute); } public static void main(String[] args) { XMLConfig xc=new XMLConfig(); xc.loadConfig("D:/=develop=/projects/fsd/web/WEB-INF/system-config.xml"); XMLNode node=xc.getNode("/system-config/pod/customer-type"); System.out.println(node.getName()+"字节点数:"+node.getChildNodeCount()); } } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值