DOM-读取数据库,将数据写入XML,解析XML..

系统 1465 0

DOM-读取数据库,将数据写入XML,解析XML..
数据的读取
将数据从数据库中读取,并写入XML。代码如下:

package com.mky.xml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.crimson.tree.XmlDocument;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class WriteXml {
static Connection conn = null;
static String sql;
static String url = "jdbc:oracle:thin:@localhost:1521:zxg";
public static void main(String[] args) {
  try {
   Class.forName("oracle.jdbc.driver.OracleDriver");
   conn = DriverManager.getConnection(url, "scott", "tiger");
   Statement st = conn.createStatement();
   ResultSet rs = st.executeQuery("select * from dept");
   DocumentBuilderFactory factory = DocumentBuilderFactory
     .newInstance();
   DocumentBuilder builder = factory.newDocumentBuilder();
   Document doc = builder.newDocument();
   Element comp = doc.createElement("COMP");
   while (rs.next()) {
    Element person = doc.createElement("PERSON");

    Element deptno = doc.createElement("DEPTNO");
    deptno.appendChild(doc.createTextNode(String.valueOf(rs
      .getInt(1))));
    person.appendChild(deptno);

    Element dname = doc.createElement("DNAME");
    dname.appendChild(doc.createTextNode(rs.getString(2)));
    person.appendChild(dname);

    Element loc = doc.createElement("LOC");
    loc.appendChild(doc.createTextNode(rs.getString(3)));
    person.appendChild(loc);

    comp.appendChild(person);
   }
   rs.close();
   st.close();
   conn.close();
   doc.appendChild(comp);
   ((XmlDocument) doc)
     .write(new FileOutputStream(new File("dept.xml")));
   System.out.println("从数据库读出数据,写入xml ..操作成功!!!");
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SQLException e1) {
   e1.printStackTrace();
  } catch (ParserConfigurationException e2) {
   e2.printStackTrace();
  } catch (FileNotFoundException e3) {
   e3.printStackTrace();
  } catch (IOException e4) {
   e4.printStackTrace();
  }
}
}

生成dept.xml  内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<COMP>
  <PERSON>
    <DEPTNO>10</DEPTNO>
    <DNAME>ACCOUNTING</DNAME>
    <LOC>NEW YORK</LOC>
  </PERSON>
  <PERSON>
    <DEPTNO>20</DEPTNO>
    <DNAME>RESEARCH</DNAME>
    <LOC>DALLAS</LOC>
  </PERSON>
  <PERSON>
    <DEPTNO>30</DEPTNO>
    <DNAME>SALES</DNAME>
    <LOC>CHICAGO</LOC>
  </PERSON>
  <PERSON>
    <DEPTNO>40</DEPTNO>
    <DNAME>OPERATIONS</DNAME>
    <LOC>BOSTON</LOC>
  </PERSON>
</COMP>
从生成的XML中读取数据。代码如下:

package com.mky.xml;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class ReadXML {

public static void main(String[] args) {
  try {
   DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
     .newInstance();
   DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
   Document doc = docBuilder.parse(new File("dept.xml"));
   doc.getDocumentElement().normalize();
   System.out.println("Root element of the doc is "
     + doc.getDocumentElement().getNodeName());
   NodeList listOfPersons = doc.getElementsByTagName("PERSON");
   int totalPersons = listOfPersons.getLength();
   System.out.println("Total NO of people : " + totalPersons);
   for (int s = 0; s < listOfPersons.getLength(); s++) {
    Node firstPersonNode = listOfPersons.item(s);
    if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
     Element firstPersonElement = (Element) firstPersonNode;

     // -------
     NodeList firstNameList = firstPersonElement
       .getElementsByTagName("DEPTNO");
     Element firstNameElement = (Element) firstNameList.item(0);
     NodeList textFNList = firstNameElement.getChildNodes();
     System.out
       .println("DEPTNO Name : "
         + ((Node) textFNList.item(0))
           .getNodeValue().trim());

     // -------
     NodeList lastNameList = firstPersonElement
       .getElementsByTagName("DNAME");
     Element lastNameElement = (Element) lastNameList.item(0);
     NodeList textLNList = lastNameElement.getChildNodes();
     System.out
       .println("DNAME Name : "
         + ((Node) textLNList.item(0))
           .getNodeValue().trim());

     // ----
     NodeList ageList = firstPersonElement
       .getElementsByTagName("LOC");
     Element ageElement = (Element) ageList.item(0);
     NodeList textAgeList = ageElement.getChildNodes();
     System.out.println("LOC : "
       + ((Node) textAgeList.item(0)).getNodeValue()
         .trim());
    }
   }
  } catch (SAXParseException err) {
   System.out.println("** Parsing error" + ", line "
     + err.getLineNumber() + ", uri " + err.getSystemId());
   System.out.println(" " + err.getMessage());

  } catch (SAXException e) {
   Exception x = e.getException();
   ((x == null) ? e : x).printStackTrace();

  } catch (Throwable t) {
   t.printStackTrace();
  }
}
}

读取结果:

  result

DOM-读取数据库,将数据写入XML,解析XML..


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论