Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read XML file in android

I want to read xml file which looks like the following ... I have stored it in assets folder with

<ImageList SpriteSheetName="hh_gmw01">
   <Image Name="gmw01"     x="0" y="0" width="1047" height="752"/>
   <Image Name="hht1l01"   x="388" y="269" width="34" height="36"/>
   <Image Name="hht1l02"   x="147" y="99" width="85" height="33"/>
</ImageList>

How do I get these values?

like image 800
user1169079 Avatar asked Feb 27 '12 11:02

user1169079


People also ask

How do I read XML files?

XML files can be opened in a browser like IE or Chrome, with any text editor like Notepad or MS-Word. Even Excel can be used to open XML files.

What is an XML file in Android?

XML stands for eXtensible Markup Language, which is a way of describing data using a text-based document. Because XML is extensible and very flexible, it's used for many different things, including defining the UI layout of Android apps.

Does Android uses XML?

In Android, the XML is used to implement UI-related data, and it's a lightweight markup language that doesn't make layout heavy.


2 Answers

There is several ways to read a XML in Android. My first option is DocumentBuilder since do not create an API version restriction (is available since API Level 1).

An example from one of my projects:

public Document parseXML(InputSource source) {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(false);
        dbf.setValidating(false);
        DocumentBuilder db = dbf.newDocumentBuilder();
        return db.parse(source);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
 }

How do you read the file and access the document values after this, well, its pretty basic, just Google it.

like image 80
Rui Carneiro Avatar answered Oct 11 '22 02:10

Rui Carneiro


/*
* <ImageList SpriteSheetName="hh_gmw01">
*       <Image Name="gmw01"     x="0" y="0" width="1047" height="752"/>
*       <Image Name="hht1l01"   x="388" y="269" width="34" height="36"/>
*       <Image Name="hht1l02"   x="147" y="99" width="85" height="33"/>
* </ImageList>
*/

private void testXML() throws XmlPullParserException {
    AssetManager assetManager = getAssets();
    try {
        InputStream xmlStream = assetManager.open("xmlfile.xml");
        try {
            XmlPullParser xmlParser = Xml.newPullParser();
            xmlParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            xmlParser.setInput(xmlStream, null);
            xmlParser.nextTag();

            int imgNum = 0;
            xmlParser.require(XmlPullParser.START_TAG, null, "ImageList"/*root node*/);
            Log.d("ImageList", "SpriteSheetName = " + xmlParser.getAttributeValue(null, "SpriteSheetName"));
            while (xmlParser.next() != XmlPullParser.END_TAG) {
                if (xmlParser.getEventType() != XmlPullParser.START_TAG) {
                    continue;
                }

                xmlParser.require(XmlPullParser.START_TAG, null, "Image");
                //. fetch nodes attributes here...
                Log.d("xmlNode", "img " + (++imgNum) +
                           " Name = " + xmlParser.getAttributeValue(null, "Name") +
                        " x = "       + xmlParser.getAttributeValue(null, "x") +
                        " y = "       + xmlParser.getAttributeValue(null, "y") +
                        " width = "   + xmlParser.getAttributeValue(null, "width") +
                        " height = "  + xmlParser.getAttributeValue(null, "height")
                );
                //................................
                xmlParser.nextTag();
                xmlParser.require(XmlPullParser.END_TAG, null, "Image");
            }


        } finally {
            xmlStream.close();
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    assetManager.close();
}
like image 24
tdjprog Avatar answered Oct 11 '22 02:10

tdjprog