龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

Java、XML与数据库编程实践 (二)

时间:2009-12-23 15:42来源:未知 作者:admin 点击:
分享到:
文件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) { xmlfil

  文件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 !
  
精彩图集

赞助商链接