Giannis Koutivas Giannis Koutivas - 1 month ago 7
Android Question

XML simple parsing

I have a question about parsing (Android Studio). It is not about something in particular. My code just doesn't run. No errors. I want to be able to press a button and show a specific text, parsed from an XML file.

For now, i'll omit the button part and throw you the codes just for printing this text on a humble textview

XML CODE (app/src/main/assets follder. articles.xml)

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE articles[
<!ELEMENT articles ANY>
<!ELEMENT article ANY>
<!ATTLIST article ID ID #IMPLIED> ]>
<articles>
<article ID="a1">TEST
</article>
<article ID="a2">1.TEST2
</article>




CLASS CODE

package com.blah.blah;

import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class MainActivity extends AppCompatActivity {

TextView textView;
String stringArticle;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

AssetManager assetManager = this.getAssets();
InputStream inputStream = null;

try {

// Loads your XML file as an InputStream
inputStream = assetManager.open("articles");

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(inputStream);

Element article = doc.getElementById("a1");
String stringArticle = article.getTextContent();

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(stringArticle);

} catch (IOException e) {
// Exception Handling Code
e.printStackTrace();
}catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}


I haven't tried the techniques that return you a Nodelist, because I dont want to iterate over anything. I find it useless for my project. What I want is something extremely primitive. It's like those simple bible apps. You press the button with the verse and the corresponding text pops up. That simple.

And again, no errors at all! Just not working. I think the TRY part is not executed. Because it gives me the default 'hello world' text on the TextView. But, once i put the

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(stringarticle);


lines after the TRY/CATCH blocks, I just get an empty TextView.

TY!

Answer

You can place your XML file in "app/src/main/assets" directory. After that you can easily access your XML file via "AssetManager" class.

You can use below code as a reference:-

 AssetManager assetManager = this.getAssets();
    InputStream inputStream = null;
    try {
        // Loads your XML file as an InputStream
        inputStream = assetManager.open("articles.xml");

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(inputStream);
        Element article = doc.getElementById("1");
        String stringarticle = article.getTextContent();

        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(stringarticle);

    } catch (IOException e) {
        // Exception Handling Code
        e.printStackTrace();
    }catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }

Just Remember to place your file in mentioned directory, otherwise "AssetManager" class won't be able to read it.

Cheers !!