String XML generates an XML file

Source: Internet
Author: User



/**
* Convert the XML of a string into a Org.w3c.dom.Document object
* @param xml
* @return
*/
public static Document getdocument (String xml) {
Document document = NULL;
try {
Documentbuilderfactory dbf = Documentbuilderfactory.newinstance ();
Documentbuilder db = Dbf.newdocumentbuilder ();
InputStream is = new Bytearrayinputstream (Xml.getbytes ());
Document = Db.parse (IS);
} catch (Exception e) {
E.printstacktrace ();
}
return document;
}
/**
* Writes the Org.w3c.dom.Document object to the specified file
*
* @param doc
* @param fileName
* @throws Exception
*/
private static void Outputxml (Document doc, String fileName) {
try {
Transformerfactory tf = Transformerfactory.newinstance ();
Transformer Transformer = Tf.newtransformer ();
Domsource Source = new Domsource (DOC);
Transformer.setoutputproperty (outputkeys.encoding, "UTF-8");
Transformer.setoutputproperty (outputkeys.indent, "yes");//increment line indent, but indent defaults to 0
Transformer.setoutputproperty ("{Http://xml.apache.org/xslt}indent-amount", "2");//Set Indent to 2
PrintWriter pw = new PrintWriter (new OutputStreamWriter (New FileOutputStream (FileName), "UTF-8"));
Streamresult result = new Streamresult (PW);
Transformer.transform (source, result);
} catch (Exception e) {
E.printstacktrace ();
}
}


String XML generates an XML file

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.