Use the built-in JDK package to operate XML

Source: Internet
Author: User

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 ));
}
}
}

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.