Write xml files in Java

Source: Internet
Author: User

[Java]
Package com. biztool;
 
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. OutputStreamWriter;
Import java. io. Writer;
 
 
Import javax. xml. parsers. DocumentBuilder;
Import javax. xml. parsers. DocumentBuilderFactory;
Import javax. xml. transform. OutputKeys;
Import javax. xml. transform. Result;
Import javax. xml. transform. Source;
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. Text;
 
 
 
 
Public class WriteXml {
Private void writeXMLFile (String xmlfile ){
File file = new File ("E:/tryfile/xml ");
File. listFiles ();
If (! File. exists ()){

File. mkdirs ();
}
DocumentBuilderFactory dbf = DocumentBuilderFactory. newInstance ();
DocumentBuilder dbuilder = null;
Try {
Dbuilder = dbf. newDocumentBuilder ();
} Catch (Exception ex ){
Ex. printStackTrace ();
}

Document doc = dbuilder. newDocument ();

Element root = doc. createElement ("student roster ");
Doc. appendChild (root );

Element stu = doc. createElement ("student ");
Stu. setAttribute ("gender", "male ");
Root. appendChild (stu );

Element stu_name = doc. createElement ("name ");
Stu. appendChild (stu_name );
Text name_text = doc. createTextNode ("Luo Wenwen ");
Stu_name.appendChild (name_text );

Element stu_age = doc. createElement ("Age ");
Stu. appendChild (stu_age );
Text age_text = doc. createTextNode ("25 ");
Stu_age.appendChild (age_text );
Try {
FileOutputStream fos = new FileOutputStream (xmlfile + "/test. xml ");
OutputStreamWriter osw = new OutputStreamWriter (fos );
CallDomWriter (doc, osw, "UTF-8 ");
Osw. close ();
Fos. close ();
} Catch (Exception ex ){
Ex. printStackTrace ();
}


}

Public void callDomWriter (Document dom, Writer writer, String encoding ){
Try {

Source source = new DOMSource (dom );
Result res = new StreamResult (writer );
Transformer xformer = TransformerFactory. newInstance (). newTransformer ();
Xformer. setOutputProperty (OutputKeys. ENCODING, encoding );
Xformer. transform (source, res );
} Catch (TransformerConfigurationException e ){
E. printStackTrace ();
} Catch (TransformerException e ){
E. printStackTrace ();
}

}

Public static void main (String [] args ){
WriteXml writeins = new WriteXml ();
String outfile = "E:/tryfile/xml ";
Writeins. writeXMLFile (outfile );
System. out. println ("finished ");
}
}

 

Related Article

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.