. Dom generate and parse XML documents

Source: Internet
Author: User


Package com. alisoft. facepay. Framework. Bean;
Import java. Io. fileinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. printwriter;
Import javax. xml. parsers. documentbuilder;
Import javax. xml. parsers. documentbuilderfactory;
Import javax. xml. parsers. parserconfigurationexception;
Import javax. xml. Transform. outputkeys;
Import javax. xml. Transform. transformer;
Import javax. xml. Transform. transformerconfigurationexception;
Import javax. xml. Transform. transformerexception;
Import javax. xml. Transform. transformerfactory;
Import javax. xml. Transform. Dom. domsource;
Import javax. xml. Transform. Stream. streamresult;
Import org. W3C. Dom. Document;
Import org. W3C. Dom. element;
Import org. W3C. Dom. node;
Import org. W3C. Dom. nodelist;
Import org. xml. Sax. saxexception;
/**
*
* @ Author hongliang. dinghl
* Dom generation and parsing of XML documents
*/
Public class domdemo implements xmldocument {
Private document;
Private string filename;
Public void Init (){
Try {
Documentbuilderfactory factory = documentbuilderfactory
. Newinstance ();
Documentbuilder builder = factory. newdocumentbuilder ();
This.doc ument = builder. newdocument ();
} Catch (parserconfigurationexception e ){
System. Out. println (E. getmessage ());
}
}
Public void createxml (string filename ){
Element root = this.doc ument. createelement ("employees ");
This.doc ument. appendchild (Root );
Element Employee = this.doc ument. createelement ("employee ");
Element name = this.doc ument. createelement ("name ");
Name.appendchild(this.doc ument. createtextnode ("ding hongliang "));
Employee. appendchild (name );
Element sex = this.doc ument. createelement ("sex ");
Sex.appendchild(this.doc ument. createtextnode ("M "));
Employee. appendchild (sex );
Element age = this.doc ument. createelement ("Age ");
Age.appendchild(this.doc ument. createtextnode ("30 "));
Employee. appendchild (AGE );
Root. appendchild (employee );
Transformerfactory TF = transformerfactory. newinstance ();
Try {
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 );
System. Out. println ("XML file generated successfully! ");
} Catch (transformerconfigurationexception e ){
System. Out. println (E. getmessage ());
} Catch (illegalargumentexception e ){
System. Out. println (E. getmessage ());
} Catch (filenotfoundexception e ){
System. Out. println (E. getmessage ());
} Catch (transformerexception e ){
System. Out. println (E. getmessage ());
}
}
Public void parserxml (string filename ){
Try {
Documentbuilderfactory DBF = documentbuilderfactory. newinstance ();
Documentbuilder DB = DBF. newdocumentbuilder ();
Document document = dB. parse (filename );
Nodelist employees = Document. getchildnodes ();
For (INT I = 0; I <employees. getlength (); I ++ ){
Node Employee = employees. item (I );
Nodelist employeeinfo = employee. getchildnodes ();
For (Int J = 0; j <employeeinfo. getlength (); j ++ ){
Node node = employeeinfo. Item (j );
Nodelist employeemeta = node. getchildnodes ();
For (int K = 0; k <employeemeta. getlength (); k ++ ){
System. Out. println (employeemeta. Item (k). getnodename ()
+ ":" + Employeemeta. Item (k). gettextcontent ());
}
}
}
System. Out. println ("parsed ");
} Catch (filenotfoundexception e ){
System. Out. println (E. getmessage ());
} Catch (parserconfigurationexception e ){
System. Out. println (E. getmessage ());
} Catch (saxexception e ){
System. Out. println (E. getmessage ());
} Catch (ioexception e ){
System. Out. println (E. getmessage ());
}
}
}
---------------
Package com. alisoft. facepay. Framework. Bean;

Import java. Io. filenotfoundexception;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. util. List;

Import org. JDOM. Document;
Import org. JDOM. element;
Import org. JDOM. jdomexception;
Import org. JDOM. Input. saxbuilder;
Import org. JDOM. Output. xmloutputter;
/**
*
* @ Author hongliang. dinghl
* JDOM generation and parsing of XML documents
*
*/
Public class jdomdemo implements xmldocument {

Public void createxml (string filename ){
Document document;
Element root;
Root = new element ("employees ");
Document = new document (Root );
Element Employee = new element ("employee ");
Root. addcontent (employee );
Element name = new element ("name ");
Name. settext ("ddvip ");
Employee. addcontent (name );
Element sex = new element ("sex ");
Sex. settext ("M ");
Employee. addcontent (sex );
Element age = new element ("Age ");
Age. settext ("23 ");
Employee. addcontent (AGE );
Xmloutputter xmlout = new xmloutputter ();
Try {
Xmlout. Output (document, new fileoutputstream (filename ));
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}

}

Public void parserxml (string filename ){
Saxbuilder builder = new saxbuilder (false );
Try {
Document document = builder. Build (filename );
Element employees = Document. getrootelement ();
List employeelist = employees. getchildren ("employee ");
For (INT I = 0; ielement Employee = (element) employeelist. Get (I );
List employeeinfo = employee. getchildren ();
For (Int J = 0; jsystem. out. println (element) employeeinfo. get (j )). getname () + ":" + (element) employeeinfo. get (j )). getvalue ());

}
}
} Catch (jdomexception e ){

E. printstacktrace ();
} Catch (ioexception e ){

E. printstacktrace ();
}

}
}

 

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.