2016-06-08 41 views
0

我解析我的XML使用功能解析文檔對象獲得的Xml文件

public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, KeyException, MarshalException, XMLSignatureException, TransformerException 
{ 
    File fXmlFile = new File("C:\\Users\\admin\\Desktop\\Esign.xml"); 
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
    Document doc = dBuilder.parse(fXmlFile); 

    test t=new test(); 
    Document d=t.sign(doc); 
} 

,但我需要的輸出((文件d))轉換回XML文件。 我該怎麼做?

+0

標準方法是使用XSLT轉換和[Identity Transform](https://en.wikipedia.org/wiki/Identity_transform) –

回答

0

這是我一直在做的方式。

//After Document d is created. 
TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
Transformer transformer = transformerFactory.newTransformer(); 
DOMSource source = new DOMSource(d); 
StreamResult result = new StreamResult(new File("C:\\file.xml")); 
transformer.transform(source, result);