Android XML parsing Not getting out put - android

How to parse xml like this (A XML which is having an inner tag).I am not getting simpleChoice Node when I try to print it it showing always blank can any one help me.
<itemBody>
<p>Look at the text in the picture.</p>
<p>
<img src="images/sign.png" alt="NEVER LEAVE LUGGAGE UNATTENDED"/>
</p>
<choiceInteraction responseIdentifier="RESPONSE" shuffle="false" maxChoices="1">
<prompt>What does it say?</prompt>
<simpleChoice identifier="ChoiceA">You must stay with your luggage at all times.</simpleChoice>
<simpleChoice identifier="ChoiceB">Do not let someone else look after your luggage.</simpleChoice>
<simpleChoice identifier="ChoiceC">Remember your luggage when you leave.</simpleChoice>
</choiceInteraction>
</itemBody>
What I had tried -
NodeList nodeList = doc.getElementsByTagName("itemBody");
XMLParser parser = new XMLParser();
for (int i = 0; i < nodeList.getLength(); i++)
{
Element e = (Element) nodeList.item(i);
Log.i("TAG","<p>: " + parser.getValue(e, "p"));
//Log.i("TAG","Count: " + parser.getValue(e, "choiceInteraction"));
//Log.i("TAG","Count: " + parser.getValue(e, "p"));
}
NodeList nodeChoiceInteractionList = doc.getElementsByTagName("choiceInteraction");
for (int j = 0; j < nodeChoiceInteractionList.getLength(); j++)
{
Element eChoiceInteractionList = (Element) nodeChoiceInteractionList.item(j);
Log.i("TAG","<prompt>: " + parser.getValue(eChoiceInteractionList, "prompt"));
}
NodeList simpleChoiceList = doc.getElementsByTagName("simpleChoice");
for (int j = 0; j < simpleChoiceList.getLength(); j++)
{
Element eSimpleChoice = (Element) simpleChoiceList.item(j);
Log.i("TAG","<simpleChoice>: " + parser.getValue(eSimpleChoice, "simpleChoice"));
}

You are looking for like a child of ItemBody, so you need to iterate over the nodeChoiceInteractionList looking for the simpleChoice node, like you do with the prompt node.
Try also to print in your log the times this codes iterates to give you an idea
for (int j = 0; j < nodeChoiceInteractionList.getLength(); j++)
{
Element eChoiceInteractionList = (Element) nodeChoiceInteractionList.item(j);
Log.i("TAG","<prompt>: " + parser.getValue(eChoiceInteractionList, "prompt"));
>>Log.i("TAG","<simpleChoice>: " + parser.getValue(eSimpleChoice, "simpleChoice"));
>>Log.d("Iterate Number",j);
}

Related

XML Parsing For News Application

I have create news application rss feed on xml parsing for collage project
this is xml http://goo.gl/X8tIr1 so any one guide me for build that application or give code to help for collage project
Can Anyone give me the whole code
Go through Creating-a-simple-rss-application-in-android. This should get you started.
I'm giving you how to XML PArsing works in doInBackground. Try like this :
DOMParser dParser = new DOMParser();
String abc = dParser.getXmlFromUrl(params[0]);
Document doc = dParser.getDocElement(abc);
NodeList n1 = doc.getElementsByTagName("channel");
for (int i = 0; i < n1.getLength(); i++) {
Element e = (Element) n1.item(i);
NodeList nl1 = e.getElementsByTagName("item");
for (int j = 0; j < nl1.getLength(); j++) {
Element e1 = (Element) nl1.item(j);
NodeList ntitle = e1.getElementsByTagName(TITLE);
NodeList ndate = e1.getElementsByTagName(PUBDATE);
NodeList ndesc = e1.getElementsByTagName(DESCRIPTION);
for (int k = 0; k < n1.getLength(); k++) {
Element e2 = (Element) ntitle.item(k);
Element e3 = (Element) ndate.item(k);
Element e4 = (Element) ndesc.item(k);
Pojo pojo = new Pojo();
pojo.setTitle(getCharacterDataFromElement(e2));
pojo.setPubdate(getCharacterDataFromElement(e3));
pojo.setDescription(getCharacterDataFromElement(e4));
itemList.add(pojo);
}
}
}
return null;

Parse two elements with same name android

I'm a bit new to XML and Android development... I've encountered this issue where I need to parse an XML where the elements are the same and include that with the overall element. It's a bit hard to explain, see code below:
<tns:camera>
<tns:congestionLocations>
<tns:congestion>Free Flow</tns:congestion>
<tns:direction>Eastbound</tns:direction>
</tns:congestionLocations>
<tns:congestionLocations>
<tns:congestion>Free Flow</tns:congestion>
<tns:direction>Westbound</tns:direction>
</tns:congestionLocations>
<tns:description>Bond St looking east</tns:description>
<tns:direction>Eastbound</tns:direction>
<tns:group>SH16-North-Western</tns:group>
<tns:lat>-36.869</tns:lat>
<tns:lon>174.746</tns:lon>
<tns:name>SH16 1 Bond St</tns:name>
<tns:viewUrl>http://www.trafficnz.info/camera/view/130</tns:viewUrl>
</tns:camera>
Basically, I need to parse the overall element (tns:camera) and include the congestion locations (seperated from each other obviously), but within the same class as i will be using all of them in a listview...
How would I achieve this?
At present, I am using the Pull Parser, and parsing it into a class object
PullParser code:
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase(KEY_SITE)) {current Site
CameraSites.add(curCameraClass);
} else if (tagname.equalsIgnoreCase(KEY_DESCRIPTION)) {
curCameraClass.setDescription(curText);
}else if (tagname.equalsIgnoreCase(KEY_NAME)) {
curCameraClass.setName(curText);
}
break;
Kind Regards!
Try this..
NodeList nodeList = doc.getElementsByTagName("tns:camera");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("tns:group");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
System.out.println("tns:group : "+((Node) nameList.item(0)).getNodeValue());
Element fstElmnt1 = (Element) node;
NodeList nameList1 = fstElmnt1.getElementsByTagName("tns:viewUrl");
Element nameElement1 = (Element) nameList1.item(0);
nameList1 = nameElement1.getChildNodes();
System.out.println("tns:viewUrl : "+ ((Node) nameList1.item(0)).getNodeValue());
//same as use to all tns:description,tns:direction and tns:lat etc.,
if(node.getNodeType() == Node.ELEMENT_NODE)
{
Element e = (Element) node;
NodeList resultNodeList = e.getElementsByTagName("tns:congestionLocations");
int resultNodeListSize = resultNodeList.getLength();
for(int j = 0 ; j < resultNodeListSize ; j++ )
{
Node resultNode = resultNodeList.item(j);
if(resultNode.getNodeType() == Node.ELEMENT_NODE)
{
Element fstElmnt2 = (Element) resultNode;
NodeList nameList2 = fstElmnt2.getElementsByTagName("tns:congestion");
Element nameElement2 = (Element) nameList2.item(0);
nameList2 = nameElement2.getChildNodes();
Log.v("tns:congestion", ""+((Node) nameList2.item(0)).getNodeValue());
Element fstElmnt3 = (Element) resultNode;
NodeList nameList3 = fstElmnt3.getElementsByTagName("tns:direction");
Element nameElement3 = (Element) nameList3.item(0);
nameList3 = nameElement3.getChildNodes();
Log.v("tns:direction--", ""+((Node) nameList3.item(0)).getNodeValue());
}
}
}
}
You can you SAXParser to parse the xml. Hope the following links will be helpful:
developersite
basic tutorial

Parse XML data from url using DOM Parser

I want to parse xml file from url :
http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=cher&api_key=5d6ce941674603e4bb75cfad6cfa13b7
I want to parse following tags of the file :
<artist>
<name>Cher</name>
<image size="medium">http://userserve-ak.last.fm/serve/64/62286415.png</image>
</artist>
But i don't know how to get the value of these two tags only.
I have tried the example code from
http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/
But it does not showing to parse same tag having different attribute value.
Can anyone guide me how this is done?
Thanx in advance.
from the link you provided, I have just extract a small part :
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName("artist");
// looping through all item nodes <artist>
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
String name = parser.getValue(e, "name"));
String image = parser.getValue(e, "image"));
//if you want the artist 'Cher' sigh ;)
if (name.equals("Cher")){
//do whatever you want
}
}
Thankx. I solved my problem from this url :
Getting element using attribute
if(str.equals("image"))
{
n = item.getElementsByTagName(str);
for (int i = 0; i < n.getLength(); i++) {
Node subNode = n.item(i);
if (subNode.hasAttributes()) {
NamedNodeMap nnm = subNode.getAttributes();
for (int j = 0; j < nnm.getLength(); j++) {
Node attrNode = nnm.item(j);
if (attrNode.getNodeType() == Node.ATTRIBUTE_NODE) {
Attr attribute = (Attr) attrNode;
if( attribute.getValue().equals("medium"))
{
return this.getElementValue(n.item(i));
}
}
}
}
}
}

Android - How to set value of ArrayList to TextView

i have a problem here.
I've ArrayList from the result of my parser class. then i wanna put that value (value from ArrayList) to TextView.
here is the work i've done till now.
I create TextView on my main.xml
<TextView
android:id="#+id/deskripsi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
and here at my onCreate Method, i initialized the Text View
desk = (TextView)findViewById(R.id.deskripsi);
and then i tried to parser the KML document from google map and at the Node Placemark i put its value to ArrayList, here my code
ArrayList<String> pathName = new ArrayList<String>();
Object[] objPlace;
//Parser Node Placemark
NodeList nlPlace = doc.getElementsByTagName("Placemark");
for(int p = 0; p < nlPlace.getLength(); p++){
Node rootPlace = nlPlace.item(p);
NodeList itemPlace = rootPlace.getChildNodes();
for(int y = 0; y < itemPlace.getLength(); y++){
Node placeStringNode = itemPlace.item(y);
NodeList place = placeStringNode.getChildNodes();
//valueName = nameList.item(0).getNodeValue().toString() + "+";
pathName.add(place.item(0).getNodeValue());
}
}
objPlace = pathName.toArray();
desk.setText("");
for (int j = 0; j < objPlace.length; j++){
desk.append("Deskripsi:\n" + objPlace[j].toString() + "\n");
}
but when itried to run its to my emulator and real device, i get an error. here's my LogCat
please help me, and sorry for my english >_<
You can do this instead; use pathName directly in the for loop...
desk.setText("");
for (int j = 0; j < pathName.size(); j++){
desk.append("Deskripsi:\n" + pathName.get(j) + "\n");
}
you can put the line
objPlace[] = new Object[pathName.size()]
before
objPlace = pathName.toArray();
and check output otherwise do this way
desk.setText("");
for (int j = 0; j < pathName.size(); j++){
desk.append("Deskripsi:\n" + pathName.get(j) + "\n");
}
You never initialize your array... objPlace[] is a null array.
You need to do something like:
objPlace[] = new Object[arraysizenumber]
Ii is caused by NullPointerException, that means some needs parameters and you have passed a null value to it.
Based on your code it might be around desk.setText("");. Try inserting a space or some value into it and run.
Thanks my fren. im still not really well on using Object[]. then i used this code for solving my problem
String strPlace[] = (String[])pathName.toArray(new String[pathName.size()]);
desk.setText("");
for (int j = 0; j < strPlace.length; j++){
desk.append("Deskripsi:\n" + strPlace[j] + "\n");
}
Thanks for userSeven7s, flameaddict and khan for trying to help me.
nice to know u all :)

how to add one more item to already exist arraylist in android

i am using wheel view in my sample application. It display the items using xml parsing.
i want to add one item to that xml data. here my code
public ArrayList<ItemIdentifierType> getBikeTypeDataList() {
NodeList nodeList = doc.getElementsByTagName(key_BikeTypes);
for (int i = 0; i < nodeList.getLength(); i++) {
ItemIdentifierType itemIdentifierTypeInstance = new ItemIdentifierType();
Element element = (Element) nodeList.item(i);
itemIdentifierTypeInstance.setId(Integer.parseInt(element
.getAttribute("id")));
itemIdentifierTypeInstance.setName(element.getAttribute("desc"));
bikeTypesList.add(itemIdentifierTypeInstance);
}
return bikeTypesList;
}
here bikeTypeList have all the items just i want to add one item like -select- within bikeTypesList. any one can help me?
now its working fine, here my code
public ArrayList<ItemIdentifierType> getBikeTypeDataList() {
NodeList nodeList = doc.getElementsByTagName(key_BikeTypes);
ItemIdentifierType itemIdentifierTypeInstance = null ;
itemIdentifierTypeInstance = new ItemIdentifierType();
itemIdentifierTypeInstance.setId(0);
itemIdentifierTypeInstance.setName("-beliebig-");
Log.i("bike typeLsit: ", ""
+ itemIdentifierTypeInstance.getId() + " "
+ itemIdentifierTypeInstance.getName());
bikeTypesList.add(itemIdentifierTypeInstance);
for (int i = 0; i < nodeList.getLength(); i++) {
itemIdentifierTypeInstance = new ItemIdentifierType();
Element element = (Element) nodeList.item(i);
itemIdentifierTypeInstance.setId(Integer.parseInt(element
.getAttribute("id")));
itemIdentifierTypeInstance.setName(element.getAttribute("desc"));
Log.i("bike details from identifier type class: ", ""
+ itemIdentifierTypeInstance.getId() + " "
+ itemIdentifierTypeInstance.getName());
bikeTypesList.add(itemIdentifierTypeInstance);
}
System.out.println("TypeList"+bikeTypesList);
return bikeTypesList;
}

Categories

Resources