Note that there are some explanatory texts on larger screens.

plurals
  1. POXML parsing from local xml
    primarykey
    data
    text
    <p>I'm parsing XML from URL. What changes has been made to parse same XML file from raw folder. Have any idea to how to reduce code ? </p> <p>This my xml file :umesh.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;appdata&gt; &lt;brand name="Lovely Products"&gt; &lt;product&gt;Hat&lt;/product&gt; &lt;product&gt;Gloves&lt;/product&gt; &lt;/brand&gt; &lt;brand name="Great Things"&gt; &lt;product&gt;Table&lt;/product&gt; &lt;product&gt;Chair&lt;/product&gt; &lt;product&gt;Bed&lt;/product&gt; &lt;/brand&gt; &lt;/appdata&gt; </code></pre> <p>Below is my java file :</p> <ol> <li><p><strong>DataHandler.java</strong></p> <pre><code>package com.umesh.xmlparsing; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import android.content.Context; import android.content.res.XmlResourceParser; import android.graphics.Color; import android.util.Log; import android.widget.TextView; public class DataHandler extends DefaultHandler{ //list for imported product data private ArrayList&lt;TextView&gt; theViews; //string to track each entry private String currBrand = ""; //flag to keep track of XML processing private boolean isProduct = false; //context for user interface private Context theContext; //constructor public DataHandler(Context cont) { super(); theViews = new ArrayList&lt;TextView&gt;(); theContext = cont; } //start of the XML document public void startDocument () { Log.i("DataHandler", "Start of XML document"); } //end of the XML document public void endDocument () { Log.i("DataHandler", "End of XML document"); } //opening element tag public void startElement (String uri, String name, String qName, Attributes atts) { //handle the start of an element //find out if the element is a brand if(qName.equals("brand")) { //set product tag to false isProduct = false; //create View item for brand display TextView brandView = new TextView(theContext); brandView.setTextColor(Color.rgb(73, 136, 83)); //add the attribute value to the displayed text String viewText = "Items from " + atts.getValue("name") + ":"; brandView.setText(viewText); //add the new view to the list theViews.add(brandView); } //the element is a product else if(qName.equals("product")) isProduct = true; } //closing element tag public void endElement (String uri, String name, String qName) { //handle the end of an element if(qName.equals("brand")) { //create a View item for the products TextView productView = new TextView(theContext); productView.setTextColor(Color.rgb(192, 199, 95)); //display the compiled items productView.setText(currBrand); //add to the list theViews.add(productView); //reset the variable for future items currBrand = ""; } } //element content public void characters (char ch[], int start, int length) { //process the element content //string to store the character content String currText = ""; //loop through the character array for (int i=start; i&lt;start+length; i++) { switch (ch[i]) { case '\\': break; case '"': break; case '\n': break; case '\r': break; case '\t': break; default: currText += ch[i]; break; } } //prepare for the next item if(isProduct &amp;&amp; currText.length()&gt;0) currBrand += currText+"\n"; } public ArrayList&lt;TextView&gt; getData() { //take care of SAX, input and parsing errors try { //set the parsing driver System.setProperty("org.xml.sax.driver","org.xmlpull.v1.sax2.Driver"); //create a parser SAXParserFactory parseFactory = SAXParserFactory.newInstance(); SAXParser xmlParser = parseFactory.newSAXParser(); //get an XML reader XMLReader xmlIn = xmlParser.getXMLReader(); //instruct the app to use this object as the handler xmlIn.setContentHandler(this); //provide the name and location of the XML file **ALTER THIS FOR YOUR FILE** URL xmlURL = new URL("http://mydomain.com/umesh.xml"); //open the connection and get an input stream URLConnection xmlConn = xmlURL.openConnection(); InputStreamReader xmlStream = new InputStreamReader(xmlConn.getInputStream()); //build a buffered reader BufferedReader xmlBuff = new BufferedReader(xmlStream); // uuu XmlResourceParser todolistXml = getResources().getXml(R.raw.c4mh_clinics); //parse the data xmlIn.parse(new InputSource(xmlBuff)); } catch(SAXException se) { Log.e("AndroidTestsActivity", "SAX Error " + se.getMessage()); } catch(IOException ie) { Log.e("AndroidTestsActivity", "Input Error " + ie.getMessage()); } catch(Exception oe) { Log.e("AndroidTestsActivity", "Unspecified Error " + oe.getMessage()); } //return the parsed product list return theViews; } } </code></pre></li> <li><p><strong>XMLParsing.java</strong></p> <pre><code>package com.umesh.xmlparsing; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.widget.LinearLayout; import android.widget.TextView; public class XMLParsing extends Activity { TextView tv; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //get a reference to the layout LayoutInflater inflater = getLayoutInflater(); LinearLayout mainLayout = (LinearLayout) inflater.inflate(R.layout.main,null); try { //create an instance of the DefaultHandler class //**ALTER THIS FOR YOUR CLASS NAME** DataHandler handler = new DataHandler(getApplicationContext()); //get the string list by calling the public method ArrayList&lt;TextView&gt; newViews = handler.getData(); //convert to an array Object[] products = newViews.toArray(); //loop through the items, creating a View item for each for(int i=0; i&lt;products.length; i++) { //add the next View in the list mainLayout.addView((TextView)products[i]); } } catch(Exception pce) { Log.e("AndroidTestsActivity", "PCE "+pce.getMessage()); } setContentView(mainLayout); } } </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload