Package jdom; / *
News Dedicated XML File Function: Delete Added by Up to Under The Lower Add Read By Under Direction, you can limit the recording by deleting X strips from top to bottom, how much is recorded, more time to delete old Inserting new data full data exchange using arraylist to handle discomfort: scalability is not strong for specific XML files, no unlimited levels of access child nodes, performance has not been optimized!
* /
Import java.ior. *; import java.lang. *; import org.jdom. *; import org.jdom.output. *; import org.jdom.input. *; import javax.servlet .http.httpservletRequest; import javax.servlet.jsp.pageContext; import com.ntsky.file.fileutil; // A class uploaded class ~ I call to generate a directory, please contact me if you need it ..
Public class writeXml {// Initializing object variable private string root_jsp = new string (); // directory root path private string filename = new string (); // XML file name private string secon_root = new string (); // creation directory Private arraylist postlist = null; // store data record private map map = new hashmap (); private int maxline = 0; // xml node's maximum number of private int Docus = 0; // Store XML number private document read_doc = New document (); private document public_doc = new document (); private string root_temp_file = ""; private string first_dir = "
Public void buildxml () throws exception // New an XML file
{Element root, news_rom, id, title, file_route, postday; root = new element ("news_info"); // Generate root element: student-info read_doc = new document (root); this.docline = 0; build );
} PUBLIC VOID ADDXML () THROWS Exception / / Add a node to XML
{Element root; // Generate root elements: info root = read_doc.getrootElement (); // Get root element // put root elements into the document DOC build (root);
}
Public void build (element root) THROWS Exception // Write data to XML file {Element news_rom, id, title, file_route, postday, sate; if (docline> maxline) {if (! DELXML (DOCLINE - MAXLINE)) {/ / Clean out space return; // If clean up the error exit program} else {this.docline = maxline;}} int a = postlist.size () / 5; // To store the number INT b = maxline - docline; // Air position if (a> = maxline) // If you need to add more records than maximum numbers a = maxline; // stored to maximum number IF (a <0) a = postlist.size () / 5; if A - b> = 0) {// The required space required to store records
IF (! DELXML (A-B)) // Clean out space return; // If clean up error exit program}
// for (int i = 0; i <(a * 5); i = i 5) for (int i = (A * 5) -1; I> = 0; i = i - 5) {news_rom = NEW ELEMENT ("news_rom"); // Generate Elements: This element will contain elements Number, Name, Age ID = New Element ("ID"); title = new element ("title"); file_route = new element (" File_route "); postday = new element; SATE = New Element (" SATE "); id.settext ((string) PostList.get (I-4)); title.settext (String) PostList. GET (I-3)); file_route.settext (String) PostList.get (I-2)); postday.settext (String) PostList.get (I-1)); Sate.Settext (String) PostList .get (i));
News_rom.addcontent (ID); news_rom.addcontent (title); news_rom.addcontent (file_route); news_rom.addcontent (postDay); news_rom.addcontent (SATE); root.addcontent (news_rom);}
THIS.PUBLIC_DOC = Read_Doc;
Public int GET_DOCLINE () {
Return this.docline;
}
Public int GET_MAXLINE () {
Return this.maxline;
}
Public void readxml_defail (String province) THROWS Exception // Node Record of the recorded ID
{Element myelement = read_doc.getrootElement (); // Get root node list Listtwo = myelement.getChildren (); // Get list, ready to traverse all nodes map a = new hashMap (); for (int i = 0; i < Listtwo.size (); i ) {ELEMENT E_KEY = (Element) Listtwo.get (i); // Get element IF under node (province.equals ("province"). getTextTrim ())) / / Compare the ID value is equal {ELEMENT E_KEY_CHILD = E_KEY.GETCHILD ("URL_XML"); for (int J = 1; j <= 9; j ) A.PUT ("new _" j, e_key_child.getchildtext ("url_xml_" J)); Break;}} this.map = a;} public arraylist readxml_one (string id) THROWS exception // Node recorded node record {ELEMENT MyElement = read_doc.getrootelement (); // Get root node List ListTWO = MyElement.getChildren (); // Get list, ready to traverse all nodes arraylist a = new arraylist (); for (int i = 0; i {Element myelement = read_doc.getrootElement (); // Get root nodes list Listttwo = myelement.getchildren (); // Get list, ready to traverse all nodes arraylist a = new arraylist (); int temp = 0; if (Listtwo .Size ()> = Number) Temp = Listtwo.size () - Number; else temp = 0; for (int i = Listtwo.size () - 1; i> = 0 && I> = Temp; I -) {ELEMENT E_KEY = (ELEMENT) Listtwo.get (i); // Get element a.add (e_key.getchild ("ID") under the node. GetTextTrim ()); a.add (e_key.getchild ("Title" ). Gettexttrim ()); A.Add (e_key.getchild ("file_route"). getTextTrim ()); A.Add (e_key.getchild ("postday"). getTextTrim ()); a.add (e_key.getchild ("SATE"). GetTextTrim ()); } RETURN A;} public arraylist readxml_all_id (int Number) THROWS Exception // All records read {Element myelement = read_doc.getrootElement (); // Get root nodes list Listttwo = myelement.getchildren (); // Get list, ready to traverse all nodes arraylist a = new arraylist (); int temp = 0; if (Listtwo .Size ()> = Number) Temp = Listtwo.size () - Number; else temp = 0; for (int i = Listtwo.size () - 1; i> = 0 && I> = Temp; I -) {ELEMENT E_KEY = (Element) Listtwo.get (i); // Get the element under the node a.add (e_key.getchild ("id"). GetTextTrim ()); } RETURN A; Public void editXML (String id) throws exception // modified record is the node of the ID {ELEMENT myelement = read_doc.getrootElement (); // Get root nodes list Listtwo = myelement.getchildren (); // Get list, ready to traverse all nodes for (int i = 0; i MyElement.getChildren (); // Get list, ready to traverse all node for (int i = 0; i Public void WreitXML_File () throws exception // Generate files to hard drive { Format Format = Format.getCompactFormat (); Format.setencoding ("GB2312"); // Set the character of the XML file to GB2312 Format.SetinDent ("); // Sets the indentation of the XML file to 4 space XMloutputter XMlout = new xmloutputter (format); // After the elements, the elements are replaced by each layer. XMlout.Output (public_doc, new fileoutputstream (get_fullrootfile ())); } // Take data from the hard disk to memory public boolean readXML () throws Exception {try {SAXBuilder builder = new SAXBuilder (); this.read_doc = builder.build (Get_FullRootFile ()); Element MyElement = read_doc.getRootElement (); // get the root List listTwo = MyElement. GetChildren (); this.docline = Listtwo.size (); listtwo = null; return true;} catch (exception e) {E.PrintStackTrace (); return false;}} Public void set_postlist (arraylist postlist) {// XML Add recorded data source THIS.POSTLIST = PostList; } Public void initialize (String URL) {// Take the root directory path of the servlet THIS.ROOT_JSP = URL;} Public void Initialize (PageContext PageContext) {// take the JSP root directory path THIS.ROOT_JSP = PageContext (). getRealPath ("/");} public void set_maxlint (int MAX) {// Maximum license memory this.maxline = max;} Public void setXMLFileName (String filename) {// Setting the XML file name string temp = new string (); if (filename.tolowercase (). Endswith (". Xml")) {this.filename = filename;} else {temp = filename ".xml"; This.FileName = Temp;}} public void firstdir (String Rot) {// Set the first directory IF (Rot.endSwith ("/")) this.first_dir = rot; else this.first_dir = rot "/";} Public void creatdir () {try { FileUtil C = new fileutil (); string fem_d = get_secon_root (); C. MakeDir (Fild_D, root_jsp); } catch (exception e) { } Public void setsecon_root (String Secon_Root) {// Setting the directory IF (Secon_Root.endSwith ("/")) this.secon_root = first_dir second_root; else this.secon_root = first_dir second_root "/"; } Public void setfull_r_name (string roco) { // Enter data by the superior program this.Root_Temp_File = ROO; } // Return to data Public String get_initialize () {// return root path Return this.root_jsp; } Public string get_setXMLFileName () {// Return to XML file name Return this.filename; } Public string get_secon_root () {// Return to the creation Return this.secon_root; } Public String get_fullrootfile () {// Return detailed file name path if (this.root_temp_file.equals (")) Return get_initialize () get_secon_root () get_setXMLFileName (); else return get_initialize () this.Root_Temp_File; } Public string get_dir_file () { / / Return detailed file name path if (this.root_temp_file.equals (")) Return GET_SECON_ROOT () GET_SETXMLFILENAME (); else return this.root_temp_file; } Public map getInputMap () {return this.map;} public string getString (String key) {// TODO automatic generation method stub Return (String) map.get (key);}} is used to manage two XML files first (This XML stores the second XML path): XML Version = "1.0" encoding = "gb2312"?>