1. 程式人生 > >讀取xml檔案,並將其存入資料庫中

讀取xml檔案,並將其存入資料庫中

import java.io.*;
import javax.xml.parsers.*;

import org.xml.sax.*;
import org.w3c.dom.*;

public class DOMDemo {

 public static void main(String[] args) throws SAXException, IOException,
   ParserConfigurationException {
  DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance()
    .newDocumentBuilder();
  Document doc = docBuilder.parse(new File("src/dictionary.xml"));
  printXmlHeader(doc.getXmlVersion(), doc.getXmlEncoding());
  printXml(doc.getDocumentElement(), 0);
 }

 /**
  * 輸出xml頭
  *
  * @param version
  * @param encode
  */
 private static void printXmlHeader(String version, String encode) {
  System.out.println("<?xml version=\"" + version + "\" encoding=\""
    + encode + "\">");
 }

 /**
  * 輸出xml的內容
  *
  * @param ele
  *            為節點物件
  * @param depth
  *            為節點深度 .
  */
 private static void printXml(Element ele, int depth) {
  // 根據xml子節點的深度進行縮排
  printTabWithXmlDepth(depth);
  String tagName = ele.getTagName();
  System.out.print("<" + tagName + ">");
  // 輸出該節點的子節點以及內容
  if (ele.hasChildNodes()) {
   NodeList nodeList = ele.getChildNodes();
   System.out.println("有子節點:" + nodeList.getLength());
   String word = nodeList.item(0).getNodeValue();
   String explain = nodeList.item(1).getNodeValue();
   System.out.println("insert into db values(" + word + " ," + explain
     + ")");
   for (int index = 0; index < nodeList.getLength(); index++) {
    Node node = nodeList.item(index);
    // 判斷該子節點是否也是一個節點
    if (node.getNodeType() == Node.ELEMENT_NODE
      && node.getNodeName().equals("oneword")) {
     if (node.hasChildNodes()) {
      System.out.println("hahha");
      if (node.hasChildNodes()) {
       NodeList childlist = node.getChildNodes();
       System.out.println("childchild");
       if (node.getChildNodes().item(0).getNodeType() == node.ELEMENT_NODE
         && node.getChildNodes().item(1)
           .getNodeType() == node.ELEMENT_NODE) {
        //將xml檔案讀取出來組成sql語句插入資料庫
        System.out.println("name--------->"
          + childlist.item(0).getChildNodes()
            .item(0).getNodeValue()
          + "---->"
          + childlist.item(1).getChildNodes()
            .item(0).getNodeValue());
       }
      }
     }
     printXml((Element) node, depth + 1);

    } else if (node.getNodeType() == Node.TEXT_NODE) {
     String value = node.getNodeValue();
     // DOM在獲取TETX_NODE型別的節點的內容時,如果是回車(換行符),解析後的內容是由一個或多個的"\n","\t"組成的字串,而內容(Value)節點解析後的則就是其值,不含"\n","\t"。
     value = value.replaceAll("\n", "").replaceAll("\r", "")
       .trim();
     if (!"".equals(value)) {
      printTabWithXmlDepth(depth + 1); // 由於內容需要在當前節點的情況下進行縮排
      System.out.print(node.getParentNode().getNodeName()
        + "="); // 輸出當前node的名字
      System.out.println(">>>>:" + value);
     }
    }
   }
  }
  // 收尾,如</root>
  printTabWithXmlDepth(depth);
  System.out.println("</" + tagName + ">");
 }

 /**
  * 縮排
  *
  * @param depth
  */
 private static void printTabWithXmlDepth(int depth) {
  for (int i = 0; i < depth; i++) {
   System.out.print("\t");
  }
 }

}