2013-04-02 75 views
0

我无法正确地将一些信息追加到我的xml文件中。这就是scrivi功能将节点追加到Java中的xml

public String scrivi (Document doc, File dest) 
    { 
    try 
    { 

     DOMSource sorgente = new DOMSource (doc); 
     StreamResult sr = new StreamResult (dest);   
     TransformerFactory tf = 
      TransformerFactory.newInstance(); 
     Transformer transf = tf.newTransformer(); 
     transf.transform (sorgente, sr); 
     return "Tutto ok"; 
    } 
    catch (TransformerConfigurationException tce) 
    { 

     System.out.println(tce.getMessage()); 
     return "<h1> Config </h1>"; 
    } 
    catch (TransformerException te) 
    { 

     System.out.println(te.getMessage()); 
     return "<h1> Transformer Errore </h1>"; 
    } 
    } 

和tath是我的代码:

try { 
       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
       DocumentBuilder db = dbf.newDocumentBuilder(); 
       Document document = db.parse(getClass().getResourceAsStream("/azioni.xml")); 


      Element root = document.getDocumentElement(); 
      Element new_azione = document.createElement("azione"); 
      Element id = document.createElement("id_azione"); 
      id.setTextContent(id_azione); 
      Element nome = document.createElement("nome_azione"); 
      nome.setTextContent(nome_azione); 
      Element prezzo_a = document.createElement("prezzo"); 
      prezzo_a.setTextContent(prezzo); 
      new_azione.appendChild(id); 
      new_azione.appendChild(nome); 
      new_azione.appendChild(prezzo_a); 
      document.getDocumentElement().appendChild(new_azione); 



      String nomexmlOut="/azioni.xml"; 



      File filedest = new File(nomexmlOut); 


      out.println(this.scrivi(document, filedest)); 

} 

我得到的错误变压器Errore ......我该怎么解决?怎么了? * UPDATE * 错误信息

java.io.FileNotFoundException: /azioni.xml (Permission denied) 
+0

显示完整的错误,所以我们可以看到它来自哪里。 –

+0

增加了错误信息 –

回答

2

很难说没有实际的异常跟踪或消息,但我的猜测是,你的问题是输出流。

File("/azioni.xml"); 

是不一样的

getClass().getResourceAsStream("/azioni.xml") 

尝试用定向输出系统,看看它是否工作。即宣布scrivi

public String scrivi (Document doc, OutputStream out) 

,并把它

scrivi(document, System.out); 

UPDATE:

要写入相同的文件位置,尝试这样的事情(未经测试)

文件移出=新文件(getClasss()的getResource( “...”)的GetFile());

并确保在尝试写入之前关闭最初读取的输入流。

+0

增加了错误信息谢谢 –

+0

好的,我是正确的,然后。您或进程无权写入/创建输出文件。尝试不带斜杠('/'); –

+0

我替换了字符串nomexmlOut =“/ azioni.xml”;用String nomexmlOut =“azioni.xml”;但我得到了相同的结果java.io.FileNotFoundException:/azioni.xml(权限被拒绝) –