Java、XML与数据库编程实践(二)
2008-05-01 05:38
423 查看
文件DbXMLParser.Java封装了对XML文件的操作。
import Javax.XML.parsers.*;
import org.w3c.dom.*;
import org.XML.sax.*;
import Java.io.*;
public class DbXMLParser
{
static String XMLfile;
public DbXMLParser(String filename)
{
XMLfile=filename;
}
public static Element loadDocument()
{
try
{
//工厂
DocumentBuilderFactory dcfactory=DocumentBuilderFactory.newInstance();
//文档构造器
DocumentBuilder db=dcfactory.newDocumentBuilder();
//构造的文档
Document doc=db.parse(XMLfile);
//根元素
Element root=doc.getDocumentElement();
return root;
}catch( ParserConfigurationException e){
System.out.println("ParserConfigurationException");
e.printStackTrace();
}catch(IOException e) {
System.out.println("IOException ");
e.printStackTrace();
}catch(SAXException e) {
System.out.println("SAXException ");
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
return null;
}
public ConnPara getSource()
{
Element root=loadDocument();
if( root==null) { return null; }
NodeList nodes=root.getElementsByTagName("source");
if(nodes.getLength()>0)
{
Node node=nodes.item(0);
String connclass=getChildElementValue(node,"class");
String url=getChildElementValue(node,"url");
String username=getChildElementValue(node,"user");
String password=getChildElementValue(node,"password");
return new ConnPara(connclass,url,username,password);
}
return null;
}
public ConnPara getDest()
{
Element root=loadDocument();
if( root==null) return null;
NodeList nodes=root.getElementsByTagName("dest");
if(nodes.getLength()>0)
{
Node node=nodes.item(0);
String connclass=getChildElementValue(node,"class");
String url=getChildElementValue(node,"url");
String username=getChildElementValue(node,"user");
String password=getChildElementValue(node,"password");
return new ConnPara(connclass,url,username,password);
}
return null;
}
//得到子元素的值
private String getChildElementValue(Node node,String subTagName)
{
String returnString = "";
if(node != null)
{
NodeList children = node.getChildNodes();
for(int innerLoop = 0; innerLoop < children.getLength(); innerLoop++)
{
Node child = children.item(innerLoop);
if(child == null || child.getNodeName() == null || !child.getNodeName().equals(subTagName))
continue;
Node grandChild = child.getFirstChild();
if(grandChild.getNodeValue() != null)
return grandChild.getNodeValue();
}
}
return returnString;
}
}
import Javax.XML.parsers.*;
import org.w3c.dom.*;
import org.XML.sax.*;
import Java.io.*;
public class DbXMLParser
{
static String XMLfile;
public DbXMLParser(String filename)
{
XMLfile=filename;
}
public static Element loadDocument()
{
try
{
//工厂
DocumentBuilderFactory dcfactory=DocumentBuilderFactory.newInstance();
//文档构造器
DocumentBuilder db=dcfactory.newDocumentBuilder();
//构造的文档
Document doc=db.parse(XMLfile);
//根元素
Element root=doc.getDocumentElement();
return root;
}catch( ParserConfigurationException e){
System.out.println("ParserConfigurationException");
e.printStackTrace();
}catch(IOException e) {
System.out.println("IOException ");
e.printStackTrace();
}catch(SAXException e) {
System.out.println("SAXException ");
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
return null;
}
public ConnPara getSource()
{
Element root=loadDocument();
if( root==null) { return null; }
NodeList nodes=root.getElementsByTagName("source");
if(nodes.getLength()>0)
{
Node node=nodes.item(0);
String connclass=getChildElementValue(node,"class");
String url=getChildElementValue(node,"url");
String username=getChildElementValue(node,"user");
String password=getChildElementValue(node,"password");
return new ConnPara(connclass,url,username,password);
}
return null;
}
public ConnPara getDest()
{
Element root=loadDocument();
if( root==null) return null;
NodeList nodes=root.getElementsByTagName("dest");
if(nodes.getLength()>0)
{
Node node=nodes.item(0);
String connclass=getChildElementValue(node,"class");
String url=getChildElementValue(node,"url");
String username=getChildElementValue(node,"user");
String password=getChildElementValue(node,"password");
return new ConnPara(connclass,url,username,password);
}
return null;
}
//得到子元素的值
private String getChildElementValue(Node node,String subTagName)
{
String returnString = "";
if(node != null)
{
NodeList children = node.getChildNodes();
for(int innerLoop = 0; innerLoop < children.getLength(); innerLoop++)
{
Node child = children.item(innerLoop);
if(child == null || child.getNodeName() == null || !child.getNodeName().equals(subTagName))
continue;
Node grandChild = child.getFirstChild();
if(grandChild.getNodeValue() != null)
return grandChild.getNodeValue();
}
}
return returnString;
}
}
相关文章推荐
- Java、XML与数据库编程实践(二)
- Java、XML与数据库编程实践
- Java、XML与数据库编程实践(一)
- Java、XML与数据库编程实践(一)
- Java、XML与数据库编程实践
- Java、XML与数据库编程实践(四)
- Java、XML与数据库编程实践(三)
- Java、XML与数据库编程实践
- Java、XML与数据库编程实践(四)
- Java数据库编程、XML解析技术
- Java网络编程丶数据库编程丶XML解析技术。
- Spring 总结XML配置的十二个最佳实践-Java基础-Java-编程开发
- Java数据库编程、Java与XML解析技术
- Java数据库编程及Java XML解析技术
- 基于Java的XML文件模拟数据库进行增删改查操作
- java数据库编程--处理大对象数据(Blob类)
- JDBC与JAVA数据库编程
- Java数据库编程中的几个常用技巧
- C/C++、VC/MFC、嵌入式、Java/XML、数据库/多线程/网络/图形/数据结构/数学”系列学习精粹
- Java并发编程实践笔记之—并发容器(Concurrent Collections)