Use JDOM to operate XML series-read record sets from databases to stacked XML files

Source: Internet
Author: User
Tags rowcount

Note the environment of this series of Files: Oracle Data and jdom1.0
The following tables and data are used in a total of four files.
The Oracle table structure is as follows:
/* The most critical fields in this table are CID and PID. Other fields can be increased or decreased as needed */
Create Table Scott. Company
(
CID number (4) not null,/* Record ID */
Cname varchar2 (20) not null,/* name */
Descpt varchar2 (40) null,/* Description */
PID Number (4) null/* parent ID */
);
/* Insert data into the table */
Insert
Into company (Company. CID, company. cname, company. descpt, company. PID)
Values (1, 'southamat', 'changsha city, Hunan Province ', 0 );
Insert
Into company (Company. CID, company. cname, company. descpt, company. PID)
Values (2, 'System integration', 'System integration', 1 );
Insert
Into company (Company. CID, company. cname, company. descpt, company. PID)
Values (3, 'Software developing', 'Software developing', 1 );
Insert
Into company (Company. CID, company. cname, company. descpt, company. PID)
Values (6, 'netoa developers ', 'net Project', 3 );
Insert
Into company (Company. CID, company. cname, company. descpt, company. PID)
Values (7, 'wang Jun ', 'wang Jun of J2EE group', 5 );
Insert
Into company (Company. CID, company. cname, company. descpt, company. PID)
Values (8, 'xiang Hong ', 'xiang Hong of J2EE group', 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 system integration', 2 );
Insert
Into company (Company. CID, company. cname, company. descpt, company. PID)
Values (11, 'wang fe', 'linux group', 10 );
Insert
Into company (Company. CID, company. cname, company. descpt, company. PID)
Values (12, 'zhang wan', 'netoa group', 6 );
Insert
Into company (Company. CID, company. cname, company. descpt, company. PID)
Values (13, 'Li bin', 'j2ee Li bin', 5 );
Insert
Into company (Company. CID, company. cname, company. descpt, company. PID)
Values (14, 'wucheng', 'linux group', 10 );
Insert
Into company (Company. CID, company. cname, company. descpt, company. PID)
Values (4, 'personnel authorization', 'Company personnel management', 1 );
Insert
Into company (Company. CID, company. cname, company. descpt, company. PID)
Values (5, 'j2ee Project', 'j2ee Project', 3 );
Insert
Into company (Company. CID, company. cname, company. descpt, company. PID)
Values (15, 'wang yi', 'j2ee Wang yi', 5 );

Package Jing. xml;

/**
* <P> title: reads the record set from the database to the stacked XML file </P>
* <P> Description: </P>
* <P> copyright: Copyright (c) 2004 </P>
* <P> company: </P>
* @ Author ouchao Jing 13873195792
* @ Version 1.0
*/

// Output the 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 = 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) 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.exe cutequery ();
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.doc ument = new document (Root); // create the document root element
Preparedstatement pstmt = dbxml. Conn. preparestatement (
"Select * from company order by CID ",
Resultset. type_scroll_sensitive, resultset. concur_updatable );
Resultset rs = pstmt.exe cutequery ();

Resultsetmetadata RMD = Rs. getmetadata ();
Int colcount = RMD. getcolumncount ();
Element elementcol = new element ("coltype ");
For (INT I = 1; I <= colcount; I ++) {// column attribute
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 (); // output in Chinese format, generating indentation and line feed
Format = outp. getformat ();
Format. setencoding ("gb2312"); // sets the language.
Format. setexpandemptyelements (true); // you can specify <sample> </sample> as an empty output element.
Outp. setformat (format );

Outp.output(dbxml.doc ument, new fileoutputstream ("companytree. xml"); // output XML document

System. Out. Print ("the XML document has been generated! ");
}
}
 

Using JDOM to operate XML series Article 2 reading record sets from databases to flat XML files
Package Jing. xml;

/**
* <P> title: reads the record set from the database to a flat XML file. </P>
* <P> Description: </P>
* <P> copyright: Copyright (c) 2004 </P>
* <P> company: </P>
* @ Author ouchao Jing 13873195792
* @ Version 1.0
*/

// Output the 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.exe cutequery ();
Document document = new document (new element ("root"); // create a document root element
Resultsetmetadata RMD = Rs. getmetadata ();
Int colcount = RMD. getcolumncount ();
Element elemnetcol = new element ("coltype ");
For (INT I = 1; I <= colcount; I ++) {// column attribute
Elemnetcol. setattribute (RMD. getcolumnname (I ),
RMD. getcolumntypename (I ));
}
Document. getrootelement (). addcontent (elemnetcol );

While (Rs. Next () {// The XML record is generated for the uncertain table.
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 (); // output in Chinese format, generating indentation and line feed

Format = outp. getformat ();
Format. setencoding ("gb2312"); // sets the language.
Format. setexpandemptyelements (true); // you can specify <sample> </sample> as an empty output element.
Outp. setformat (format );

Outp. Output (document, new fileoutputstream ("company. xml"); // output XML document
System. Out. Print ("the XML document has been generated! ");
}
}

Use JDOM to operate XML series articles triplicate XML files
Package Jing. xml;
/**
* <P> title: plane XML file to Hierarchical XML file </P>
* <P> Description: </P>
* <P> copyright: Copyright (c) 2004 </P>
* <P> company: </P>
* @ Author ouchao Jing 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 Constructor
Doc = sb. Build (New fileinputstream ("company. xml"); // read the file
Root = Doc. getrootelement (); // obtain the root element

}

Public void listelemnet (string PID, 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); // create the document root element

BB. listelemnet ("0", roote );

Xmloutputter outp = new xmloutputter (format. getprettyformat (); // output in Chinese format, generating indentation and line feed
Format = outp. getformat ();
Format. setencoding ("gb2312"); // sets the language.
Format. setexpandemptyelements (true); // you can specify <sample> </sample> as an empty output element.
Outp. setformat (format );
Outp. Output (tdocument, new fileoutputstream ("companytree. xml"); // output the XML document
// Outp. Output (tdocument, system. Out );
System. Out. Print ("the XML document has been generated! ");
}
}

Use JDOM to operate XML series Article 4 query using JDOM and xpath

Package Jing. xml;
/**
* <P> title: query using JDOM and XPath </P>
* <P> Description: </P>
* <P> copyright: Copyright (c) 2004 </P>
* <P> company: </P>
* @ Author ouchao Jing 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 Constructor
Document Doc = sb. Build (New fileinputstream ("company. xml"); // read the file
Element root = Doc. getrootelement (); // obtain the root element
List ROW = root. getchildren (); // get the node list
// Locate by CID and directly locate the set returned by the Row Element.
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-condition 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 (); // output in Chinese format, generating indentation and line feed

// Reformat
Format = outp. getformat ();
Format. setencoding ("gb2312 ");
Format. setexpandemptyelements (true );
Outp. setformat (format );

// Outp. Output (Doc, new fileoutputstream ("jdomcompany. xml"); // output the XML document
Outp. Output (Doc, system. Out );
System. Out. println ("the XML file for JDOM operations is complete! ");
}
}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.