Note This series of file usage environments: Oracle data and JDOM1.0 versions
The following tables and data are used in a total of four files.
The Oracle table structure is as follows:
/* The most critical of this table is the CID and PID two fields, the other need to be able to add or subtract fields */
CREATE TABLE Scott.company
(
CID Number (4) not NULL,/* Record ID */
CNAME VARCHAR2 () not NULL,/* Name */
DESCPT VARCHAR2 (+) NULL,/* Description */
PID Number (4) NULL/* Parent ID No. */
);
/* Insert data into the table */
INSERT
Into company (Company.cid, Company.cname, COMPANY.DESCPT, Company.pid)
VALUES (1,´ in South ´,´, Changsha ´, Hunan Province, 0);
INSERT
Into company (Company.cid, Company.cname, COMPANY.DESCPT, Company.pid)
VALUES (2,´ system integration ´,´ Various system integration ´, 1);
INSERT
Into company (Company.cid, Company.cname, COMPANY.DESCPT, Company.pid)
VALUES (3,´ Software development ´,´ Software development ´, 1);
INSERT
Into company (Company.cid, Company.cname, COMPANY.DESCPT, Company.pid)
VALUES (6,´netoa Development Group ´,´net Project Development ´, 3);
INSERT
Into company (Company.cid, Company.cname, COMPANY.DESCPT, Company.pid)
VALUES (7,´ Wang June ´,´j2ee group Wang June ´, 5);
INSERT
Into company (Company.cid, Company.cname, COMPANY.DESCPT, Company.pid)
VALUES (8,´ ´,´j2ee Group Xiang Red ´, 5);
INSERT
Into company (Company.cid, Company.cname, COMPANY.DESCPT, Company.pid)
VALUES (9,´windows Integration Group ´,´windows system Integration ´, 2);
INSERT
Into company (Company.cid, Company.cname, COMPANY.DESCPT, Company.pid)
VALUES (10,´linux Integration Group ´,´linux related systems Integration ´, 2);
INSERT
Into company (Company.cid, Company.cname, COMPANY.DESCPT, Company.pid)
VALUES (11,´ Wang Fei ´,´linux group ´, 10);
INSERT
Into company (Company.cid, Company.cname, COMPANY.DESCPT, Company.pid)
VALUES (12,´ Universal ´,´netoa Group ´, 6);
INSERT
Into company (Company.cid, Company.cname, COMPANY.DESCPT, Company.pid)
VALUES (13,´ Li Bing ´,´j2ee li Bing ´, 5);
INSERT
Into company (Company.cid, Company.cname, COMPANY.DESCPT, Company.pid)
VALUES (14,´ his ´,´linux group ´, 10);
INSERT
Into company (Company.cid, Company.cname, COMPANY.DESCPT, Company.pid)
VALUES (Personnel Management Department of 4,´ personnel ´,´ company ´, 1);
INSERT
Into company (Company.cid, Company.cname, COMPANY.DESCPT, Company.pid)
VALUES (5,´J2EE project Team ´,´J2EE Project Development ´, 3);
INSERT
Into company (Company.cid, Company.cname, COMPANY.DESCPT, Company.pid)
VALUES (Wang ´, 15,´ Wang ´,´j2ee Group, 5);
Package jing.xml;
/**
* <p>title: Read Recordset from database to cascading XML file </p>
* <p>description: </p>
* <p>copyright:copyright (c) 2004</p>
* <p>company: </p>
* @author Ouchi 13873195792
* @version 1.0
*/
To output a database table as an XML document
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);//create Doc root element
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++) {//Column properties
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 China output, resulting in indentation and line breaks
Format format = Outp.getformat ();
Format.setencoding ("GB2312"); Set Language
Format.setexpandemptyelements (TRUE); Set the output empty element to <sample></sample> format
Outp.setformat (format);
Outp.output (Dbxml.document, New FileOutputStream ("Companytree.xml")); Output XML document
System.out.print ("XML document Generation completed. ");
}
}
Using Jdom to manipulate XML series article two reading a recordset from a database to a flat XML file
Package jing.xml;
/**
* <p>title: Read Recordset from database to flat XML file </p>
* <p>description: </p>
* <p>copyright:copyright (c) 2004</p>
* <p>company: </p>
* @author Ouchi 13873195792
* @version 1.0
*/
To output a database table as an XML document
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")); Creating the document root element
ResultSetMetaData RMD = Rs.getmetadata ();
int colcount = Rmd.getcolumncount ();
Element elemnetcol = new Element ("Coltype");
for (int i = 1; I <= colcount; i++) {//Column properties
Elemnetcol.setattribute (Rmd.getcolumnname (i),
Rmd.getcolumntypename (i));
}
Document.getrootelement (). Addcontent (Elemnetcol);
while (Rs.next ()) {//Indeterminate table generates an XML record
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 China output, resulting in indentation and line breaks
Format format = Outp.getformat ();
Format.setencoding ("GB2312"); Set Language
Format.setexpandemptyelements (TRUE); Set the output empty element to <sample></sample> format
Outp.setformat (format);
Outp.output (document, New FileOutputStream ("Company.xml")); Output XML document
System.out.print ("XML document Generation completed. ");
}
}
Using the Jdom Action XML series article three-dimensional XML file to cascade XML file
Package jing.xml;
/**
* <p>title: Flat XML file to cascade XML file </p>
* <p>description: </p>
* <p>copyright:copyright (c) 2004</p>
* <p>company: </p>
* @author Ouchi 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 (); New Build constructor
doc = Sb.build (new FileInputStream ("Company.xml")); Read in file
root = Doc.getrootelement (); Get the root element elements
}
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); Creating the document root element
Bb. Listelemnet ("0", roote);
Xmloutputter OUTP = new Xmloutputter (Format.getprettyformat ()); Format China output, resulting in indentation and line breaks
Format format = Outp.getformat ();
Format.setencoding ("GB2312"); Set Language
Format.setexpandemptyelements (TRUE); Set the output empty element to <sample></sample> format
Outp.setformat (format);
Outp.output (Tdocument, New FileOutputStream ("Companytree.xml")); Output XML document
Outp.output (tdocument,system.out);
System.out.print ("XML document Generation completed. ");
}
}
Using Jdom to manipulate XML series article four using Jdom and XPath in conjunction with queries
Package jing.xml;
/**
* <p>title: Query with Jdom and XPath </p>
* <p>description: </p>
* <p>copyright:copyright (c) 2004</p>
* <p>company: </p>
* @author Ouchi 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 (); New Build constructor
Document doc = sb.build (new FileInputStream ("Company.xml")); Read in file
Element root = Doc.getrootelement (); Get the root element elements
List row = Root.getchildren (); Get node List
Locate directly to the row element by CID lookup returns a collection
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"));
}
Multi-Criteria Query
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 China output, resulting in indentation and line breaks
Reformat
Format format = Outp.getformat ();
Format.setencoding ("GB2312");
Format.setexpandemptyelements (TRUE);
Outp.setformat (format);
Outp.output (Doc, New FileOutputStream ("Jdomcompany.xml")); Output XML document
Outp.output (Doc, System.out);
System.out.println ("Jdom operation XML document finished. ");
}
}