Android development (5)-use the original XML file

Source: Internet
Author: User

Application scenarios

During Android Application Development, sometimes we manually create some XML files, which may be used in the project development process, that is to say, the original XML resource file is used. The original XML resource file can be obtained through the following two methods of the Resouce class:

GetResouce. getXml (int id );

GetRawResouce (int id)

The objects returned by these two methods are objects of the XmlResouceParser class. The XmlResouceParser class is a subclass of the XmlPullParser class. Therefore, we can use the built-in Pull mode of Android to parse our XML files. This is my favorite method for parsing XML files. Compared with DOM parsing, it has a small memory and fast resolution. Compared with SAX, writing code is not that complicated. You only need to write a little code to parse the XML file.

For details about parsing XML files in the PULL mode, refer to an article I wrote earlier:

Parsing XML files in the Android platform using the PULL mode

 

Http://www.bkjia.com/kf/201112/115458.html

 

Example of parsing in PULL mode: use the original XML file

 

 

 

Effect:

 

 

Implementation process:

1. Create an xml folder under res/, and create a person. xml file under this folder. The content is as follows:


<? Xml version = "1.0" encoding = "UTF-8"?>
<Persons>
<Person id = "1">
<Name> Zhang San </name>
<Age> 20 </age>
</Person>
<Person id = "2">
<Name> Li Si </name>
<Age> 22 </age>
</Person>
</Persons>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Persons>
<Person id = "1">
<Name> Zhang San </name>
<Age> 20 </age>
</Person>
<Person id = "2">
<Name> Li Si </name>
<Age> 22 </age>
</Person>
</Persons>
 

 

2. Compile the layout file: main. xml


<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<Button
Android: id = "@ + id/btnParser"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "parsing original XML resource files"/>
<EditText
Android: id = "@ + id/edtContent"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: lines = "5"/>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<Button
Android: id = "@ + id/btnParser"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "parsing original XML resource files"/>
<EditText
Android: id = "@ + id/edtContent"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: lines = "5"/>
</LinearLayout>
 

3. Compile the Activity code


Package com. jiahui. xmlres;
 
Import android. app. Activity;
Import android. content. res. XmlResourceParser;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
 
Public class XmlResTestActivity extends Activity {
 
Private Button btnParser;
Private EditText edtContent;
 
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
BtnParser = (Button) this. findViewById (R. id. btnParser );
EdtContent = (EditText) this. findViewById (R. id. edtContent );

BtnParser. setOnClickListener (new View. OnClickListener (){
 
@ Override
Public void onClick (View v ){
// Obtain the native xml file under res/XML
XmlResourceParser parser = getResources (). getXml (R. xml. persons );
Try {
 
StringBuilder sb = new StringBuilder ();
Int eventType = parser. getEventType ();
While (eventType! = XmlResourceParser. END_DOCUMENT ){
 
Switch (eventType ){
Case XmlResourceParser. START_TAG:
 
String tagName = parser. getName ();
 
If ("person"). equals (tagName )){
 
// Obtain the first attribute
String personid = parser. getAttributeValue (0 );
Sb. append ("ID:" + personid );
 
} Else if ("name". equals (parser. getName ())){
String name = parser. nextText ();
Sb. append ("name:" + name );
 
} Else if ("age". equals (parser. getName ())){
String age = parser. nextText ();
Sb. append ("age:" + age );
}
Break;
Case XmlResourceParser. END_TAG:
// Add a blank line at the end
If ("person". equals (parser. getName ())){
Sb. append ("\ n ");
}
Break;
Default:
Break;
}
// Parse the next event www.2cto.com
EventType = parser. next ();
}
// Put the parsed result in the text edit box
EdtContent. setText (sb. toString ());
 
} Catch (Exception e ){
// TODO: handle exception
}
}
});
}
}

From jiahui524 Column
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.