xml2

public class DealConfXml {
	//配置文件与Document相对应
	private  Map<String,Document> map=new HashMap<String,Document>();
	private  List<ChangeBean> list=new ArrayList<ChangeBean>();
	private  DocumentBuilder builder;
	private  XPathFactory pathFactory;
	private  XPath xpath;
	private  Document doc;
	//基路径
	private  String basePath="src/";
	public DealConfXml(){
		try {
			init();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}
	
	//加载主配置文件
	public  void init() throws Exception{
		//创建DocumentFactory 和DocumentBuilder
	     builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
		//根据地址构建document
		 doc=builder.parse("src/encode2.xml");
		 pathFactory = XPathFactory.newInstance();  
		 xpath = pathFactory.newXPath();
		NodeList nodeList=doc.getElementsByTagName("file");
		if(nodeList!=null&&nodeList.getLength()>0){	
			for(int i=0;i<nodeList.getLength();i++){
				Node node=nodeList.item(i);
				String filepath=xpath.compile("filepath/text()").evaluate(node, XPathConstants.STRING).toString();
				String nodepath=xpath.compile("nodepath/text()").evaluate(node, XPathConstants.STRING).toString();
				String nodevalue=xpath.compile("nodevalue/text()").evaluate(node, XPathConstants.STRING).toString();
				String descript=xpath.compile("descript/text()").evaluate(node, XPathConstants.STRING).toString();
				String type=xpath.compile("type/text()").evaluate(node, XPathConstants.STRING).toString();
				ChangeBean changeBean=new ChangeBean(filepath,nodepath,nodevalue,descript,type);
				System.out.println(changeBean);
				list.add(changeBean);
			}
			
		}
	}
	
	//根据changeBean 加载相关的配置文件的document
	public  void initMap() throws SAXException, IOException,Exception{
		for(int i=0;i<list.size();i++){
			ChangeBean changeBean=list.get(i);
			//避免重复加载配置文件
			if(map.keySet().contains(changeBean.getFilepath())){
				continue;
			}
			Document document=builder.parse(basePath+changeBean.getFilepath());
			map.put(changeBean.getFilepath(), document);
		}
	}
	//根据主配置文件修改其它的配置文件
	public  void update() throws XPathExpressionException{
		for(int i=0;i<list.size();i++){
			ChangeBean changeBean=list.get(i);
			Object result=xpath.compile(changeBean.getNodepath()).evaluate(map.get(changeBean.getFilepath()),XPathConstants.NODE );
			Element node=(Element)result;
			if("0".equals(changeBean.getType())){
				node.getFirstChild().setNodeValue(changeBean.getNodevalue());
			}else if("1".equals(changeBean.getType())){
				node.setAttribute(getAttribute(changeBean.getNodepath()), changeBean.getNodevalue());
			}
			System.out.println(node.getFirstChild().getNodeValue());
		}
	}
	
	//将xml的修改保存到文件
	public  void save() throws Exception{
		TransformerFactory transformerFactory = TransformerFactory.newInstance();  
		Transformer transformer = transformerFactory.newTransformer();  
	    Set<String> key=map.keySet();
		Iterator<String> iterator=key.iterator();
		while(iterator.hasNext()){
			String filepath=iterator.next();
			StreamResult streamResult = new StreamResult(basePath+filepath); 
			DOMSource domSource = new DOMSource(map.get(filepath));  
			Properties property=transformer.getOutputProperties();
			property.setProperty(OutputKeys.ENCODING, "UTF-8");
			property.setProperty(OutputKeys.INDENT, "yes");//设置缩进
			transformer.setOutputProperties(property);
			transformer.transform(domSource, streamResult);
		}
			
}
		
	
	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值