Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8308459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:49:18+00:00 2026-06-08T18:49:18+00:00

Why I am always getting this error.I am trying to append data to existing

  • 0

Why I am always getting this error.I am trying to append data to existing xml file.I read here the answer and tried what was suggested.
But still no success.I know this error mean that the top root element can
only come once.But why I am getting this error I don’t know.

This will be the structure of my xml file.

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

And the ip tag will keep on increasing.Here’s how I am trying to read and append data to existing file.

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

Here’s how I am creating the xml file for the first time which shows the root element is only inserted once.

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());
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:49:20+00:00Added an answer on June 8, 2026 at 6:49 pm

    This looks like your problem:

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

    You are trying to parse the file path, rather than the file. You need to create an inputStream and parse that.

    If you are getting the file from a web server it might look like this:

    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();
    .....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run Junit test from my ant build.xml file. I read here
I seem to be getting this particular always when i am trying to receive
am trying send an array to php file using $_POST ,still always getting an
I am trying to grab a webpage with actionscript, but keep getting this error
I'm getting this error when trying to run unit tests from Eclipse with an
All of a sudden I start getting this error while trying to open 2
So I keep getting this error when trying to compile C++ code using CodeBlocks.
i m getting this force down error in my app,when i m trying to
I am getting this error while trying to connect my web site. Unspecified error
I am trying to make a button. I am always getting a R.id error.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.