【问题标题】:Android only one root element is allowd. Xml errorAndroid 只允许一个根元素。 xml 错误
【发布时间】:2012-07-27 18:44:11
【问题描述】:

为什么我总是收到此错误。我正在尝试将数据附加到现有的 xml 文件中。我阅读了here 的答案并尝试了建议的内容。 但仍然没有成功。我知道这个错误意味着顶部根元素可以 只来一次。但我不知道为什么会出现这个错误。

这将是我的 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());
    }
}

【问题讨论】:

  • 你能发布错误日志和/或logcat

标签: android


【解决方案1】:

这看起来像你的问题:

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();
.....

【讨论】:

    猜你喜欢
    • 2019-10-15
    • 2011-09-23
    • 2020-04-23
    • 1970-01-01
    • 2012-07-14
    • 2016-07-30
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多