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();
}
}
}
读取结果: