TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
通常一些应用框架都会用 XML 作为配置,而且很多都支持多个 XML 文件,例如 Struts 框架可以配置多个 struts-config-xxx.xml 文件,Spring 也允许你用多个 applicationContext-xxx.xml 文件,再比如 DWR 也是可以由多个 dwr-xxx.xml 依功能或其他方式分开来配置。
我们知道,这样的多个 XML 有相同的规范定义,那么程序如何一并解析它们呢?我看过 ActionServlet 是对 struts-config-xxx.xml 逐个解析的。我这里介绍的一种方法是把那些有着相同规范定义的 XML 合成一个 Document 然后对这个 Document 对象进行处理,如 XPath 查找、进行 DOM 对象操作,就不需要每次到多个 Document 中去查找一遍。
众所周知,对 XML 的操作有两种方式,DOM:XML 映射在内存中一颗树;SAX:基于事件的方式。常用的 XML java 解析组件有 DOM4J(Apache的)、JDOM、和JAXP(Sun的),它们都提供了 DOM 和 SAX 实现和 Xpath 查找。
下面我就介绍用 JDOM 完成对多个 XML 文件进行合并得到一个 Document 对象,实现代码如下:
- /*
- * Created on Sep 23, 2007
- */
-
- import java.io.*;
- import java.util.List;
-
- import org.jdom.Document;
- import org.jdom.Element;
- import org.jdom.JDOMException;
- import org.jdom.input.SAXBuilder;
- import org.jdom.output.*;
-
- /**
- * Combine two or more XML together as one document
- * I put this operation into main method.
- * @author Unmi
- */
- public class CombineXML
- {
-
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception
- {
- Document document = null;
- try {
- SAXBuilder dbf = new SAXBuilder();
- document = (Document) dbf.build(args[0]);
- Element docRoot = document.getRootElement();
-
- for (int i = 1; i < args.length; i++) {
- Document tmpdoc = dbf.build(args[i]);
- List nlt = tmpdoc.getRootElement().getChildren();
- for (int j = 0; j < nlt.size(); ) {
- Element el = (Element)nlt.get(0);
- // get free element
- el.detach();
- docRoot.addContent(el);
- }
- }
- } catch (IOException e) {
- throw new Exception("File not readable.");
- } catch (JDOMException e) {
- throw new Exception("File parsed error.");
- }
-
- //TODO, You can process that document now.
- Format f=Format.getRawFormat();
- f.setEncoding("gb2312");
- f.setIndent(" ");//每一层元素缩排两格
-
- XMLOutputter XMLOut = new XMLOutputter(f);
- XMLOut.output(document, new FileOutputStream("test1.xml"));
-
- }
- }
复制代码 研究这个的目的是,我想做一个工具合并多个 Struts-config 配置文件,然后输入 nath、nction、norward、name 或 type 等中的一个属性值查找到相关的其他属性值,这样能方便程序调试,比如看到哪个 aciton.do 就知道会转向到哪些个页面,附着了哪个 formbean,以及其他相关的属性值是什么。
源码下载:http://file.javaxxz.com/2014/10/10/015751625.zip |
|