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 файл: 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>
приведенный выше код только читает код, у меня есть файл XML, сохраненный в файле res / raw / data.xml, который яЯ просто читаю, но как писать записи и обновления в локальном файле XML с внутренним чтением исходного кода записи для записи и обновления в локальном файле XML