将XML文档保存在另一个xml文件中

时间:2016-07-14 20:15:26

标签: java xml dom

怎么了?此代码不保存并且不显示错误

此代码:

  1. 读取文件menuAdm.xml
  2. 对待该文件
  3. 将处理过的内容保存在另一个xml(menuAdm2.xml)

    DocumentBuilder dBuilder = null;
    try {
        dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = dBuilder.parse(new File("/seguranca/menuAdm.xml"));
    
    
        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        Element root = doc.getDocumentElement();
    
        // do treatment of doc (menuAdm.xml)            
    
        // save into another file (menuAdm2.xml)
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("/seguranca/menuAdm2.xml") );
        transformer.transform(source, result);
    
    } catch (Exception e) {
        System.out.println("Error to save file " + e.getMessage());
    }
    

0 个答案:

没有答案