/** * Insert location information into Document. * And considering whether there are the same value of table and xpath which have been existed. * If true,do nothing,or insert. * @param xpath * @return Document */ public Document insertLocation( String table,String xpath,String fieldNum,String date ) { if( !hasTableXpath(document,table,xpath) ) { int id = getRuleNum( document );
Element elementRule = document.createElement( "rule" ); Element elementId = document.createElement( "id" ); Element elementTable = document.createElement( "table" ); Element elementLocation = document.createElement( "location" ); Element elementFieldNum = document.createElement( "fieldNum" ); Element elementDate = document.createElement( "date" );
Text idvalue = document.createTextNode( String.valueOf(id+1) ); Text tablevalue = document.createTextNode( table ); Text locationvalue = document.createTextNode( xpath ); Text fieldNumvalue = document.createTextNode( fieldNum ); Text datevalue = document.createTextNode( date );
/** * Insert view information into Document * @param view * @return Document */ public Document insertView( String view ) { return document; }
/** * Whether there is the same value of table and xpath * @param table * @param xpath * @return boolean if has,return true;else false */ public boolean hasTableXpath( Document document,String tableVal,String xpathVal ) { boolean hasFlag = false;
/** * Save rule into xml file * @param document */ public void save( Document document ) { try { // Prepare the DOM document for writing Source source = new DOMSource( document );
// Prepare the output file File file = new File( filePath ); Result result = new StreamResult( file );
// Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform( source, result ); } catch ( TransformerConfigurationException e ) { } catch ( TransformerException e ) { } }
/** * Get all xpath string about table specialed * @param table * @return List */ public List getLocation( String tableVal ) { NodeList tables = document.getElementsByTagName( "table" ); ArrayList list = new ArrayList(); for( int i = 0; i < tables.getLength(); i ++ ) { Node table = tables.item(i).getFirstChild(); String tableValue = table.getNodeValue(); if( tableVal.equalsIgnoreCase(tableValue) ) { Element parent = (Element)tables.item(i).getParentNode(); Node xpath = parent.getElementsByTagName("location").item(0).getFirstChild(); list.add( xpath.getNodeValue() ); System.out.println( xpath.getNodeValue() ); } } return list; }
/** * Get currently number of rule for IE * @return */ private int getRuleNum( Document document ) { NodeList rules = document.getElementsByTagName( "rule" ); return rules.getLength(); }
public List getRules() { NodeList rules = document.getElementsByTagName( "rule" ); ArrayList rulesList = new ArrayList(); for( int i = 0; i < rules.getLength(); i ++ ) { Node node = rules.item( i ); StringBuffer rule = new StringBuffer( "" ); traverse( node,rule ); rulesList.add( rule.toString() ); }
/** * Decide if the node is text, and so must be handled specially * */ private boolean isTextNode( Node n ) { if ( n == null ) return false; short nodeType = n.getNodeType();
* Created on 2004-9-30
*/
package com.yuch.xml;/**
* @author yuch
*///import java.util.Date;
import java.util.List;
import java.util.ArrayList;import java.io.File;
import java.net.URL;import javax.swing.JOptionPane;import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;import com.yuch.ui.Utility;public class RuleXML {
//the specialed xml file storeing IE rules
private final static String ruleXMLFile = "com/yuch/xml/ierules.xml";
private String filePath = null;//absolute path
//Document of xml file
private Document document = null;
/**
* Constructor with no args.
*
*/
public RuleXML() {
URL url = RuleXML.class.getClassLoader().getResource( ruleXMLFile );
filePath = url.toString().substring(6);
File file = new File( url.toString().substring(6) );
document = Utility.parseXMLFile( file,false );
}
/**
* Insert location information into Document.
* And considering whether there are the same value of table and xpath which have been existed.
* If true,do nothing,or insert.
* @param xpath
* @return Document
*/
public Document insertLocation( String table,String xpath,String fieldNum,String date ) {
if( !hasTableXpath(document,table,xpath) ) {
int id = getRuleNum( document );
Element elementRule = document.createElement( "rule" );
Element elementId = document.createElement( "id" );
Element elementTable = document.createElement( "table" );
Element elementLocation = document.createElement( "location" );
Element elementFieldNum = document.createElement( "fieldNum" );
Element elementDate = document.createElement( "date" );
Text idvalue = document.createTextNode( String.valueOf(id+1) );
Text tablevalue = document.createTextNode( table );
Text locationvalue = document.createTextNode( xpath );
Text fieldNumvalue = document.createTextNode( fieldNum );
Text datevalue = document.createTextNode( date );
elementRule.appendChild( elementId );
elementRule.appendChild( elementTable );
elementRule.appendChild( elementLocation );
elementRule.appendChild( elementFieldNum );
elementRule.appendChild( elementDate );
elementId.appendChild( idvalue );
elementTable.appendChild( tablevalue );
elementLocation.appendChild( locationvalue );
elementFieldNum.appendChild( fieldNumvalue );
elementDate.appendChild( datevalue ); NodeList rules = document.getElementsByTagName( "rules" );
Element elementRules = (Element)rules.item(0);
elementRules.appendChild( elementRule );
}
else {
JOptionPane.showMessageDialog( null,"已存在相同的规则","提示",
JOptionPane.INFORMATION_MESSAGE );
}
return document;
}
* Insert view information into Document
* @param view
* @return Document
*/
public Document insertView( String view ) {
return document;
}
/**
* Whether there is the same value of table and xpath
* @param table
* @param xpath
* @return boolean if has,return true;else false
*/
public boolean hasTableXpath( Document document,String tableVal,String xpathVal ) {
boolean hasFlag = false;
NodeList tables = document.getElementsByTagName( "table" );
for( int i = 0; i < tables.getLength(); i ++ ) {
Node table = tables.item(i).getFirstChild();
String tableValue = table.getNodeValue();
if( tableVal.equalsIgnoreCase(tableValue) ) {
Element parent = (Element)tables.item(i).getParentNode();
Node xpath = parent.getElementsByTagName("location").item(0).getFirstChild();
String xpathValue = xpath.getNodeValue();
if( xpathVal.equalsIgnoreCase(xpathValue) ) {
hasFlag = true;
break;
}
}
}
return hasFlag;
}
/**
* Save rule into xml file
* @param document
*/
public void save( Document document ) {
try {
// Prepare the DOM document for writing
Source source = new DOMSource( document );
// Prepare the output file
File file = new File( filePath );
Result result = new StreamResult( file );
// Write the DOM document to the file
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform( source, result );
}
catch ( TransformerConfigurationException e ) {
}
catch ( TransformerException e ) {
}
}
/**
* Get all xpath string about table specialed
* @param table
* @return List
*/
public List getLocation( String tableVal ) {
NodeList tables = document.getElementsByTagName( "table" );
ArrayList list = new ArrayList();
for( int i = 0; i < tables.getLength(); i ++ ) {
Node table = tables.item(i).getFirstChild();
String tableValue = table.getNodeValue();
if( tableVal.equalsIgnoreCase(tableValue) ) {
Element parent = (Element)tables.item(i).getParentNode();
Node xpath = parent.getElementsByTagName("location").item(0).getFirstChild();
list.add( xpath.getNodeValue() );
System.out.println( xpath.getNodeValue() );
}
} return list;
}
/**
* Get currently number of rule for IE
* @return
*/
private int getRuleNum( Document document ) {
NodeList rules = document.getElementsByTagName( "rule" );
return rules.getLength();
}
public List getRules() {
NodeList rules = document.getElementsByTagName( "rule" );
ArrayList rulesList = new ArrayList();
for( int i = 0; i < rules.getLength(); i ++ ) {
Node node = rules.item( i );
StringBuffer rule = new StringBuffer( "" );
traverse( node,rule );
rulesList.add( rule.toString() );
}
return rulesList;
}
private void traverse( Node node,StringBuffer rule ) {
if( isTextNode(node) ) {
String text = node.getNodeValue().trim();
rule.append( text + "##" );
}
Node child = node.getFirstChild();
while ( child != null ) {
traverse( child,rule );
child = child.getNextSibling();
}
}
/**
* Decide if the node is text, and so must be handled specially
*
*/
private boolean isTextNode( Node n ) {
if ( n == null ) return false;
short nodeType = n.getNodeType();
return nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.TEXT_NODE;
}
/**
* Test class
* @param args
*/
public static void main( String args[] ) {
RuleXML test = new RuleXML();
// Document document = test.insertLocation( "Gene","/html/body/table/","1",new Date().toString() );
// test.save( document );
// test.getLocation( "Factor" );
// System.out.println( "over..." );
ArrayList rules = (ArrayList)test.getRules();
for( int i = 0; i < rules.size(); i ++ ) {
String rule = (String)rules.get(i);
String[] tokens = rule.split( "##" );
System.out.print( tokens[1] + " " );
System.out.print( tokens[2] + " " );
System.out.print( tokens[3] + " " );
System.out.print( tokens[4] );
System.out.println();
}
}
}
我现在要实现的是 在jsp页面选择一个本地的xml文件
提交到servlet 解析xml,将内容插入数据库导出是从数据库查询出结果集 点击导出
导出成xml文件
请问有这样的代码吗
谢谢