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:
Nice blog . I have also been making tutorials related to Sketchware. In my Sketchware Interface video I have explaines each and every part of the Sketchware app itself. Do check my channel to learn more about sketchware.
ReplyDeleteMy channel link - https://youtube.com/channel/UCOUL_rPb3vQZoQ_ENC9lB3w
Thanks Sanjeev sir. I have learnt about sketchware by watching your videos . So Thanks a lot.