解析文档对象以获取Xml文件

时间:2016-06-08 05:36:11

标签: java xml parsing dom

我正在解析我的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文件。 我怎么能这样做?

1 个答案:

答案 0 :(得分: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);