Thursday, April 25, 2013

[android help] Sax parser with string xml + malformation error


This will work as expected


Type.java



package com.example.test;

public class Type
{
private String lory;
private String car;

public String getLory()
{
return lory;
}

public void setLory(String lory)
{
this.lory = lory;
}

public String getCar()
{
return car;
}

public void setCar(String car)
{
this.car = car;
}

@Override
public String toString()
{
return "Lory : " + this.lory + "\nCar : " + this.car;
}

public String getDetails()
{
String result = "Lory : " + this.lory + "\nCar : " + this.car;
return result;
}
}


SAXXMLHandler.java



package com.example.test;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXXMLHandler extends DefaultHandler
{
private List types;
private String tempVal;
private Type tempType;

public SAXXMLHandler()
{
types = new ArrayList();
}

public List getTypes()
{
return types;
}

// Event Handlers
@Override
public void startElement(String uri, String localName, String qualifiedName, Attributes attributes) throws SAXException
{
// reset
tempVal = "";
if ( qualifiedName.equalsIgnoreCase("data") )
{
// create a new instance of type
tempType = new Type();
}
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException
{
tempVal = new String(ch, start, length);
}

@Override
public void endElement(String uri, String localName, String qualifiedName) throws SAXException
{
if ( qualifiedName.equalsIgnoreCase("type") )
{
// add it to the list and create new instance
types.add(tempType);
tempType = new Type();
}
else if ( qualifiedName.equalsIgnoreCase("lory") )
{
tempType.setLory(tempVal);
}
else if ( qualifiedName.equalsIgnoreCase("car") )
{
tempType.setCar(tempVal);
}
}
}


SAXXMLParser.java



package com.example.test;

import java.io.InputStream;
import java.util.List;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.util.Log;

public class SAXXMLParser
{
public static List parse(InputStream is)
{
List types = null;
try
{
// create a XMLReader from SAXParser
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
// create a SAXXMLHandler
SAXXMLHandler saxHandler = new SAXXMLHandler();
// store handler in XMLReader
xmlReader.setContentHandler(saxHandler);
// the process starts
xmlReader.parse(new InputSource(is));
// get the `Type list`
types = saxHandler.getTypes();
}
catch ( Exception ex )
{
Log.d("XML", "SAXXMLParser: parse() failed");
}
// return Type list
return types;
}
}


SAXParserActivity.java



package com.example.test;

import java.io.ByteArrayInputStream;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class SAXParserActivity extends Activity implements OnClickListener
{
Button button;
List types = null;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}

@Override
public void onClick(View v)
{
String xml = " vroom crack doom chack ";
types = SAXXMLParser.parse(new ByteArrayInputStream(xml.getBytes()));
Log.d("SSDDSD", "Length : " + "" + types.size());
for ( Type type : types )
{
Log.d("SAXParserActivity", type.toString());
Toast.makeText(getApplicationContext(), type.toString(), Toast.LENGTH_SHORT).show();
}
}
}


main.xml




android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Parse XML Using SAX" />



You can see the output in both LogCat and Toast.



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...