Write the string in xml format to an xml file

package demo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.Date;

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.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class DocXml {
    public static void main(String[] args) {
        String s1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><img src=\"http://b0.upaiyun.com/1523978382096.jpg\"><h1>百度</h1></img>";
        Document doc = str2Document(s1);
        String fileName = "E:\\learn\\a.html";
        createXml(fileName, doc);
        
    }
    
    
    public static Document str2Document(String xmlStr) {
        Document doc = null;
        StringReader sr = new StringReader(xmlStr);
        InputSource is = new InputSource(sr);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        try {
            builder = factory.newDocumentBuilder();
            doc = builder.parse(is);
        } catch (ParserConfigurationException e) {
            System.out.println("ParserConfiguration错误"+e);
        } catch (SAXException e) {
            System.out.println("SAX错误"+e);
        } catch (IOException e) {
            System.out.println("IO错误"+e);
        }
        return doc;
    }
    
    
    public synchronized static void createXml(String fileName, Document document) {
        System.out.println( "============Enter the method to generate xml:" + new Date().toLocaleString() + "============== ===" );
         try {
             // Determine whether the file exists, delete it if it exists 
            File file = new File(fileName);
             if (file.exists()) {
                file.delete();
                System.out.println( "==============Delete xml file==============" );
            }
            /** Write the contents of document to the file */ 
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(new FileOutputStream(fileName));
            transformer.transform(source, result);
            System.out.println( "--------------------------------" + "Update XML file successfully" + "- ------------------------------------" );
        } catch (final Exception exception) {
            System.out.println("更新" + fileName + "出错:"+exception);
        }
        System.out.println( "============Exit generate xml method: " + new Date().toLocaleString() + "============== ===" );
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324727363&siteId=291194637