Write XML:
Import org. W3C. Dom .*;
Import javax. xml. parsers .*;
Import javax. xml. Transform .*;
Import javax. xml. Transform. Dom. domsource;
Import javax. xml. Transform. Stream. streamresult;
Import java. Io .*;
Public class xmlwrite {
Private document;
Private string filename;
Public xmlwrite (string name) throws parserconfigurationexception {
Filename = Name;
Documentbuilderfactory factory = documentbuilderfactory. newinstance ();
Documentbuilder builder = factory. newdocumentbuilder ();
Document = builder. newdocument ();
}
Public void towrite (string mytitle, string mycontent ){
Element root = Document. createelement ("Workshop ");
Document. appendchild (Root );
Element Title = Document. createelement ("title ");
Title. appendchild (document. createtextnode (mytitle ));
Root. appendchild (title );
Element Content = Document. createelement ("content ");
Content. appendchild (document. createtextnode (mycontent ));
Root. appendchild (content );
}
Public void tosave (){
Try {
Transformerfactory TF = transformerfactory. newinstance ();
Transformer transformer = TF. newtransformer ();
Domsource source = new domsource (document );
Transformer. setoutputproperty (outputkeys. encoding, "gb2312 ");
Transformer. setoutputproperty (outputkeys. indent, "yes ");
Printwriter PW = new printwriter (New fileoutputstream (filename ));
Streamresult result = new streamresult (PW );
Transformer. Transform (source, result );
}
Catch (transformerexception Mye ){
Mye. printstacktrace ();
}
Catch (ioexception exp ){
Exp. printstacktrace ();
}
}
Public static void main (string ARGs []) {
Try {
Xmlwrite myxml = new xmlwrite ("9.xml ");
Myxml. towrite ("Chinese Question", "Chinese content ");
Myxml. tosave ();
System. Out. Print ("your writing is successful .");
}
Catch (parserconfigurationexception exp ){
Exp. printstacktrace ();
System. Out. Print ("your writing is failed .");
}
}
}
Read XML:
Import java. Io .*;
Import java. util. vector;
Import javax. xml. parsers .*;
Import org. W3C. Dom .*;
Public class xmlread {
Static document;
Private Boolean validating;
Public xmlread (){
}
Public vector toread (string filename ){
Vector Title = new vector ();
Vector content = new vector ();
String mystr = new string ();
Try {
Documentbuilderfactory factory = documentbuilderfactory. newinstance ();
Factory. setvalidating (validating );
Documentbuilder builder = factory. newdocumentbuilder ();
Document = builder. parse (new file (filename ));
Document. getdocumentelement (). normalize ();
Node node = Document. getfirstchild ();
Nodelist list = node. getchildnodes ();
For (INT I = 0; I <list. getlength (); I ++ ){
Node nodeitm = List. item (I );
If (nodeitm. getnodename (). Equals ("title ")){
Mystr = nodeitm. getfirstchild (). getnodevalue ();
Title. addelement (mystr); // getfirstchild ()
}
If (nodeitm. getnodename (). Equals ("content ")){
Mystr = nodeitm. getfirstchild (). getnodevalue ();
Content. addelement (mystr );
}
}
} Catch (exception exp ){
Exp. printstacktrace ();
Return NULL;
}
Vector all = new vector ();
All. Add (title );
All. Add (content );
Return all;
}
Public static void main (string [] ARGs ){
Vector;
Xmlread my = new xmlread ();
A = My. toread ("9.xml ");
For (INT I = 0; I <A. Size (); I ++ ){
System. Out. println (A. elementat (I ));
}
}
}