2012-07-27 4 views
0

왜 항상이 오류가 발생합니다. 기존 XML 파일에 데이터를 추가하려고합니다. here의 대답을 읽고 제안 된 내용을 시도했습니다. 하지만 여전히 성공하지 못했습니다.이 오류는 최상위 루트 요소가 일 뿐이라는 것을 의미합니다. 그러나 왜 나는이 오류가 발생하는지 모르겠습니다.Android에만 하나의 루트 요소가 허용됩니다. Xml 오류

이것은 내 xml 파일의 구조입니다.

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

그리고 ip 태그는 계속 증가 할 것입니다. 여기 어떻게 기존 파일에 데이터를 읽고 추가하려고하는지 알려줍니다.

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

는 오류 로그 및/또는 로그 캣을 게시 할 수 – BlackHatSamurai

답변

0

이 문제처럼 보이는 : 당신은 오히려 파일보다 파일 경로를 분석하려고

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

. inputStream을 만들고 구문 분석해야합니다. 당신은 웹 서버에서 파일을 얻는 경우

는 다음과 같습니다

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(); 
..... 
관련 문제