2012-07-27 64 views
0

爲什麼我總是得到這個錯誤。我試圖追加數據到現有的XML文件。我讀了here的答案,並嘗試了建議。 但仍然沒有成功。我知道這個錯誤意味着最上面的根元素可以 只會來一次。但爲什麼我得到這個錯誤,我不知道。Android只允許一個根元素。 Xml錯誤

這將是我的XML文件的結構。

<root> 
    <ip>ip1</ip> 
    <ip>ip2</ip> 
</root> 

和IP標籤將繼續increasing.Here的我是多麼想讀和將數據追加到現有的文件。

private void UpdateExistingXML(String ip,File file) 
{ 
    try 
    { 
     DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); 
     Document doc = docBuilder.parse(file.toURI().toString()); // <--error here 

     // Get the root element 
     Node root= doc.getFirstChild(); 
     org.w3c.dom.Element newip=doc.createElement("ip"); 
     newip.appendChild(doc.createTextNode(ip)); 
     root.appendChild(newip); 
     TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
     Transformer transformer = transformerFactory.newTransformer(); 
     DOMSource source = new DOMSource(doc); 
     StreamResult result = new StreamResult(file); 
     transformer.transform(source, result); 
    } 
    catch (ParserConfigurationException pce) 
    { 
      pce.printStackTrace(); 
    } 
    catch (TransformerException tfe) 
    { 
      tfe.printStackTrace(); 
    } 
    catch (IOException ioe) 
    { 
      ioe.printStackTrace(); 
    } 
    catch (SAXException sae) 
    { 
      sae.printStackTrace(); 
    } 
    catch (Exception e) 
    { 
     Log.e("eeee",e.getMessage()); 
    } 
} 

以下是我首次創建xml文件的方式,它顯示根元素只插入一次。

private void CreateNewXML(String ip) throws FileNotFoundException 
{ 
    FileOutputStream fos=null ; 

    Log.i("Fileeee","new"); 
    try 
    { 
     fos = openFileOutput("clients.xml", Context.MODE_PRIVATE); 
    } 
    catch(FileNotFoundException e) 
    { 
      Log.e("FileNotFoundException", "can't create FileOutputStream"); 
    } 
    XmlSerializer serializer = Xml.newSerializer(); 
    try { 
        serializer.setOutput(fos, "UTF-8"); 
        serializer.startDocument(null, Boolean.valueOf(true)); 
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); 
        serializer.startTag(null, "root"); 

          serializer.startTag(null, "ip"); 
          serializer.text(ip); 
          serializer.endTag(null, "ip"); 

        serializer.endTag(null, "root"); 
        serializer.endDocument(); 
        serializer.flush(); 
        fos.close(); 

      } 
    catch (Exception e) 
    { 
        Log.e("Exceptionhaiiiiiiiiiii",e.getMessage()); 
    } 
} 
+0

你能後的錯誤日誌和/或logcat的 – BlackHatSamurai 2012-07-27 20:20:03

回答

0

這看起來像你的問題:

Document doc = docBuilder.parse(file.toURI().toString()); // <--error here 

您正試圖解析文件路徑,而不是文件。你需要創建一個inputStream並解析它。

如果從Web服務器獲取文件時,它可能是這樣的:

URL url = new URL(DATAURL); 
      conn = (HttpURLConnection) url.openConnection(); 
      conn.setReadTimeout(TIMEOUT); 
      conn.setConnectTimeout(CONNECT_TIMEOUT); 
      conn.setRequestMethod("GET"); 
      conn.setDoInput(true); 
      conn.connect(); 

DocumentBuilderFactory builderFactory = 
      DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder = null; 

     try 
     { 

      builder = builderFactory.newDocumentBuilder(); 
     } 
     catch (ParserConfigurationException e) 
     { 
      Log.e(TAG, "Parse Configuration issue", e); 
      throw new ServiceException("Service Exception Error"); 
     } 
     catch (IllegalAccessError e) 
     { 
      Log.e(TAG, "Illegal Accessor Error", e); 
      throw new ServiceException("Service Exception Error"); 
     } 

     try 
     { 
      // parse input from server 
      Document document = builder.parse(conn.getInputStream()); 
      Element xmlElement = document.getDocumentElement(); 
      NodeList recordNodes = xmlElement.getChildNodes(); 

      // assign parsed data to listItem object 
      for (int i = 0; i < recordNodes.getLength(); i++) 
      { 

       Node record = recordNodes.item(i); 
       NodeList recordDetails = record.getChildNodes(); 
       ListData listItem = new ListData(); 

       for (int ii = 0; ii < recordDetails.getLength(); ii++) 
       { 
        Node detailItem = recordDetails.item(ii); 
        String detailType = detailItem.getNodeName(); 
        String detailValue = detailItem.getTextContent(); 
.....