I have a xml data like below:
<toplevel>
<CompleteSuggestion>
<suggestion data="madonna"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="madonna like a prayer"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="madonna like a virgin"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="madonna vogue"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="madonna la isla bonita"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="madonna frozen"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="madonna holiday"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="madonna music"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="madonna gimme all your love"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="madonna celebration"/>
</CompleteSuggestion>
</toplevel>
I am parsing this by using a XMLParser class below:
public class XMLParser {
// constructor
public XMLParser() {
}
/**
* Getting XML from URL making HTTP request
* #param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* #param XML string
* */
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* #param elem element
*/
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
Log.e("in element","element");
if (elem.hasChildNodes()){
Log.e("in child","child nodes");
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
Log.e("in for","for loop");
child = elem.getFirstChild();
if( child.getNodeType() == Node.TEXT_NODE ){
Log.e("in if condition","if cond");
return elem.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* #param Element node
* #param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
Here is my Activity code:
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Log.e("string xml","hello"+xml);
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
Log.e("no. of items",""+i);
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
Log.e("names","hi"+parser.getValue(e, KEY_NAME));
menuItems.add(map);
}
I observed that my getElementValue( Node elem ) in XmLParser is always returning empty String. I think I need to change this method. I tried by changing some of the statements but didn't find any solution.
Related
I am making a quiz application. For the MCQ questions I have an XML, which I have parsed.
This is the XML:
<quiz>
<mchoice>
<question>Not a team sport for sure</question>
<id>1</id>
<option1>Cricket</option1>
<option2>Tennis</option2>
<option3>Rugby</option3>
<option4>Soccer</option4>
<answer>Tennis</answer>
</mchoice>
<mchoice>
<question>I am the biggest planet in the Solar System</question>
<id>2</id>
<option1>Saturn</option1>
<option2>Jupiter</option2>
<option3>Neptune</option3>
<option4>Pluto</option4>
<answer>Jupiter</answer>
</mchoice>
<mchoice>
<question>I am the closest star to the earth</question>
<id>3</id>
<option1>Milky way</option1>
<option2>Moon</option2>
<option3>Sun</option3>
<option4>North Star</option4>
<answer>Sun</answer>
</mchoice>
<mchoice>
<question>A number which is not prime</question>
<id>4</id>
<option1>31</option1>
<option2>61</option2>
<option3>71</option3>
<option4>91</option4>
<answer>91</answer>
</mchoice>
<mchoice>
<question>Which is correct?</question>
<id>5</id>
<option1>Foreine</option1>
<option2>Fariegn</option2>
<option3>Foreig</option3>
<option4>Foreign</option4>
<answer>Foreign</answer>
</mchoice>
</quiz>
This is the XML Parser class which I have used:
public class XMLParser {
// constructor
public XMLParser() {
}
/**
* Getting XML from URL making HTTP request
* #param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* #param XML string
* */
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* #param elem element
*/
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* #param Element node
* #param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
This is the activity from which I am calling the XML parser Class and adding the parsed data into an Arraylist having hashMap inside it:
// All static variables
static final String URL = "http://gujaratimandal.org/data.xml";
// XML node keys
static final String KEY_MCHOICE = "mchoice"; // parent node
static final String KEY_QUESTION = "question";
static final String KEY_ID = "id";
static final String KEY_OPTION1 = "option1";
static final String KEY_OPTION2 = "option2";
static final String KEY_OPTION3 = "option3";
static final String KEY_OPTION4 = "option4";
static final String KEY_ANSWER = "answer";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_MCHOICE);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_MCHOICE, parser.getValue(e, KEY_MCHOICE));
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_QUESTION, parser.getValue(e, KEY_QUESTION));
//map.put(KEY_QUESTION, parser.getValue(e, KEY_QUESTION));
map.put(KEY_OPTION1, parser.getValue(e, KEY_OPTION1));
map.put(KEY_OPTION2, parser.getValue(e, KEY_OPTION2));
map.put(KEY_OPTION3, parser.getValue(e, KEY_OPTION3));
map.put(KEY_OPTION4, parser.getValue(e, KEY_OPTION4));
map.put(KEY_ANSWER, parser.getValue(e, KEY_ANSWER));
// adding HashList to ArrayList
menuItems.add(map);
for (HashMap.Entry<String, String> entry : map.entrySet()) {
String display_id=entry.getKey();
String display_question = entry.getValue();
makeAToast( ""+display_id+":"+ display_question);
}
}
makeAToast(""+menuItems.size());
}
public void makeAToast(String str) {
Toast toast = Toast.makeText(this,str, Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.setDuration(1000000);
toast.show();
}
The problem is that, the data is getting retrieved but not in the desired way.
I want to retrieve the data in the following format:
Such that I can populate these TextViews with the data from every question:
What should I do?
make one string in you public class activity
String keyoption,keyoption2,name;
and
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
keyoption = parser.getValue(e, KEY_OPTION1);
keyoption2 = parser.getValue(e, KEY_OPTION2);
map.put(KEY_OPTION1, keyoption);
map.put(KEY_OPTION2, keyoption2);
}
name.setText(keyoption);
Create an xml file for the layout like that you mentioned.
Its simple.
Create a LinearLayout as the root of your xml. (this is anyways the default) android:gravity="center"
Looks like you need 5 textViews inside the LinearLayout. One for the question. 4 for your options. all of them with android:gravity="center_horizontal"
One EditText in the end for the answer.
<NewDataSet>
<Table>
<Cust_Name>Dev</Cust_Name>
<Order_No>OR00000009</Order_No>
<Freight_Rate>68000</Freight_Rate>
<Station_Name>Chennai</Station_Name>
<Station_Name1>Kolkatta</Station_Name1>
</Table>
<Table>
<Cust_Name>FSL</Cust_Name>
<Order_No>OR00000010</Order_No>
<Freight_Rate>6000</Freight_Rate>
<Station_Name>Mumbai</Station_Name>
<Station_Name1>Pune</Station_Name1>
</Table>
<Table>
<Cust_Name>FSL</Cust_Name>
<Order_No>OR00000011</Order_No>
<Freight_Rate>7000</Freight_Rate>
<Station_Name>Mumbai</Station_Name>
<Station_Name1>Pune</Station_Name1>
</Table>
<Table>
<Cust_Name>FSL</Cust_Name>
<Order_No>OR00000012</Order_No>
<Freight_Rate>7000</Freight_Rate>
<Station_Name>Chennai</Station_Name>
<Station_Name1>Bangalore</Station_Name1>
</Table>
</NewDataSet>
How to arrange Cust_Name,Order_No,Freight_Rate etc,.. in a row of a gridview in android?
This is the exact link for the above XML file
"http://54.251.60.177/StudentWebService/StudentDetail.asmx/GetTMSOrders"
Sources for reference
public class GridSample extends Activity
{
// All static variables
static final String URL = "http://54.251.60.177/StudentWebService/StudentDetail.asmx/GetTMSOrders";
// XML node keys
static final String KEY_TABLE = "Table"; // parent node
static final String KEY_CUST = "Cust_Name";
static final String KEY_ORDER = "Order_No";
static final String KEY_FREIGHT = "Freight_Rate";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_TABLE);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++)
{
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_CUST, parser.getValue(e, KEY_CUST));
map.put(KEY_ORDER, parser.getValue(e, KEY_ORDER));
map.put(KEY_FREIGHT,parser.getValue(e, KEY_FREIGHT));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.grid_item,new String[] { KEY_CUST, KEY_ORDER, KEY_FREIGHT },
new int[] { R.id.name, R.id.desciption, R.id.cost });
GridView gv = (GridView)findViewById(R.id.gridVi);
gv.setAdapter(adapter);
gv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_CUST, name);
in.putExtra(KEY_ORDER, cost);
in.putExtra(KEY_FREIGHT, description);
startActivity(in);
}
});
}}
SingleMenuItemActivity.java
public class SingleMenuItemActivity extends Activity {
// XML node keys
static final String KEY_CUST = "Cust_Name";
static final String KEY_ORDER = "Order_No";
static final String KEY_FREIGHT = "Freight_Rate";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_grid_item);
// getting intent data
Intent in = getIntent();
// Get XML values from previous intent
String cust = in.getStringExtra(KEY_CUST);
String order = in.getStringExtra(KEY_ORDER);
String freight = in.getStringExtra(KEY_FREIGHT);
// Displaying all values on the screen
TextView lblcust = (TextView) findViewById(R.id.cust_label);
TextView lblorder = (TextView) findViewById(R.id.Order_label);
TextView lblfreight = (TextView) findViewById(R.id.freight_label);
lblcust.setText(cust);
lblorder.setText(order);
lblfreight.setText(freight);
}}
XMLParser.java
public class XMLParser
{
// constructor
public XMLParser()
{
}
/**
* Getting XML from URL making HTTP request
* #param url string
* */
public String getXmlFromUrl(String url)
{
String xml = null;
try
{
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* #param XML string
* */
public Document getDomElement(String xml)
{
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e)
{
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e)
{
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e)
{
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* #param elem element
*/
public final String getElementValue( Node elem )
{
Node child;
if( elem != null)
{
if (elem.hasChildNodes())
{
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() )
{
if( child.getNodeType() == Node.TEXT_NODE )
{
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* #param Element node
* #param key string
* */
public String getValue(Element item, String str)
{
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}}
Thanks a lot!..
you can set your xml file layout in horizontal way and try parsing xml in that gridview
I've seen quite a few posts about this, but actually I did not get any to work. I am building a simple TV guide android application. I simply Use RSS from a tvprofil.net to show whats on TV today. The problem is, I do not know how to Parse CDATA in XML. I am using some standard parser with DOM... at least I think so..
This is a bit of XML:
.
.
.
<item>
<title>RTS1 14.08.2012</title>
<pubDate>Tue, 14 Aug 2012 06:00:00</pubDate>
<content:encoded><![CDATA[06:00 Vesti<br>06:05 Jutarnji program<br>08:00 Dnevnik
<br>8:15 Jutarnji Program<br>09:00 Vesti ... ]]></content:encoded>
</item>
.
.
.
now, this is my main app:
public class Main extends ListActivity {
// All static variables
static final String URL = "http://tvprofil.net/rss/feed/channel-group-2.xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_NAME = "title";
static final String KEY_DATE = "pubDate";
static final String KEY_DESC = "content:encoded";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<HashMap<String,String>> menuItems = new ArrayList<HashMap<String,String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); //get XML
Document doc = parser.getDomElement(xml); // get DOM elem.
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
//loop
for (int i=0; i< nl.getLength(); i++){
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
//add to map
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
// hash => list
menuItems.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.list_item,
new String[]{KEY_NAME, KEY_DESC, KEY_DATE}, new int[]{
R.id.name, R.id.description, R.id.date
});
setListAdapter(adapter);
//singleView
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String name = ((TextView)view.findViewById(R.id.name)).getText().toString();
String date = ((TextView)view.findViewById(R.id.date)).getText().toString();
String description = ((TextView)view.findViewById(R.id.description)).getText().toString();
//intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_NAME, name);
in.putExtra(KEY_DATE, date);
in.putExtra(KEY_DESC, description);
startActivity(in);
}
});
}
}
and the parser class:
public class XMLParser {
// constructor
public XMLParser() {
}
/**
* Getting XML from URL making HTTP request
* #param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* #param XML string
* */
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* #param elem element
*/
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* #param Element node
* #param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
there is one more class for Single menu item.. but I think it's irrelevant in this case.
Now, I'd just like to see no HTML tags after parsing it and dealing with CDATA...
Anyone got idea about this one?
Add this
dbf.setCoalescing(true);
where dbf is
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
First add this method
public String getCharacterDataFromElement(Element e, String str) {
NodeList n = e.getElementsByTagName(str);
Element e1=(Element) n.item(0);
Node child = e1.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "";
}
Call the above method as so-
map.put(KEY_DESC, parser.getCharacterDataFromElement(e, KEY_DESC));
This should get you the CDATA in String format. HOpe this helps
getTextContent.
This attribute returns the text content of this node and its
descendants
getNodeValue()
The value of this node, depending on its type;
usually you shouled use getTextContent.
zg_spring's answer worked perfectly for me when I needed to extract image URLs from CDATA in a set of "description" xml elements:
//Get the content of all "item" elements
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(xml)));
NodeList nlDetails = doc.getElementsByTagName("item");
//Loop through elements and extract content of "description" elements
for(int k = 0; k < numDetails; k++) {
Element nDetails = (Element)nlDetails.item(k);
NodeList nlCoverURL = nDetails.getElementsByTagName("description");
Node nCoverURL = nlCoverURL.item(0);
String sCoverURL = nCoverURL.getTextContent();
//Isolate the relevant part of the String and load it into an ArrayList
String[] descriptionContent = sCoverURL.split("\"");
String s = descriptionContent[11]
alImages.add(s);
}
Ok, about this specific situation, I have large XML file that should be parsed and shown in Android, and the part of it is here:
.
.
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/:
.
.
<item>
<title>RTS1</title>
<pubDate>Tue, 14, Aug 2012</pubDate>
<content:encoded><![CDATA[6:00 Vesti<br>06:05 Jutarnji program<br>08:15 Jutarnji dnevnik
<br>09:00 Kamiondzije .................. 23:15 Film<br>]]>
</content:encoded>
<item>
In my main, app I have done this:
public class Main extends ListActivity {
// All static variables
static final String URL = "http://tvprofil.net/rss/feed/channel-group-2.xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_NAME = "title";
static final String KEY_DATE = "pubDate";
static final String KEY_DESC = "content:encoded";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<HashMap<String,String>> menuItems = new ArrayList<HashMap<String,String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); //get XML
Document doc = parser.getDomElement(xml); // get DOM elem.
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
//loop
for (int i=0; i< nl.getLength(); i++){
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
//add to map
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
// hash => list
menuItems.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.list_item,
new String[]{KEY_NAME, KEY_DESC, KEY_DATE}, new int[]{
R.id.name, R.id.description, R.id.date
});
setListAdapter(adapter);
//singleView
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String name = ((TextView)view.findViewById(R.id.name)).getText().toString();
String date = ((TextView)view.findViewById(R.id.date)).getText().toString();
String description = ((TextView)view.findViewById(R.id.description)).getText().toString();
//intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_NAME, name);
in.putExtra(KEY_DATE, date);
in.putExtra(KEY_DESC, description);
startActivity(in);
}
});
}
}
please, look closely at:
static final String KEY_DESC = "content:encoded";
I've tried with:
static final String KEY_DESC = "encoded";
and even with:
static final String KEY_DESC = "http://purl.org/rss/1.0/modules/content/encoded";
but nothing works.. doesn't show anything nor in Log.w when I set it nor on phone...
I'll even post parser class, I don't want any confusion:
public class XMLParser {
// constructor
public XMLParser() {
}
/**
* Getting XML from URL making HTTP request
* #param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* #param XML string
* */
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setCoalescing(true);
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* #param elem element
*/
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* #param Element node
* #param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
I am making a connection to the Internet using HTTP Request, parsing an XML
Everything works correctly, but I want to do is make the connection from a AsyncTask for new versions of android sdk.
Usually should not give problems, but I am following a tutorial and after trying many options and see similar threads on this website, I can not apply it correctly.
This is the code I'm using
Activity 1:
public class CustomizedListView extends Activity {
// All static variables
static final String URL = "http://api.androidhive.info/music/music.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
private ArrayList<HashMap<String, String>> data;
ListView list;
LazyAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_SONG);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
}
}
And this is the second activity:
public class XMLParser {
// constructor
public XMLParser() {
}
/**
* Getting XML from URL making HTTP request
* #param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* #param XML string
* */
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* #param elem element
*/
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* #param Element node
* #param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
...I understand that it must be encapsulated in a AsycTask is:
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
...But to be using strings of two activitys, I can not find a way to do
Thank you very much for the help
Regards
use this type of class as subclass in your activity:
private class YourDownload extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
/*
make connection & download XML here,
use your XML parser class object to parse the xml from here
create ArrayList & etc. from here...
*/
return null;
}
#Override
protected void onPostExecute(Void result) {
// postexecute logic
super.onPostExecute(result);
}
#Override
protected void onPreExecute() {
// pre execute logic
super.onPreExecute();
}
}
use YourDownload from onCreate() as new YourDownload().execute();
Probably this will help you..