使用JDOM操作XML系列----从数据库中读取记录集到层叠式XML文

原创 2005年05月30日 02:09:00

注意本系列文件使用环境:ORACLE数据和JDOM1.0版本
总计四篇文件中都使用到以下表和数据。
Oracle表结构如下:
/*此表中最关键的就是CID和PID两个字段,其它的跟据需要可以增减字段*/
CREATE TABLE SCOTT.COMPANY
(
CID NUMBER(4) NOT NULL, /*记录ID号*/
CNAME VARCHAR2(20) NOT NULL, /*名字*/
DESCPT VARCHAR2(40) NULL, /*描述*/
PID NUMBER(4) NULL /*父ID号*/
);
/*向表中插入数据*/
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 1, ´中南迈特´, ´湖南省长沙市´, 0 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 2, ´系统集成´, ´各种系统集成´, 1 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 3, ´软件开发´, ´软件开发´, 1 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 6, ´NetOA开发组´, ´Net项目开发´, 3 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 7, ´王军´, ´J2EE组王军´, 5 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 8, ´湘红´, ´J2EE组湘红´, 5 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 9, ´Windows集成组´, ´Windows系统集成´, 2 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 10, ´Linux集成组´, ´Linux相关系统集成´, 2 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 11, ´王非´, ´Linux组´, 10 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 12, ´张万´, ´NetOA组´, 6 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 13, ´李兵´, ´J2EE李兵´, 5 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 14, ´武成´, ´Linux组´, 10 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 4, ´人事部´, ´公司人事管理部门´, 1 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 5, ´J2EE项目组´, ´J2EE项目开发´, 3 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 15, ´王义´, ´J2EE组王义´, 5 );


package jing.xml;

/**
* <p>Title: 从数据库中读取记录集到层叠XML文件</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author 欧朝敬 13873195792
* @version 1.0
*/

//将数据库表输出为XML文档
import org.jdom.*;
import org.jdom.output.*;
import java.sql.*;
import java.io.*;

public class dbtoxmltree {
public String url = null;
public Connection conn = null;
public Document document = null;
public dbtoxmltree() throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
url = "jdbc:oracle:thin:@192.168.128.250:1521:sample";
conn = DriverManager.getConnection(url, "scott", "tiger");
}

public void digui(int pid,Element element) throws Exception {
String sql = "select * from company where pid=" + pid;
PreparedStatement pstmt = conn.prepareStatement(
sql,
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = pstmt.executeQuery();
ResultSetMetaData rmd = rs.getMetaData();
int colcount = rmd.getColumnCount();
while (rs.next()) {
Element element0 = new Element("DSTree");
for(int i=1;i<=colcount;i++){
element0.setAttribute(rmd.getColumnName(i),
(rs.getString(i) == null ? "" :
rs.getString(i)));
}
element0.setAttribute("open","false");
element.addContent(element0);
digui(rs.getInt("CID"),element0);
}
rs.close();
pstmt.close();

}

public static void main(String[] args) throws Exception {
dbtoxmltree dbxml = new dbtoxmltree();
Element root=new Element("DSTreeRoot");
dbxml.document=new Document(root);//创建文档ROOT元素
PreparedStatement pstmt = dbxml.conn.prepareStatement(
"select * from company order by cid",
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = pstmt.executeQuery();

ResultSetMetaData rmd = rs.getMetaData();
int colcount = rmd.getColumnCount();
Element elementcol = new Element("COLTYPE");
for (int i = 1; i <= colcount; i++) { //列属性
elementcol.setAttribute(rmd.getColumnName(i),
rmd.getColumnTypeName(i));
}
root.addContent(elementcol);
rs.close();
pstmt.close();

dbxml.digui(0,root);

dbxml.conn.close();
XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat()); //格式华输出,产生缩进和换行
Format format = outp.getFormat();
format.setEncoding("GB2312"); //设置语言
format.setExpandEmptyElements(true); //设置输出空元素为<sample></sample>格式
outp.setFormat(format);

outp.output(dbxml.document, new FileOutputStream("companytree.xml")); //输出XML文档

System.out.print("XML 文档生成完毕!");
}
}
 

使用JDOM操作XML系列文章二 从数据库中读取记录集到平面式XML文件
package jing.xml;

/**
* <p>Title: 从数据库中读取记录集到平面式XML文件</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author 欧朝敬 13873195792
* @version 1.0
*/

//将数据库表输出为XML文档
import org.jdom.*;
import org.jdom.output.*;
import java.sql.*;
import java.io.*;

public class databasetoxml {
public databasetoxml() {
}

public static void main(String[] args) throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url = "jdbc:oracle:thin:@192.168.128.250:1521:sample";
Connection conn = DriverManager.getConnection(url, "scott", "tiger");
PreparedStatement pstmt = conn.prepareStatement(
"select * from company order by cid",
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = pstmt.executeQuery();
Document document = new Document(new Element("ROOT")); //创建文档ROOT元素
ResultSetMetaData rmd = rs.getMetaData();
int colcount = rmd.getColumnCount();
Element elemnetcol = new Element("COLTYPE");
for (int i = 1; i <= colcount; i++) { //列属性
elemnetcol.setAttribute(rmd.getColumnName(i),
rmd.getColumnTypeName(i));
}
document.getRootElement().addContent(elemnetcol);

while (rs.next()) { //不确定表生成XML记录
Element element0 = new Element("ROW");
for (int i = 1; i <= colcount; i++) {
element0.setAttribute(rmd.getColumnName(i),(rs.getString(i)==null?"":rs.getString(i)));
}
document.getRootElement().addContent(element0);
}
rs.close();
pstmt.close();
conn.close();
XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat()); //格式华输出,产生缩进和换行

Format format = outp.getFormat();
format.setEncoding("GB2312"); //设置语言
format.setExpandEmptyElements(true); //设置输出空元素为<sample></sample>格式
outp.setFormat(format);

outp.output(document, new FileOutputStream("company.xml")); //输出XML文档
System.out.print("XML 文档生成完毕!");
}
}  

使用JDOM操作XML系列文章三 平面式XML文件转层叠式XML文件
package jing.xml;
/**
* <p>Title: 平面式XML文件转层叠式XML文件</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author 欧朝敬 13873195792
* @version 1.0
*/
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import org.jdom.xpath.*;
import java.io.*;
import java.util.*;

public class xmltotree {
public SAXBuilder sb = null;
public Document doc = null;
public Element root = null;
public xmltotree() throws Exception {
sb = new SAXBuilder(); // 新建立构造器
doc = sb.build(new FileInputStream("company.xml")); // 读入文件
root = doc.getRootElement(); // 获得根元素element

}

public void Listelemnet(String pid, Element element) throws Exception {
List find = XPath.selectNodes(root, "/ROOT/ROW[@PID=´" + pid + "´]");
int rowcount = find.size();
for (int i = 0; i < rowcount; i++) {
Element findelement = (Element) find.get(i);
Element element0 = new Element("ROW");
List attrib = findelement.getAttributes();
int j = attrib.size();
for (int h = 0; h < j; h++) {
Attribute attribute = (Attribute) attrib.get(h);
element0.setAttribute(
attribute.getName(),
attribute.getValue());

}
element.addContent(element0);
Listelemnet(findelement.getAttributeValue("CID"), element0);
}
}

public static void main(String[] args) throws Exception {
xmltotree bb = new xmltotree();

Element roote = new Element("ROOT");
Document tdocument = new Document(roote); //创建文档ROOT元素

bb.Listelemnet("0", roote);

XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat()); //格式华输出,产生缩进和换行
Format format = outp.getFormat();
format.setEncoding("GB2312"); //设置语言
format.setExpandEmptyElements(true); //设置输出空元素为<sample></sample>格式
outp.setFormat(format);
outp.output(tdocument, new FileOutputStream("companytree.xml")); //输出XML文档
//outp.output(tdocument,System.out);
System.out.print("XML 文档生成完毕!");
}
}  

使用JDOM操作XML系列文章四 使用JDOM和XPATH结合查询

package jing.xml;
/**
* <p>Title: 使用JDOM和XPATH结合查询</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author 欧朝敬 13873195792
* @version 1.0
*/
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import org.jdom.xpath.*;

import java.io.*;
import java.util.*;

public class TreeXML {
public TreeXML() {
}

public static void main(String[] args) throws Exception {
SAXBuilder sb = new SAXBuilder(); // 新建立构造器
Document doc = sb.build(new FileInputStream("company.xml")); // 读入文件
Element root = doc.getRootElement(); // 获得根元素element
List row = root.getChildren(); //取得节点列表
//按CID查找直接定位到ROW元素返回的是集合
List find = XPath.selectNodes(root, "/ROOT/ROW[@PID=´1´]");
for (int i = 0; i < find.size(); i++) {
Element findelement = (Element) find.get(i);
System.out.println(findelement.getAttributeValue("CNAME"));
}

//多条件查询
Element findelement=(Element)XPath.selectSingleNode(root,"/ROOT/ROW[@PID=´3´][@CID=´10´]");
System.out.println(findelement.getAttributeValue("CNAME"));

XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat()); //格式华输出,产生缩进和换行

//重新格式化
Format format = outp.getFormat();
format.setEncoding("GB2312");
format.setExpandEmptyElements(true);
outp.setFormat(format);

//outp.output(doc, new FileOutputStream("jdomcompany.xml")); //输出XML文档
outp.output(doc, System.out);
System.out.println("JDOM操作XML文档完毕!");
}
}
 

利用XSLT把ADO记录集转换成XML

  • 2008年10月22日 16:22
  • 12KB
  • 下载

JAVA100例之实例71 使用JDOM操作XML

  • 2008年12月05日 10:19
  • 767KB
  • 下载

XML入门之二:Jdom2的写入和读取操作

注: 我用的jdom版本是jdom-2.0.3,所以包名称对应的都是jdom2.* 如果用的是1.x 包名称对应的是jdom.* package com.rt.jdom; impo...

使用jdom简单操作XML

来自:http://splend.blog.51cto.com/3717743/984811 package examples.ftp; /** * * 引入jar包: * 1、c...

使用Jdom查询数据库后,把数据写入xml文件中

1.WriteXml.java: package cn.gov.csrc.xml; import java.io.File; import java.io.FileWriter; import ja...

jdom 或 dom4j读取xml文件时如何让dtd验证使用本地dtd文件或者不生效

一、写在所有之前: 因为dom4j和jdom在这个问题上处理的方法是一模一样的,只是一个是SAXBuilder 一个SAXReader,这里以jdom距离,至于dom4j只需要同理替换一下就可以了。...

使用JDOM读取XML文件,并输出其中的数据

废话就不多说了,直接写代码了。 用到的XML文件:   1-2 ABC DEF GHJ   JAVA代码如下: package com.X...

使用JDOM读取XML信息

package JdomTest;/** * @author Administrator * @version 2017-1-14 * @description JDOM 对xml文件进行解析 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:使用JDOM操作XML系列----从数据库中读取记录集到层叠式XML文
举报原因:
原因补充:

(最多只允许输入30个字)