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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值