java自带dom工具使用实例
来源:互联网 发布:ftp上传工具 mac版 编辑:程序博客网 时间:2024/05/22 13:24
代码参考自
黄亿华大神的<<1000行代码读懂Spring(一)- 实现一个基本的IoC容器>>
原网页如下
http://my.oschina.net/flashsword/blog/192551
package com.myspring; import java.io.FileInputStream; import java.io.InputStream; 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 { public static void main(String[] args) { new XmlReader(); } public XmlReader(){ DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); try { DocumentBuilder domBuilder = domfac.newDocumentBuilder(); //默认是工程目录 InputStream is =new FileInputStream("bin/resources/tinyioc.xml"); Document doc = domBuilder.parse(is); Element root = doc.getDocumentElement(); NodeList beans = root.getChildNodes(); if(beans!=null) for (int i = 0; i < beans.getLength(); i++) { Node bean = beans.item(i); if (bean.getNodeName().equals("bean")) { Element el=(Element) bean; System.out.println( el.getAttribute("id") +" "+el.getAttribute("class") ); NodeList propertyNode = el.getElementsByTagName("property"); for (int j = 0; j < propertyNode.getLength(); j++) if(propertyNode.item(j) instanceof Element){ Element e=(Element) propertyNode.item(j); String name=e.getAttribute("name"); String value=e.getAttribute("value").equals("")?e.getAttribute("ref"):e.getAttribute("value"); System.out.println(" "+name+" "+value ); }} } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }xml如下
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="helloWorldService" class="com.myspring.HelloWorldServiceImpl"> <property name="text" value="Hello World!"></property> <property name="out" ref="outputService"></property> </bean> <bean id="outputService" class="com.myspring.OutputService"> </bean> <bean id="autoProxyCreator" class="us.codecraft.tinyioc.aop.AspectJAwareAdvisorAutoProxyCreator"></bean> <bean id="timeInterceptor" class="us.codecraft.tinyioc.aop.TimerInterceptor"></bean> <bean id="aspectjAspect" class="us.codecraft.tinyioc.aop.AspectJExpressionPointcutAdvisor"> <property name="advice" ref="timeInterceptor"></property> <property name="expression" value="execution(* us.codecraft.tinyioc.*.*(..))"></property> </bean></beans>
运行结果如下
helloWorldService com.myspring.HelloWorldServiceImpl
text Hello World!
out outputService
outputService com.myspring.OutputService
autoProxyCreator us.codecraft.tinyioc.aop.AspectJAwareAdvisorAutoProxyCreator
timeInterceptor us.codecraft.tinyioc.aop.TimerInterceptor
aspectjAspect us.codecraft.tinyioc.aop.AspectJExpressionPointcutAdvisor
advice timeInterceptor
expression execution(* us.codecraft.tinyioc.*.*(..))
0 0
- java自带dom工具使用实例
- 使用Java自带DOM工具解析XML
- VS2012自带的 性能分析 工具使用实例
- VS2012自带的 性能分析 工具使用实例
- VS2012自带的 性能分析 工具使用实例
- 【转】VS2012自带的 性能分析 工具使用实例
- VS2012自带的 性能分析 工具使用实例
- Java自带优秀工具
- 使用Java自带SAX工具解析XML
- 使用Java自带SAX工具解析XML
- 使用Java自带SAX工具解析XML
- 关于VS2012自带的 性能分析 工具使用实例(图文介绍)
- VC自带的工具SPY++使用
- VC自带的工具SPY++使用
- 使用Wince 自带的远程工具
- jdk自带常用命令行工具使用
- windows10自带截图工具快捷键使用
- linux自带i2c工具使用
- ArcGIS For Flex报错
- GVIM: 手把手教你把Vim改装成一个IDE编程环境(图文)
- linux下使用gdb调试nasm程序
- GVIM: 手把手教你把Vim改装成一个IDE编程环境(图文) -- 续1
- Struts2:通过action标签向页面传值
- java自带dom工具使用实例
- #ifdef __cplusplus 倒底是什么意思?
- struts2连接数据库实现用户注册与登录
- C++函数指针实例详解(篇五)
- 【Leetcode】Sort List JAVA实现
- ArcGIS For Flex报错二
- 考研or就业
- .bashrc文件
- Ping程序