XML文件解析示例
2017-07-01 14:34
330 查看
从所周知,XML文件解析有SAX和DOM
其中DOM需要一次性将xml文件读入内存,构建DOM树
SAX无须在一开始将xml文件读入内存,采用事件驱动,文件读入的过程,即解析的过程。
除了优秀的Dom4j解析,本文介绍下apche里的解析器。
XPath使用路径表达式识别XML文档里的节点
http://www.cnblogs.com/skyblue/archive/2008/06/19/900187.html
示例:
其中DOM需要一次性将xml文件读入内存,构建DOM树
SAX无须在一开始将xml文件读入内存,采用事件驱动,文件读入的过程,即解析的过程。
除了优秀的Dom4j解析,本文介绍下apche里的解析器。
XPath使用路径表达式识别XML文档里的节点
http://www.cnblogs.com/skyblue/archive/2008/06/19/900187.html
示例:
package com.zte.sunquan.demo.xml; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.EnvironmentConfiguration; import org.apache.commons.configuration.HierarchicalConfiguration; import org.apache.commons.configuration.XMLConfiguration; import org.apache.commons.configuration.event.ConfigurationEvent; import org.apache.commons.configuration.event.ConfigurationListener; import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; import org.apache.commons.configuration.reloading.ReloadingStrategy; import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import java.util.concurrent.TimeUnit; /** * Created by 10184538 on 2017/6/25. */ public class XmlRead { private String configFile = "/config.xml"; private String rootElementName = "properties"; private XMLConfiguration cfg; public void loadCfg(Class cls) { cfg = new XMLConfiguration(); cfg.setRootElementName(rootElementName); cfg.setAttributeSplittingDisabled(true); try { cfg.load(cls.getResourceAsStream(configFile)); FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy(); strategy.setRefreshDelay(1); cfg.setFileName("config.xml");//如果设置了reloading-strategy则必须设置文件 cfg.setReloadingStrategy(strategy); } catch (ConfigurationException e) { e.printStackTrace(); } } private void loadConfig(XMLConfiguration rootCfg) { for (HierarchicalConfiguration cfg : rootCfg.configurationsAt("property")) { System.out.println(cfg.getString("[@name]")); } } @Before public void init() { loadCfg(XmlRead.class); } @Test public void getCommonConfig() { Assert.assertEquals("default-topic", cfg.getString("property.[@name]")); Assert.assertEquals("China", cfg.getString("property.country")); Assert.assertEquals("China", cfg.getString("property(0).country")); Assert.assertEquals("Japan", cfg.getString("property(1).country")); } @Test public void getConfigByXPath() throws ConfigurationException { cfg.setExpressionEngine(new XPathExpressionEngine()); Assert.assertEquals("China", cfg.getString("property[@name = 'default-topic']/country")); Assert.assertEquals("Japan", cfg.getString("property[num>11]/country")); Assert.assertEquals("Japan", cfg.getString("property[last()]/country")); } @Test public void getEnvironmentConfig() { EnvironmentConfiguration config = new EnvironmentConfiguration(); Assert.assertEquals("C:\\Program Files\\Java\\jdk1.8.0_91", config.getString("JAVA_HOME")); } @Test public void whenConfigChange() throws InterruptedException, ConfigurationException { cfg = new XMLConfiguration("config.xml"); FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy(); strategy.setRefreshDelay(1); cfg.setReloadingStrategy(strategy); while (true) { System.out.println(cfg.getString("property.country")); TimeUnit.SECONDS.sleep(2); } } @Test public void whenConfigChangeListener() throws InterruptedException, ConfigurationException { cfg = new XMLConfiguration("config.xml"); FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy(); strategy.setRefreshDelay(1); cfg.setReloadingStrategy(strategy); cfg.addConfigurationListener(new ConfigurationListener() { @Override public void configurationChanged(ConfigurationEvent event) { if (!event.isBeforeUpdate()) { // only display events after the modification was done System.out.println("Received event!"); System.out.println("Type = " + event.getType()); if (event.getPropertyName() != null) { System.out.println("Property name = " + event.getPropertyName()); } if (event.getPropertyValue() != null) { System.out.println("Property value = " + event.getPropertyValue()); } } } }); while (true) { System.out.println(cfg.getString("property.country")); TimeUnit.SECONDS.sleep(2); } } }
相关文章推荐
- 解析XML文件示例
- java使用dom4j解析xml配置文件实现抽象工厂反射示例
- Java用SAX解析XML文件示例
- 使用pull方式解析xml文件示例:
- java实现简单解析XML文件功能示例
- XML_Java_ XML_利用Jdom(Java)解析xml文件 示例
- c# 将xml解析成一个类,和解析tomcat配置文件示例
- ASP.NET中读取XML文件信息的4种方法与示例代码
- 在JSP开发中使用jdom解析临时存放数据的XML文件
- 使用Digester解析XML文档示例
- 用DOM/JDOM解析XML文件
- xml的复杂解析取值和节点插入导出合并后的xml文件
- 使用PHP DOM-XML创建和解析XML文件
- XML文件解析
- 使用digester 解析xml配置文件
- JAVA高手解析XML配置文件的读取操作
- jsp读取大对象CLOB并生成xml文件示例
- 使用Struts Digester来解析XML文件
- jsp读取大对象CLOB并生成xml文件示例
- Asp.Net(C#)利用XPath解析XML文档示例