n_erudite > 12-15-2015, 09:17 AM
xmlpullparser parser = Xml.newPullParser();XmlPullParserFactory pullParserFactory;
try {
pullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = pullParserFactory.newPullParser();
} catch (XmlPullParserException e) {
e.printStackTrace();
}<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<productname>Jeans</productname>
<productcolor>red</productcolor>
<productquantity>5</productquantity>
</product>
<product>
<productname>Tshirt</productname>
<productcolor>blue</productcolor>
<productquantity>3</productquantity>
</product>
<product>
<productname>shorts</productname>
<productcolor>green</productcolor>
<productquantity>4</productquantity>
</product>
</products>class Product
{
public String name;
public String quantity;
public String color;
}
public class XMLDemo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
XmlPullParserFactory pullParserFactory;
try {
pullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = pullParserFactory.newPullParser();
InputStream in_s = getApplicationContext().getAssets().open("temp.xml");
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in_s, null);
parseXML(parser);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
{
ArrayList<product> products = null;
int eventType = parser.getEventType();
Product currentProduct = null;
while (eventType != XmlPullParser.END_DOCUMENT){
String name = null;
switch (eventType){
case XmlPullParser.START_DOCUMENT:
products = new ArrayList();
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name == "product"){
currentProduct = new Product();
} else if (currentProduct != null){
if (name == "productname"){
currentProduct.name = parser.nextText();
} else if (name == "productcolor"){
currentProduct.color = parser.nextText();
} else if (name == "productquantity"){
currentProduct.quantity= parser.nextText();
}
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("product") && currentProduct != null){
products.add(currentProduct);
}
}
eventType = parser.next();
}
printProducts(products);
}
private void printProducts(ArrayList</product><product> products)
{
String content = "";
Iterator</product><product> it = products.iterator();
while(it.hasNext())
{
Product currProduct = it.next();
content = content + "nnnProduct :" + currProduct.name + "n";
content = content + "Quantity :" + currProduct.quantity + "n";
content = content + "Color :" + currProduct.color + "n";
}
TextView display = (TextView)findViewById(R.id.info);
display.setText(content);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}![[Image: image002.jpg]](http://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2013/06/image002.jpg)
try {
DocumentBuilderFactory DOMfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder DOMbuilder = DOMfactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}SAXParserFactory SAXfactory = SAXParserFactory.newInstance();
try {
SAXParser SAXParserObj = SAXfactory.newSAXParser();
} catch (Exception e) {
e.printStackTrace();
}