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 7556715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:55:59+00:00 2026-05-30T11:55:59+00:00

public class LocalXMLActivity extends Activity { /** Called when the activity is first created.

  • 0
public class LocalXMLActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); // set the layout.. it refers main.xml
                                       // from the layout folder.
        InputStream is;
        DocumentBuilderFactory factory;
        DocumentBuilder builder;
        Document doc;
        NodeList allTitles = null;
        String dataAll;
        try {

            // Input Stream is used to read the bytes. getResources refers to
            // the android resources(res)folder and open RawResource to the raw
            // folder under res. (res/raw)
            is = this.getResources().openRawResource(R.raw.data);
            // Is to read the parser that prodcues from XML DOM.
            factory = DocumentBuilderFactory.newInstance();
            // Once an instance of this class is obtained, XML can be parsed
            // from a variety of input sources. These input sources are
            // InputStreams, Files, URLs, and SAX InputSources. There are many
            // sources to take data from internet.
            builder = factory.newDocumentBuilder();
            // the Document represents the entire HTML or XML document.
            // Conceptually, it is the root of the document tree, and provides
            // the primary access to the document's data.
            doc = builder.parse(is);
            // Retrieving the "company" node from xml
            allTitles = doc.getElementsByTagName("Company");

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        dataAll = "";
        for (int i = 0; i < allTitles.getLength(); i++) {
            dataAll += allTitles.item(i).getChildNodes().item(0).getNodeValue() + "\n";
        }

        TextView tv = (TextView)findViewById(R.id.myTextView); // finds the
                                                               // widget"myTextView"
                                                               // from main.xml
        tv.setText(dataAll); // setting the dataAll value to myTextView.
    }
}

xml file:res/raw/data.xml

    <?xml version="1.0" ?>
<content>
    <item>
        <Company>Google</Company>
        <Feature>Search Engine / Mail / Social Media</Feature>
        <url>www.google.com</url>
    </item>
    <item>
        <Company>Yahoo</Company>
        <Feature>Search Engine / Mail</Feature>
        <url>www.yahoo.com</url>
    </item>
</content>

the above code is only reading code,I have an XML file stored in the res/raw/data.xml that I am just reading, but how to write writing and updating in local xml file with inside reading source code write to writing and updating in local xml file

  • 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-05-30T11:56:01+00:00Added an answer on May 30, 2026 at 11:56 am

    You can create a new file on SD card like this:

    File Directory=  new File(Environment.getExternalStorageDirectory()
                            + File.separator + "working" +File.separator+"accounts");
    
    
    File masterXMLfile = new File(Directory,"MasterCategories.xml");
                     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                      //Get the DocumentBuilder
                      DocumentBuilder docBuilder = null;
                    try {
                        docBuilder = factory.newDocumentBuilder();
                    } catch (ParserConfigurationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                      //Create blank DOM Document
                      Document doc = docBuilder.newDocument();
    
    
                      //create the root element
                      Element root = doc.createElement("Categories");
                      //all it to the xml tree
                      doc.appendChild(root);
                      for(int i =0; i < nodeArray.length;i++)
                      {
                          System.out.println("entered into if loop");
                          Element subroot = doc.createElement("Category");
                          root.appendChild(subroot);
                          Element nameNode = doc.createElement("Name");
                          nameNode.appendChild(doc.createTextNode(nodeArray[i]));
                          subroot.appendChild(nameNode);
    
                          Element thumbNailNode = doc.createElement("thumbnail");
                          thumbNailNode.appendChild(doc.createTextNode(nodeArray[i]+".jpg"));
                          subroot.appendChild(thumbNailNode);
                          Element descriptionNode = doc.createElement("description");
                          descriptionNode.appendChild(doc.createTextNode(nodeArray[i]+" data"));
                          subroot.appendChild(descriptionNode);
                      }
                      TransformerFactory transfactory = TransformerFactory.newInstance();
                        Transformer transformer= null;
                        try {
                            transformer = transfactory.newTransformer();
                        } catch (TransformerConfigurationException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
    
                        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    
                        // create string from xml tree
                        // StringWriter sw = new StringWriter();
                        // StreamResult result = new StreamResult(sw);
                        FileWriter categoriesFW = null;
                        try {
                            categoriesFW = new FileWriter(masterXMLfile);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        StreamResult result = new StreamResult(categoriesFW);
                        DOMSource source = new DOMSource(doc);
                        try {
                            transformer.transform(source, result);
                        } catch (TransformerException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
    
                    if(!masterXMLfile.exists())
                    {
                        try {
                            masterXMLfile.createNewFile();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class video extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
public class Offer_Popup extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.offer_popup); //newly
public class SettingsActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /*
public class Browser1Activity extends Activity { TextView url; WebView ourBrow; @Override protected void onCreate(Bundle
public class Boards : TabActivity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Tab);
public class TestConnection extends Activity { /** Called when the activity is first created.
public class HomeActivity extends Activity{ // public ArrayList<User> users1 = new ArrayList<User>(); @Override public
public class async extends AsyncTask<String, Integer, String>{ ProgressDialog prog; @Override protected void onPreExecute() {
public class IdAsync extends AsyncTask<String, Void, Void> { AlertDialog alertDialog = new AlertDialog.Builder(MainClass.this).create(); protected
public class CustomEditor : Editor { protected override void Render(HtmlTextWriter writer) { Toolbar topToolbar

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.