load xml file in android at dynamic time - android

I am trying to parse some text at run time. I am new to android any help would be appreciated.
I wanted to parse text and put that in a image view. I am doing this:
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL(
"http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Get result from MyXMLHandler SitlesList Object */
sitesList = MyXMLHandler.sitesList;
/** Assign textview array lenght by arraylist size */
name = new TextView[sitesList.getName().size()];
website = new TextView[sitesList.getName().size()];
category = new TextView[sitesList.getName().size()];
/** Set the result text in textview and add it to layout */
for (int i = 0; i < sitesList.getName().size(); i++) {
name[i] = new TextView(this);
name[i].setText("Name = "+sitesList.getName().get(i));
website[i] = new TextView(this);
website[i].setText("Website = "+sitesList.getWebsite().get(i));
category[i] = new TextView(this);
category[i].setText("Website Category = "+sitesList.getCategory().get(i));
layout.addView(name[i]);
layout.addView(website[i]);
layout.addView(category[i]);
}

Related

Parsing XML node having different attribute value in Android

I have following XML data,
<RESPONSE>
<param name="Type">NBFundTransfer</param>
<param name="Id">3213</param>
<param name="Token">26&ffr$5%877</param>
<param name="Stage">1</param>
</RESPONSE>
I want to fetch the node by its name.I use following method to fetch data,
NodeList nl = doc.getElementsByTagName("RESPONSE");
String[] Agreement = new String[nl.getLength()];
for (int i = 0; i < nl.getLength(); i++) {
Node item = nl.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE) {
Element ielem = (Element) item;
NodeList id = ielem.getElementsByTagName("param");
Data[i] = id.item(0).getChildNodes().item(0).getNodeValue();
}
}
But the problem is, Iam only getting the data "NBFundTransfer" from first node which is names as "Type" .I want to fetch the data from all other nodes(Id,Token etc).Please someone help me to get a solution.Thank you..
public class XMLParsingExample extends Activity {
/** Create Object For SiteList Class */
SitesList sitesList = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
/** Create a new textview array to display the results */
TextView name[];
TextView website[];
TextView category[];
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL(
"http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Get result from MyXMLHandler SitlesList Object */
sitesList = MyXMLHandler.sitesList;
/** Assign textview array lenght by arraylist size */
name = new TextView[sitesList.getName().size()];
website = new TextView[sitesList.getName().size()];
category = new TextView[sitesList.getName().size()];
/** Set the result text in textview and add it to layout */
for (int i = 0; i < sitesList.getName().size(); i++) {
name[i] = new TextView(this);
name[i].setText("Name = "+sitesList.getName().get(i));
website[i] = new TextView(this);
website[i].setText("Website = "+sitesList.getWebsite().get(i));
category[i] = new TextView(this);
category[i].setText("Website Category = "+sitesList.getCategory().get(i));
layout.addView(name[i]);
layout.addView(website[i]);
layout.addView(category[i]);
}
/** Set the layout view to display */
setContentView(layout);
}
}

I cannot read XML data from a particular URL

I've been stuck for a while now trying to read XML data from a particular URL. I got a number of material regarding pulling XML Data with SAX and DOM but it doesnt work with my particular URL
Here is the code i tried. This one uses SAX and works perfectly with
TextView name[];
TextView website[];
TextView category[];
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL(
"http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Get result from MyXMLHandler SitlesList Object */
sitesList = MyXMLHandler.sitesList;
/** Assign textview array lenght by arraylist size */
name = new TextView[sitesList.getName().size()];
website = new TextView[sitesList.getName().size()];
category = new TextView[sitesList.getName().size()];
/** Set the result text in textview and add it to layout */
for (int i = 0; i < sitesList.getName().size(); i++) {
name[i] = new TextView(this);
name[i].setText("Name = "+sitesList.getName().get(i));
website[i] = new TextView(this);
website[i].setText("Website = "+sitesList.getWebsite().get(i));
category[i] = new TextView(this);
category[i].setText("Website Category = "+sitesList.getCategory().get(i));
layout.addView(name[i]);
layout.addView(website[i]);
layout.addView(category[i]);
}
also included in the code is the XMLHandler class that extends DefaultHandler
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = true;
if (localName.equals("maintag"))
{
/** Start */
sitesList = new SitesList();
} else if (localName.equals("website")) {
/** Get attribute value */
String attr = attributes.getValue("category");
sitesList.setCategory(attr);
}
}
/** Called when tag closing ( ex:- <name>AndroidPeople</name>
* -- </name> )*/
#Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentElement = false;
/** set value */
if (localName.equalsIgnoreCase("name"))
sitesList.setName(currentValue);
else if (localName.equalsIgnoreCase("website"))
sitesList.setWebsite(currentValue);
}
But when i change the url, it returns a XML Pasing Exception = java.net.UnknownHostException: giantstrides.crs.gov.ng and an Unable to start activity error (using Tomcat)
TextView title[];
TextView description[];
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
URL sourceUrl = new URL(
"http://giantstrides.crs.gov.ng/index.php?option=com_ninjarsssyndicator&feed_id=22&format=raw");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Get result from MyXMLHandler SitlesList Object */
sitesList = MyXMLHandler.sitesList;
/** Assign textview array lenght by arraylist size */
title = new TextView[sitesList.getTitle().size()];
description = new TextView[sitesList.getTitle().size()];
//category = new TextView[sitesList.getName().size()];
/** Set the result text in textview and add it to layout */
//for (int i = 0; i < sitesList.getTitle().size(); i++) {
for (int i = 0; i < 2; i++) {
title[i] = new TextView(this);
title[i].setText("Title = "+sitesList.getTitle().get(i));
description[i] = new TextView(this);
description[i].setText("Description = "+sitesList.getDescription().get(i));
//category[i] = new TextView(this);
//category[i].setText("Website Category = "+sitesList.getCategory().get(i));
layout.addView(title[i]);
layout.addView(description[i]);
There is no problem with the URL because it loads the feeds on a browser but i cant get the information i require. Please, i need help, anybody, please..
Gozie

store data as xml from link in android

I have a link from that link:
http://api.maxxiscentral.com/maxxiscentral/GetTyrePatterns
from this link i want to catch the data as string and from that string i want to create its xml file.
package com.androidpeople.xml.parsing;
import java.net.URL;
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.widget.LinearLayout;
import android.widget.TextView;
public class XMLParsingExample extends Activity {
/** Create Object For SiteList Class */
SitesList sitesList = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
/** Create a new textview array to display the results */
TextView name[];
TextView website[];
TextView category[];
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL(
"http://api.maxxiscentral.com/maxxiscentral/GetTyrePatterns");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Get result from MyXMLHandler SitlesList Object */
sitesList = MyXMLHandler.sitesList;
/** Assign textview array lenght by arraylist size */
name = new TextView[sitesList.getName().size()];
website = new TextView[sitesList.getName().size()];
category = new TextView[sitesList.getName().size()];
/** Set the result text in textview and add it to layout */
for (int i = 0; i < sitesList.getName().size(); i++) {
name[i] = new TextView(this);
name[i].setText("Name = "+sitesList.getName().get(i));
website[i] = new TextView(this);
website[i].setText("Website = "+sitesList.getWebsite().get(i));
category[i] = new TextView(this);
category[i].setText("Website Category = "+sitesList.getCategory().get(i));
layout.addView(name[i]);
layout.addView(website[i]);
layout.addView(category[i]);
}
/** Set the layout view to display */
setContentView(layout);
}
}
i have this code to convert string into xml but,
this code run in java but not in android. why?
and how can i get the data as from that link.
thanks in advanced.
get from here
get it
get it
get it
get it

Android: how to get textview position from table

In list view the position of particular row can easily be determined through array adapter and then by using onlistitemclick method we can have that value in toast message
but how can we get position or id of any particular text view(when it is clicked) from table view where table rows are generating dynamically through xml parser..
where column names are id, name and class.
id is auto incremented..
pls suggest.
the code is
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
URL sourceUrl = new URL("http://ip-address/test/player.xml");
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
DataList dataList = XMLHandler.DataList;
name = new TextView[dataList.getName().size()];
class = new TextView[dataList.getName().size()];
/** Set the result text in textview and add it to layout */
for (int i = 0; i < DataList.getName().size(); i++) {
TableRow tr = new TableRow(this);
tr.setId(100+i);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to house the name of the province
name[i]= new TextView(this);
name[i].setId(200+i);
name[i].setText(dataList.getName().get(i));
name[i].setTextColor(Color.WHITE);
name[i].setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(name[i]);
class[i] = new TextView(this);
class[i].setId(i);
class[i].setText(dataList.getClass().get(i));
class[i].setTextColor(Color.WHITE);
class[i].setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(class[i]);
t1.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
Invoke ViewGroup method:
public int indexOfChild (View child)
on TableLayout, where child is TableRow. TableRow should be accesible through
public final ViewParent getParent ()
of View inside TableRow.
TableRow tableRow = (TableRow)mTextView.getParent();
int index = -1;
if(parent != null){
index = mTable.indexOfChild(tableRow);
}
if(index < 0){
throw new IllegalStateException("TextView not found");
}
Hope that help, but I haven't tested it.

Layout Parsing Xml

i'm make example parsing xml from
http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser/
in this example, layout using an array textview..
I tried to change the layout into a ListView, but is not working..
I get error in adapter.. can anyone help me?
change your XMLParsingExample.java class
public class XMLParsingExample extends Activity {
SitesList sitesList = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
ListView listVw = new ListView(this);
ArrayList<String> listItm = new ArrayList<String>();
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
URL sourceUrl = new URL(
"http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
sitesList = MyXMLHandler.sitesList;
for (int i = 0; i < sitesList.getName().size(); i++) {
listItm.add("Name = " + sitesList.getName().get(i));
listItm.add("Website = " + sitesList.getWebsite().get(i));
listItm.add("Website Category = " + sitesList.getCategory().get(i));
}
listVw.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listItm));
layout.addView(listVw);
setContentView(layout);
}
}
run this code and get all parsing value in listview.

Categories

Resources