Read XML file using XmlPullParser in Sketchware

To read an Xml String do following:

1. Create a more block readXmlString [myxmlData] to [TextView: textview]. Put following codes in it.

try {

org.xmlpull.v1.XmlPullParserFactory factory = org.xmlpull.v1.XmlPullParserFactory.newInstance();

factory.setNamespaceAware(true);

org.xmlpull.v1.XmlPullParser xpp = factory.newPullParser();

xpp.setInput(new java.io.StringReader(_xmlData));

int eventType = xpp.getEventType();

String text = "";

while (eventType != org.xmlpull.v1.XmlPullParser.END_DOCUMENT) {

if (eventType == org.xmlpull.v1.XmlPullParser.START_DOCUMENT) {

} else if (eventType == org.xmlpull.v1.XmlPullParser.START_TAG) {

text = text + xpp.getName();

} else if (eventType == org.xmlpull.v1.XmlPullParser.END_TAG) {

} else if (eventType == org.xmlpull.v1.XmlPullParser.TEXT) {

text = text +" "+ xpp.getText();

}

eventType = xpp.next();

}

_textview.setText(text);

} catch (org.xmlpull.v1.XmlPullParserException xppe){

showMessage(xppe.getMessage());

} catch (java.io.IOException ioe){

showMessage(ioe.getMessage());

}


2. In VIEW area, add a TextView textview1.

3. Create a String variable myxmldata.

4. In onCreate, set myxmldata to data in xml file, and use more block to display it in textview1.

To read an Xml file in assets folder do following:

1. Create a more block readXmlFile [myxmlFile] to [TextView: textview]. Put following codes in it.

try {

org.xmlpull.v1.XmlPullParserFactory factory = org.xmlpull.v1.XmlPullParserFactory.newInstance();

factory.setNamespaceAware(true);

org.xmlpull.v1.XmlPullParser xpp = factory.newPullParser();

xpp.setInput(new java.io.InputStreamReader(getAssets().open(_myxmlFile)));

int eventType = xpp.getEventType();

String text = "";

while (eventType != org.xmlpull.v1.XmlPullParser.END_DOCUMENT) {

if (eventType == org.xmlpull.v1.XmlPullParser.START_DOCUMENT) {

} else if (eventType == org.xmlpull.v1.XmlPullParser.START_TAG) {

text = text + xpp.getName();

} else if (eventType == org.xmlpull.v1.XmlPullParser.END_TAG) {

} else if (eventType == org.xmlpull.v1.XmlPullParser.TEXT) {

text = text +" "+ xpp.getText();

}

eventType = xpp.next();

}

_textview.setText(text);

} catch (org.xmlpull.v1.XmlPullParserException xppe){

showMessage(xppe.getMessage());

} catch (java.io.IOException ioe){

showMessage(ioe.getMessage());

}


2. In VIEW area, add a TextView textview1.

3. If you have XML file countries.xml, add countries.xml to .sketchware/mysc/699(project number)/app/src/main/assets/ folder.

4. In onCreate, use more block to display countries.xml in textview1.

See video below:




Comments

Popular posts from this blog

Create Music Player app in Sketchware

A Flash Light App in Sketchware

Admob rewarded video Ads in Sketchware

Firebase auth in Sketchware for Login Activity

Simple car racing android game in Sketchware