Sometimes we want to disable soapAction of Axis2 to communicate .Net WCF webservice.
Add below in NetworkNode2Stub.java
//adding SOAP soap_headers
_serviceClient.addHeadersToEnvelope(env);
// set the message context with that soap envelope
_messageContext.setEnvelope(env);
// add the message contxt to the operation client
_operationClient.addMessageContext(_messageContext);
// Disable SOAP_ACTION to fix .Net endpoint without soapAction
_operationClient.getOptions().setProperty(
org.apache.axis2.Constants.Configuration.DISABLE_SOAP_ACTION,
org.apache.axis2.Constants.VALUE_TRUE);
//execute the operation client
_operationClient.execute(true);
This blogger is recording some code samples,technical skill of java,.Net,javascript, css, html for myself use. All articles are coming from my own experience and my collections which are from internet world. If you find any material have copy right issue please leave a comment to me. I will delete it immediately. These articles are writing in English or Chinese. Hope these information can help other technical guys. Thanks for reading.
Wednesday, January 20, 2010
Friday, January 8, 2010
Tomcat 5.X setup jndi
1. Copy jdbc driver to Tomcat 5.X\common\lib
2. Romove all commons-dbcp-x.x.x,commons-pool-x.x and jdbc driver from application/WEB-INF/lib
3. Add Context and jndi parameters in server.xml file as below:
Tomcat 5.0.xx:
(the entity way) was the only way that worked.
[server.xml]:
<Service...>
<Engine...>
<Host...>
...
<Context crossContext="true" docBase="/opt/tomcat5/webapps/oracleTest" path="/oracleTest" reloadable="true">
<Resource name="jdbc/myoracle" auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/myoracle">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.OracleDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:@15.1.1.40:1521:dbastra</value>
</parameter>
<parameter>
<name>username</name>
<value>uuuuuu</value>
</parameter>
<parameter>
<name>password</name>
<value>xxxxxxxx</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
</ResourceParams>
<Resource name="jdbc/myoracle5" auth="Container"
type="javax.sql.DataSource" />
</Context>
</Host>
</Engine>
</Service>
Tomcat 5.5.xx:
(the attributes way) was the only way that worked.
[server.xml]:
<Service...>
<Engine...>
<Host...>
...
<Context path="/oracleTest" reloadable="true"
docBase="/opt/tomcat5/webapps/oracleTest">
<Logger className="org.apache.catalina.logger.SystemOutLogger"
verbosity="4" timestamp="true" />
<Resource name="jdbc/myoracle5" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@15.1.1.40:1521:dbastra" username="aradmin"
password="ar#admin#" maxActive="20" maxIdle="10" maxWait="-1" />
</Context>
</Host>
</Engine>
</Service>
for BOTH versions:
Here there are some points we should take a look:
<Context> in [server.xml] !!
"Please note that for tomcat 5, unlike tomcat 4.x, it is NOT recommended to place <Context> elements directly in the server.xml file. Instead, put them in the META-INF/context.xml directory of your WAR file or the conf directory as described above."
http://tomcat.apache.org/tomcat-5.5-doc/config/context.html
And if we cut the <Context>..</Context> body we described above from the server.xml and paste it into [META-INF/context.xml] or into [conf/Catalina/localhost/oracleTest.xml], it works...
<?xml version='1.0' encoding='utf-8'?>
<Context crossContext="true" docBase="/forge/oracleTest" path="/oracleTest" reloadable="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_DBTest_log." suffix=".txt" timestamp="true" />
<Resource ... (the one that applies to your version)>
</Context>
GlobalNamingResources:
"GlobalNamingResources element defines the global JNDI resources for the Server. These resources are listed in the server's global JNDI resource context. This context is distinct from the per-web-application JNDI contexts described in the JNDI Resources HOW-TO. The resources defined in this element are not visible in the per-web-application contexts unless you explicitly link them with <ResourceLink> elements."
http://tomcat.apache.org/tomcat-5.5-doc/config/globalresources.html
http://tomcat.apache.org/tomcat-5.5-doc/config/context.html#Resource%20Links
Article From http://forums.sun.com/thread.jspa?threadID=567630&start=15
2. Romove all commons-dbcp-x.x.x,commons-pool-x.x and jdbc driver from application/WEB-INF/lib
3. Add Context and jndi parameters in server.xml file as below:
Tomcat 5.0.xx:
(the entity way) was the only way that worked.
[server.xml]:
<Service...>
<Engine...>
<Host...>
...
<Context crossContext="true" docBase="/opt/tomcat5/webapps/oracleTest" path="/oracleTest" reloadable="true">
<Resource name="jdbc/myoracle" auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/myoracle">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.OracleDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:@15.1.1.40:1521:dbastra</value>
</parameter>
<parameter>
<name>username</name>
<value>uuuuuu</value>
</parameter>
<parameter>
<name>password</name>
<value>xxxxxxxx</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
</ResourceParams>
<Resource name="jdbc/myoracle5" auth="Container"
type="javax.sql.DataSource" />
</Context>
</Host>
</Engine>
</Service>
Tomcat 5.5.xx:
(the attributes way) was the only way that worked.
[server.xml]:
<Service...>
<Engine...>
<Host...>
...
<Context path="/oracleTest" reloadable="true"
docBase="/opt/tomcat5/webapps/oracleTest">
<Logger className="org.apache.catalina.logger.SystemOutLogger"
verbosity="4" timestamp="true" />
<Resource name="jdbc/myoracle5" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@15.1.1.40:1521:dbastra" username="aradmin"
password="ar#admin#" maxActive="20" maxIdle="10" maxWait="-1" />
</Context>
</Host>
</Engine>
</Service>
for BOTH versions:
Here there are some points we should take a look:
<Context> in [server.xml] !!
"Please note that for tomcat 5, unlike tomcat 4.x, it is NOT recommended to place <Context> elements directly in the server.xml file. Instead, put them in the META-INF/context.xml directory of your WAR file or the conf directory as described above."
http://tomcat.apache.org/tomcat-5.5-doc/config/context.html
And if we cut the <Context>..</Context> body we described above from the server.xml and paste it into [META-INF/context.xml] or into [conf/Catalina/localhost/oracleTest.xml], it works...
<?xml version='1.0' encoding='utf-8'?>
<Context crossContext="true" docBase="/forge/oracleTest" path="/oracleTest" reloadable="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_DBTest_log." suffix=".txt" timestamp="true" />
<Resource ... (the one that applies to your version)>
</Context>
GlobalNamingResources:
"GlobalNamingResources element defines the global JNDI resources for the Server. These resources are listed in the server's global JNDI resource context. This context is distinct from the per-web-application JNDI contexts described in the JNDI Resources HOW-TO. The resources defined in this element are not visible in the per-web-application contexts unless you explicitly link them with <ResourceLink> elements."
http://tomcat.apache.org/tomcat-5.5-doc/config/globalresources.html
http://tomcat.apache.org/tomcat-5.5-doc/config/context.html#Resource%20Links
Article From http://forums.sun.com/thread.jspa?threadID=567630&start=15
Tuesday, January 5, 2010
Parse Large xml using dom4j
dom4j提供了基于事件的模型来操作xml文档。利用该模型开发人员可以一部分、一部分的处理XML文档,而不需要将整个XML文档都加载到内存中。例如:假想你要处理一个非常大的XML文档,它可能是由数据库的某张数据表而来的。如下所示:
<ROWSET>
<ROW ID="1">
...
</ROW>
<ROW ID="2">
...
</ROW>
...
<ROW ID="N">
...
</ROW>
</ROWSET>
我们可以在某一时间只处理一个ROW节点,而不必立刻将文档的所有内容加载到内存中。dom4j提供一个基于事件的模型来实现它。我们可以注册一个事件处理器来处理一个或多个路径表达式。事件处理器会在注册路径的开始和结束时被调用执行。当注册路径的开始标签找到时执行事件处理器的 onStart()方法,当注册路径的结束标签被找到时执行事件处理器的onEnd()方法。
onStart()和onEnd()方法传递一个ElementPath实例参数,这个实例既为根据注册路径遍历xml文档时的当前节点(Element)。如果想对遍历的当前节点进行操作,可以在onEnd()方法中对当前节点调用detach()方法保存改。
下面是示例代码:
SAXReader reader = new SAXReader();
reader.addHandler( "/ROWSET/ROW",
new ElementHandler() {
public void onStart(ElementPath path) {
// do nothing here...
}
public void onEnd(ElementPath path) {
// process a ROW element
Element row = path.getCurrent();
Element rowSet = row.getParent();
Document document = row.getDocument();
...
// prune the tree
row.detach();
}
}
);
Document document = reader.read(url);
上面的办法解决了读的问题可是写的问题还没有解决。我命由我不由天(吹牛皮),畅游dom4j的doc文档找到如下几个方法
startDocument()
writeOpen();
writeClose();
endDocument()
动手一试,问题搞定,看来牛皮没白吹。下面是示例代码:
/**
* 数据写入xml文件
* @param filePath 目标xml文件的存放路径
* @return
*/
public boolean writeXML(String filePath){
XMLWriter out;
try {
/*
* 创建XMLWriter对象,设置XML编码,解决中文问题。
*/
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
outputFormat.setEncoding("GBK");
out = new XMLWriter(new FileWriter(filePath),outputFormat);
out.startDocument();
Element rootElement = DocumentHelper.createElement("mans");
out.writeOpen(rootElement);
/*
* 向mans节点写入子节点
*/
for(int i=0 ; i<1000000 ; i++){
Element man = createManElement(new Long(i), "shuhang"+i);//用于创建节点的方法
out.write(man);
System.out.println(" the loop index is : " + i);
}
out.writeClose(rootElement);
out.endDocument();
out.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (SAXException e) {
e.printStackTrace();
return false;
}
}
新问题出现。对于读取数据的方法,在onEnd()方法中只是进行对Element的简单操作而已,若要对Element进行复杂的处理怎么办,如将Element节点的数据写入数据库,无法在onEnd()方法中加入过多的代码,因为无法通过编译。仔细想了一下dom4j的注册事件处理器应该用的是模板方法,通过钩子将用户的操作加入到模板中去。何不自己写一个事件处理器来完成我们自己的定制操作,代码如下:
public class ManElementHandler implements ElementHandler {
public String mdbName;
public ManElementHandler(){
}
public ManElementHandler(String mdbName){
this.mdbName = mdbName;
}
public boolean saveMan(Element element, String mdbName){
return true;
}
public void onEnd(ElementPath arg0) {
Element row = arg0.getCurrent();
Element rowSet = row.getParent();
Document document = row.getDocument();
Element root = document.getRootElement();
Iterator it = root.elementIterator();
while(it.hasNext()){
Element element = (Element)it.next();
System.out.println(" id : " + element.elementText("id") + " name : " + element.elementText("name"));
saveMan(element, this.mdbName);
}
row.detach();
}
public void onStart(ElementPath path) {
}
}
下面给出完整的实例代码。该实例首先创建一个xml文档,然后读取xml文档中的数据并将数据写入Access数据库中。测试时使用的文件大小为125M未发生内存溢出。
//*****************************************************************************************************************
package com;
import java.sql.Connection;
import java.sql.DriverManager;
/**
* 获取Access数据库的连接
* @author 佛山无影脚
* @version 1.0
* Jul 7, 2008 4:35:49 PM
*/
public class AccessMDBUtil {
public static Connection connectMdb(String mdbName) {
if(mdbName == null mdbName.equals("")){
return null;
}
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dburl ="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+mdbName;
Connection conn=DriverManager.getConnection(dburl);
return conn;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
//*******************************************************************************************************************
//*******************************************************************************************************************
package com;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.ElementHandler;
import org.dom4j.ElementPath;
/**
* 定制的事件处理器
* @author 佛山无影脚
* @version 1.0
* Jul 7, 2008 4:35:49 PM
*/
public class ManElementHandler implements ElementHandler {
public String mdbName;//数据库名称 含路径
public ManElementHandler(){
}
public ManElementHandler(String mdbName){
this.mdbName = mdbName;
}
/**
*将Element节点数据保存到数据库
*@param element 遍历XML文档时的当前节点
*@param mdbName 数据库名称 含路径
*@return
*/
public boolean saveMan(Element element, String mdbName){
System.out.println(" the method saveMan in ManElementHandler is execute!");
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
try {
conn= AccessMDBUtil.connectMdb(mdbName);
stmt=conn.createStatement();
String sql = "insert into mans(id,name) values(" + element.elementText("id") + ",'" + element.elementText("name") + "')";
stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return true;
}
public void onEnd(ElementPath arg0) {
Element row = arg0.getCurrent();
Element rowSet = row.getParent();
Document document = row.getDocument();
Element root = document.getRootElement();
Iterator it = root.elementIterator();
while(it.hasNext()){
Element element = (Element)it.next();
System.out.println(" id : " + element.elementText("id") + " name : " + element.elementText("name"));
saveMan(element, this.mdbName);
}
row.detach();
}
public void onStart(ElementPath path) {
}
}
//*************************************************************************************************************************
//*************************************************************************************************************************
package com;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
* 测试
* @author 佛山无影脚
* @version 1.0
* Jul 7, 2008 4:35:49 PM
*/
public class ManTest {
/**
* 创建man节点
* <man><id>1</id><name>佛山无影脚</name></man>
* @param id
* @param name
* @return
*/
public Element createManElement(Long id,String name){
Element manElement = DocumentHelper.createElement("man");
manElement.addElement("id").addText(id.toString());
manElement.addElement("name").addText(name);
return manElement;
}
/**
* 数据写入xml文件
* @param filePath 目标xml文件的存放路径
* @return
*/
public boolean writeXML(String filePath){
XMLWriter out;
try {
/*
* 创建XMLWriter对象,设置XML编码,解决中文问题。
*/
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
outputFormat.setEncoding("GBK");
out = new XMLWriter(new FileWriter(filePath),outputFormat);
out.startDocument();
Element rootElement = DocumentHelper.createElement("mans");
out.writeOpen(rootElement);
/*
* 向mans节点写入子节点
*/
for(int i=0 ; i<1000000 ; i++){
Element man = this.createManElement(new Long(i), "shuhang"+i);
out.write(man);
System.out.println(" the loop index is : " + i);
}
out.writeClose(rootElement);
out.endDocument();
out.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (SAXException e) {
e.printStackTrace();
return false;
}
}
/**
* 从xml文件中读取数据,并将数据写入Access数据
* @param filePath xml文件的存放路径
* @return
*/
public boolean readXML(String filePath){
ManElementHandler manElementHandler = new ManElementHandler("F:\\dom4j\\xmlTest.mdb");
SAXReader reader = new SAXReader();
reader.addHandler( "/mans/man", manElementHandler);
Document document = null;
try {
File file = new File(filePath);
document = reader.read(file);
} catch (DocumentException e) {
e.printStackTrace();
return false;
}
return true;
}
public static void main(String[] args){
XMLReader reader = null;
long startTime = System.currentTimeMillis();
ManTest mantest = new ManTest();
mantest.writeXML("f:\\dom4j\\mans.xml");
mantest.readXML("f:\\dom4j\\mans.xml");
long endTime = System.currentTimeMillis();
System.out.println(" is end! the millis is : " + (endTime - startTime));
}
}
//*************************************************************************************************************************
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/shuhang1106/archive/2008/07/09/2627581.aspx
<ROWSET>
<ROW ID="1">
...
</ROW>
<ROW ID="2">
...
</ROW>
...
<ROW ID="N">
...
</ROW>
</ROWSET>
我们可以在某一时间只处理一个ROW节点,而不必立刻将文档的所有内容加载到内存中。dom4j提供一个基于事件的模型来实现它。我们可以注册一个事件处理器来处理一个或多个路径表达式。事件处理器会在注册路径的开始和结束时被调用执行。当注册路径的开始标签找到时执行事件处理器的 onStart()方法,当注册路径的结束标签被找到时执行事件处理器的onEnd()方法。
onStart()和onEnd()方法传递一个ElementPath实例参数,这个实例既为根据注册路径遍历xml文档时的当前节点(Element)。如果想对遍历的当前节点进行操作,可以在onEnd()方法中对当前节点调用detach()方法保存改。
下面是示例代码:
SAXReader reader = new SAXReader();
reader.addHandler( "/ROWSET/ROW",
new ElementHandler() {
public void onStart(ElementPath path) {
// do nothing here...
}
public void onEnd(ElementPath path) {
// process a ROW element
Element row = path.getCurrent();
Element rowSet = row.getParent();
Document document = row.getDocument();
...
// prune the tree
row.detach();
}
}
);
Document document = reader.read(url);
上面的办法解决了读的问题可是写的问题还没有解决。我命由我不由天(吹牛皮),畅游dom4j的doc文档找到如下几个方法
startDocument()
writeOpen();
writeClose();
endDocument()
动手一试,问题搞定,看来牛皮没白吹。下面是示例代码:
/**
* 数据写入xml文件
* @param filePath 目标xml文件的存放路径
* @return
*/
public boolean writeXML(String filePath){
XMLWriter out;
try {
/*
* 创建XMLWriter对象,设置XML编码,解决中文问题。
*/
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
outputFormat.setEncoding("GBK");
out = new XMLWriter(new FileWriter(filePath),outputFormat);
out.startDocument();
Element rootElement = DocumentHelper.createElement("mans");
out.writeOpen(rootElement);
/*
* 向mans节点写入子节点
*/
for(int i=0 ; i<1000000 ; i++){
Element man = createManElement(new Long(i), "shuhang"+i);//用于创建节点的方法
out.write(man);
System.out.println(" the loop index is : " + i);
}
out.writeClose(rootElement);
out.endDocument();
out.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (SAXException e) {
e.printStackTrace();
return false;
}
}
新问题出现。对于读取数据的方法,在onEnd()方法中只是进行对Element的简单操作而已,若要对Element进行复杂的处理怎么办,如将Element节点的数据写入数据库,无法在onEnd()方法中加入过多的代码,因为无法通过编译。仔细想了一下dom4j的注册事件处理器应该用的是模板方法,通过钩子将用户的操作加入到模板中去。何不自己写一个事件处理器来完成我们自己的定制操作,代码如下:
public class ManElementHandler implements ElementHandler {
public String mdbName;
public ManElementHandler(){
}
public ManElementHandler(String mdbName){
this.mdbName = mdbName;
}
public boolean saveMan(Element element, String mdbName){
return true;
}
public void onEnd(ElementPath arg0) {
Element row = arg0.getCurrent();
Element rowSet = row.getParent();
Document document = row.getDocument();
Element root = document.getRootElement();
Iterator it = root.elementIterator();
while(it.hasNext()){
Element element = (Element)it.next();
System.out.println(" id : " + element.elementText("id") + " name : " + element.elementText("name"));
saveMan(element, this.mdbName);
}
row.detach();
}
public void onStart(ElementPath path) {
}
}
下面给出完整的实例代码。该实例首先创建一个xml文档,然后读取xml文档中的数据并将数据写入Access数据库中。测试时使用的文件大小为125M未发生内存溢出。
//*****************************************************************************************************************
package com;
import java.sql.Connection;
import java.sql.DriverManager;
/**
* 获取Access数据库的连接
* @author 佛山无影脚
* @version 1.0
* Jul 7, 2008 4:35:49 PM
*/
public class AccessMDBUtil {
public static Connection connectMdb(String mdbName) {
if(mdbName == null mdbName.equals("")){
return null;
}
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dburl ="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+mdbName;
Connection conn=DriverManager.getConnection(dburl);
return conn;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
//*******************************************************************************************************************
//*******************************************************************************************************************
package com;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.ElementHandler;
import org.dom4j.ElementPath;
/**
* 定制的事件处理器
* @author 佛山无影脚
* @version 1.0
* Jul 7, 2008 4:35:49 PM
*/
public class ManElementHandler implements ElementHandler {
public String mdbName;//数据库名称 含路径
public ManElementHandler(){
}
public ManElementHandler(String mdbName){
this.mdbName = mdbName;
}
/**
*将Element节点数据保存到数据库
*@param element 遍历XML文档时的当前节点
*@param mdbName 数据库名称 含路径
*@return
*/
public boolean saveMan(Element element, String mdbName){
System.out.println(" the method saveMan in ManElementHandler is execute!");
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
try {
conn= AccessMDBUtil.connectMdb(mdbName);
stmt=conn.createStatement();
String sql = "insert into mans(id,name) values(" + element.elementText("id") + ",'" + element.elementText("name") + "')";
stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return true;
}
public void onEnd(ElementPath arg0) {
Element row = arg0.getCurrent();
Element rowSet = row.getParent();
Document document = row.getDocument();
Element root = document.getRootElement();
Iterator it = root.elementIterator();
while(it.hasNext()){
Element element = (Element)it.next();
System.out.println(" id : " + element.elementText("id") + " name : " + element.elementText("name"));
saveMan(element, this.mdbName);
}
row.detach();
}
public void onStart(ElementPath path) {
}
}
//*************************************************************************************************************************
//*************************************************************************************************************************
package com;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
* 测试
* @author 佛山无影脚
* @version 1.0
* Jul 7, 2008 4:35:49 PM
*/
public class ManTest {
/**
* 创建man节点
* <man><id>1</id><name>佛山无影脚</name></man>
* @param id
* @param name
* @return
*/
public Element createManElement(Long id,String name){
Element manElement = DocumentHelper.createElement("man");
manElement.addElement("id").addText(id.toString());
manElement.addElement("name").addText(name);
return manElement;
}
/**
* 数据写入xml文件
* @param filePath 目标xml文件的存放路径
* @return
*/
public boolean writeXML(String filePath){
XMLWriter out;
try {
/*
* 创建XMLWriter对象,设置XML编码,解决中文问题。
*/
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
outputFormat.setEncoding("GBK");
out = new XMLWriter(new FileWriter(filePath),outputFormat);
out.startDocument();
Element rootElement = DocumentHelper.createElement("mans");
out.writeOpen(rootElement);
/*
* 向mans节点写入子节点
*/
for(int i=0 ; i<1000000 ; i++){
Element man = this.createManElement(new Long(i), "shuhang"+i);
out.write(man);
System.out.println(" the loop index is : " + i);
}
out.writeClose(rootElement);
out.endDocument();
out.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (SAXException e) {
e.printStackTrace();
return false;
}
}
/**
* 从xml文件中读取数据,并将数据写入Access数据
* @param filePath xml文件的存放路径
* @return
*/
public boolean readXML(String filePath){
ManElementHandler manElementHandler = new ManElementHandler("F:\\dom4j\\xmlTest.mdb");
SAXReader reader = new SAXReader();
reader.addHandler( "/mans/man", manElementHandler);
Document document = null;
try {
File file = new File(filePath);
document = reader.read(file);
} catch (DocumentException e) {
e.printStackTrace();
return false;
}
return true;
}
public static void main(String[] args){
XMLReader reader = null;
long startTime = System.currentTimeMillis();
ManTest mantest = new ManTest();
mantest.writeXML("f:\\dom4j\\mans.xml");
mantest.readXML("f:\\dom4j\\mans.xml");
long endTime = System.currentTimeMillis();
System.out.println(" is end! the millis is : " + (endTime - startTime));
}
}
//*************************************************************************************************************************
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/shuhang1106/archive/2008/07/09/2627581.aspx
Subscribe to:
Posts (Atom)
-
If you get "This page calls for XML namespace http://richfaces.org/a4j declared with prefix a4j but no taglibrary exists for that names...
-
Method 1: import oracle.sql.*; public class JClob { String tableName = null; // String primaryKey = null; // String primaryValue = null; // ...
-
1.HTTP-binding(ServiceMix) 1.4 各组件间传递消息流的过程如下: 1. HttpClient : Http 客户端运行一个独立的 Java 客户端程序,通过其中的 URLConnection 类连接到 http://...