Sample XML data used in this example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | < Items > < ItemData > < ItemNumber >ABC</ ItemNumber > < Description >ABC Item Description</ Description > < Price >9.95</ Price > < Weight >10.00</ Weight > </ ItemData > < ItemData > < ItemNumber >XYZ</ ItemNumber > < Description >XYZ Item Description</ Description > < Price >19.95</ Price > < Weight >22.22</ Weight > </ ItemData > </ Items > |
Source code for ItemMaster.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.as400samplecode; public class ItemMaster { String itemNumber = null ; String description = null ; String price = null ; double weight = 0 ; public String getItemNumber() { return itemNumber; } public void setItemNumber(String itemNumber) { this .itemNumber = itemNumber; } public String getDescription() { return description; } public void setDescription(String description) { this .description = description; } public String getPrice() { return price; } public void setPrice(String price) { this .price = price; } public double getWeight() { return weight; } public void setWeight( double weight) { this .weight = weight; } } |
Source code for ItemXMLHandler.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | package com.as400samplecode; import java.util.ArrayList; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class ItemXMLHandler extends DefaultHandler { Boolean currentElement = false ; String currentValue = "" ; ItemMaster item = null ; private ArrayList<ItemMaster> itemsList = new ArrayList<ItemMaster>(); public ArrayList<ItemMaster> getItemsList() { return itemsList; } // Called when tag starts @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { currentElement = true ; currentValue = "" ; if (localName.equals( "ItemData" )) { item = new ItemMaster(); } } // Called when tag closing @Override public void endElement(String uri, String localName, String qName) throws SAXException { currentElement = false ; /** set value */ if (localName.equalsIgnoreCase( "ItemNumber" )) item.setItemNumber(currentValue); else if (localName.equalsIgnoreCase( "Description" )) item.setDescription(currentValue); else if (localName.equalsIgnoreCase( "Price" )) item.setPrice(currentValue); else if (localName.equalsIgnoreCase( "Weight" )) item.setWeight(Double.parseDouble(currentValue)); else if (localName.equalsIgnoreCase( "ItemData" )) itemsList.add(item); } // Called to get tag characters @Override public void characters( char [] ch, int start, int length) throws SAXException { if (currentElement) { currentValue = currentValue + new String(ch, start, length); } } } |
Source code for AndroidParseXMLActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | package com.as400samplecode; import java.io.StringReader; import java.util.ArrayList; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class AndroidParseXMLActivity extends Activity { private EditText xmlInput; private TextView xmlOutput; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); xmlInput = (EditText) findViewById(R.id.xmlInput); xmlOutput = (TextView) findViewById(R.id.xmlOutput); Button parseMyXML = (Button) findViewById(R.id.parse); parseMyXML.setOnClickListener( new View.OnClickListener() { public void onClick(View view) { parseXML(); } }); } private void parseXML() { String parsedData = "" ; try { Log.w( "AndroidParseXMLActivity" , "Start" ); /** Handling XML */ SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); ItemXMLHandler myXMLHandler = new ItemXMLHandler(); xr.setContentHandler(myXMLHandler); InputSource inStream = new InputSource(); Log.w( "AndroidParseXMLActivity" , "Parse1" ); inStream.setCharacterStream( new StringReader(xmlInput.getText().toString())); Log.w( "AndroidParseXMLActivity" , "Parse2" ); xr.parse(inStream); Log.w( "AndroidParseXMLActivity" , "Parse3" ); ArrayList<ItemMaster> itemsList = myXMLHandler.getItemsList(); for ( int i= 0 ;i<itemsList.size();i++){ ItemMaster item = itemsList.get(i); parsedData = parsedData + "----->\n" ; parsedData = parsedData + "Item Number: " + item.getItemNumber() + "\n" ; parsedData = parsedData + "Description: " + item.getDescription() + "\n" ; parsedData = parsedData + "Price: " + item.getPrice() + "\n" ; parsedData = parsedData + "Weight: " + item.getWeight() + "\n" ; } Log.w( "AndroidParseXMLActivity" , "Done" ); } catch (Exception e) { Log.w( "AndroidParseXMLActivity" ,e ); } xmlOutput.setText(parsedData); } } |
Source code for main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <? xml version = "1.0" encoding = "utf-8" ?> android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > < TextView android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "@string/hello" android:textStyle = "bold" android:textSize = "25sp" android:paddingBottom = "15dp" /> < EditText android:layout_height = "wrap_content" android:layout_width = "match_parent" android:id = "@+id/xmlInput" android:lines = "10" > < requestFocus /> </ EditText > < Button android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:id = "@+id/parse" android:text = "Parse Now" /> < TextView android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:textAppearance = "?android:attr/textAppearanceLarge" android:id = "@+id/textView1" android:text = "Parsed XML Data" android:paddingBottom = "15dp" android:textStyle = "bold" /> < ScrollView android:layout_height = "wrap_content" android:layout_width = "match_parent" android:id = "@+id/scrollView1" > < LinearLayout android:id = "@+id/linearLayout1" android:layout_width = "match_parent" android:layout_height = "match_parent" > < TextView android:layout_height = "wrap_content" android:textAppearance = "?android:attr/textAppearanceMedium" android:id = "@+id/xmlOutput" android:layout_width = "match_parent" /> </ LinearLayout > </ ScrollView > </ LinearLayout > |
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.