Java tutorial
/** * Copyright (c) 2010-2012 love320.com * * Licensed under the Apache License, Version 2.0 (the "License"); * * Founder admin@love320.com */ package com.love320.templateparser.util; import java.io.InputStream; import java.util.Iterator; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @ClassName: ConfigBeanXML * @Description: TODO * @author love320.com * @date 2012-6-16 ?05:21:32 ?? */ public class ConfigBeanXML { private final static Logger logger = LoggerFactory.getLogger(ConfigBeanXML.class); public static Element action(String config, String... paths) { Document document = null; document = getXML(FileUtil.getJar(config));//? // ????? if (paths != null) { for (String path : paths) { if (path != null) { document = merger(document, getXML(FileUtil.get(path)));// ?? } } } return document.getRootElement(); } // ???,??? private static Document merger(Document document, Document doc) { Iterator documentIter = selectNodes(document, "/beans");// if (documentIter.hasNext()) {// Element documentElement = (Element) documentIter.next();//?Element Iterator dociter = selectNodes(doc, "/beans/bean");// while (dociter.hasNext()) {//?? Element docElement = (Element) dociter.next();//?Element deleteElement(document, docElement.attributeValue("id"));// documentElement.add(docElement.createCopy());//?, } } return document; } //? private static void deleteElement(Document document, String beanId) { Iterator documentIter = selectNodes(document, "/beans/bean[@id='" + beanId + "']");// while (documentIter.hasNext()) { Element element = (Element) documentIter.next();//?Element element.detach();// } } //??? private static Iterator selectNodes(Document document, String xpath) { List documentList = document.selectNodes(xpath);//?beans Iterator documentIter = documentList.iterator();// return documentIter; } // ?xml private static Document getXML(InputStream is) { SAXReader sax = new SAXReader(); Document document = null; try { document = sax.read(is);// ? } catch (DocumentException e) { logger.error("DocumentException", e); } return document; } }