Establish, modify the XML document with DOM4J, and resolve formatting outputs and Chinese issues
---------------------
Package com.holen.dom4j;
Import java.io.file;
Import java.io.filewriter;
Import java.util.iterator;
Import java.util.list;
Import org.dom4j.attribute;
Import org.dom4j.document;
Import org.dom4j.documenthelper;
Import org.dom4j.element;
Import Org.dom4j.io.outputFormat;
Import org.dom4j.io.saxreader;
Import org.dom4j.io.xmlwriter;
/ **
* @Author holin chen
* /
Public class dom4jdemo {
Public Dom4jdemo () {
}
/ **
* Establish an XML document, the document name is determined by the input parameter
* @Param FileName needs to be established file name
* @return returns the result of the operation, 0 tables fail, 1 table is successful
* /
Public int createXmlfile (String filename) {
/ ** Return to the results, 0 tables fail, 1 table is successful * /
Int returnvalue = 0;
/ ** Establish Document object * /
Document Document = documenthelper.createdocument ();
/ ** Establish the root of the XML document BOOKS * /
ELEMENT BOOKSELEMENT = Document.AddeElement ("Books");
/ ** Add a line of comments * /
BookSelement.addcomment ("this is a test for Dom4j, Holen, 2004.9.11");
/ ** Join the first BOOK node * /
Element Bookelement = Bookselement.addelement ("book"); "BOOK");
/ ** Add to show parameter content * /
Bookelement.Addattribute ("Show", "Yes");
/ ** Add Title Node * /
Element TitleElement = Bookelement.addeElement ("Title");
/ ** Set content for Title * /
TitleElement.Settext ("Dom4j Tutorials");
/ ** Similar to the two BOOK * /
Bookelement = bookselement.addelement ("book"); Bookelement.addattribute ("Show", "YES");
TitleElement = Bookelement.addeElement ("Title");
TitleElement.Settext ("Lucene Studing");
Bookelement = BookSelement.addelement ("book");
Bookelement.addattribute ("Show", "NO");
TitleElement = Bookelement.addeElement ("Title");
TitleElement.Settext ("Lucene In Action");
/ ** Add to Owner Node * /
Element OwneRelement = BookSelement.addeElent ("Owner");
OwneRelement.Settext ("O'Reilly");
Try {
/ ** Write the contents in Document * /
XMLWRITER Writer = New FILEWRITER (New File (FileName)))
Writer.write (Document);
Writer.close ();
/ ** Execute success, need to return 1 * /
ReturnValue = 1;
} catch (exception ex) {
EX.PrintStackTrace ();
}
Return ReturnValue;
}
/ **
* Modify the content in the XML file and save as a new file
* Keep how to add nodes in DOM4J, modify nodes, delete nodes
* @Param FileName Modify Object File
* @Param newFileName After modifying saving as this file
* @return returns the result of the operation, 0 tables fail, 1 table is successful
* /
Public int modixmlfile (string newfilename) {
Int returnvalue = 0;
Try {
SAXReader saxreader = new saxreader ();
Document Document = SAXReader.read (New File (FileName));
/ ** One of the modifications: If the content of the Show parameter in the Book node is YES, modify it into no * /
/ ** Find objects with XPath * /
List list = document.selectnodes ("/ books / book / @ Show");
Iterator it = list.iterator ();
While (ore.hasnext ()) {
Attribute attribute = (attribute) iter.next ();
IF (attribute.getvalue (). Equals ("yes")) {
Attribute.SetValue ("no");
}
}
/ **
* Two two modifications: change the Owner item content to tSHINGHUA
* Add the Date node in the Owner node, the Date node is 2004-09-11, add a parameter Type for the Date node
* /
List = Document.selectNodes ("/ books / Owner");
iter = list.iterator ();
IF (iter.hasnext ()) {ELEMENT OWNERELEMENT = (Element) iter.next ();
OwneRelement.setText ("TSHINGHUA");
Element DateElement = OwneRelement.addeElement ("Date");
DateElement.Settext ("2004-09-11");
DateElement.addattribute ("Type", "Gregorian Calendar";
}
/ ** Three modifications: If the title content is DOM4J Tutorials, delete the node * /
List = document.selectnodes ("/ books / book");
iter = list.iterator ();
While (ore.hasnext ()) {
ELEMENT BOOKELEMENT = (Element) iTer.next ();
Iterator itrator = Bookelement.elementIterator ("Title");
While (item.hasnext ()) {
Element TitleElement = (Element) Iterator.next ();
IF (TitleElement.getText (). Equals ("DOM4J Tutorials")) {
Bookelement.remove (TitleElement);
}
}
}
Try {
/ ** Write the contents in Document * /
XMLWRITER Writer = New Filewriter (New File (New File (New File (NewFileName));
Writer.write (Document);
Writer.close ();
/ ** Execute success, need to return 1 * /
ReturnValue = 1;
} catch (exception ex) {
EX.PrintStackTrace ();
}
} catch (exception ex) {
EX.PrintStackTrace ();
}
Return ReturnValue;
}
/ **
* Format XML document and solve Chinese problems
* @Param FileName
* @Return
* /
Public int formatXMLFile (String filename) {
Int returnvalue = 0;
Try {
SAXReader saxreader = new saxreader ();
Document Document = SAXReader.read (New File (FileName));
XMLWRITER OUTPUT = NULL;
/ ** Format output, type IE browsing * /
Outputformat format = outputformat.createprettyPrint ();
/ ** Specify XML character set encoding * /
Format.setencoding ("GBK");
Output = New XMLWRITER (New File (New File (Filename), Format);
Output.write (document);
Output.close ();
/ ** Execute success, need to return 1 * /
ReturnValue = 1;
} catch (exception ex) {
EX.PrintStackTrace ();
}
Return ReturnValue;
}
Public static void main (string [] args) {DOM4JDEMO TEMP = New Dom4jdemo ();
System.out.println (Temp.createxmlfile))))); "D: //holen.xml");
System.out.println (Temp.Modixmlfile ("D: //holen.xml", "D: //holen2.xml");
System.out.println (Temp.FormatXMLFile ("D: //holen2.xml));
}
}