用Dom4J解析带命名空间的XML文件
Dom4J是一个开源的优秀的XML解析API,现在越来越多的项目中开始采用这种解析方式,其中包含了著名的Hibernate。这里我们使用Dom4J解析一个带命名空间的CXF的Spring配置文件。先导入dom4j-1.6.1.jar
spring 配置文件 applicationContext-cxf.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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"
default-autowire="byName" default-lazy-init="true">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<bean id="jaxWsServiceFactoryBean" class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean" scope="prototype">
<property name="wrapped" value="true"/>
<property name="dataBinding" ref="aegisBean"/>
<property name="serviceConfigurations">
<list>
<bean class="org.apache.cxf.jaxws.support.JaxWsServiceConfiguration"/>
<bean class="org.apache.cxf.aegis.databinding.AegisServiceConfiguration"/>
<bean class="org.apache.cxf.service.factory.DefaultServiceConfiguration"/>
</list>
</property>
</bean>
<bean id="aegisBean" class="org.apache.cxf.aegis.databinding.AegisDatabinding"/>
<jaxws:endpoint id="WSpersonService" implementor="#personService" address="/PersonService">
<jaxws:serviceFactory>
相关文档:
使用XMLList读取XML数据
XML对象提供了一个返回XMLList类型的方法elements(),既返回所有XML对象的子元素节点,随后我们可以通过for each语句循环则可访问到整个XML数据内容。以上面创建内部XML文档的XML数据为例,详细见如下代码块:
varlist:XMLList=pXml.elements();
foreach(varelement:XMLinlist)
{
// ......
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-configuration (View Source for full doctype...)>
- <!-- Generated by MyEclipse Hibernate Tools.
--> - <!-- 以上几行所有的hibernate.cfg.xml都相同
--> [url=file:///C:/Documents%20and%20Settings/ ......
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using i_salesDAL;
using i_s ......
此XML实例以服务器端部门管理为例:(其界面如下)
private void btn_UpdateGroup_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
& ......
//打开某文件(假设web.config在根目录中)
string filename=Server.MapPath("/") + @"WebApplication1\web.config";
XmlDocument xmldoc= new XmlDocument();
xmldoc.Load(filename);
//得到顶层节点列表
......