0

为什么我总是收到此错误。我正在尝试将数据附加到现有的 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());
    }
}
4

1 回答 1

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();
.....
于 2012-07-27T20:25:20.897 回答