Android - KML Parser using DOM Parser - android

i've a problem here.
i wanna parsing a KML document from google maps. i'd successfuly to parsing the nodes which stores a coordinate.
but i wanna parse the node as childnodes
here is the KML documents.
<Placemark>
<name>Head north on Jalan Kebagusan Raya toward Jalan Anggrek</name>
<description><![CDATA[go 850 m]]></description>
<address>Jalan Kebagusan Raya</address>
...
</Placemark
to parse it i used this code,
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());
}
}
desk.setText("");
for (int j = 0; j < strPlace.length; j++){
desk.append("Deskripsi:\n" + strPlace[j] + "\n");
}
but when i tried in a real device/emulator, i got something i dont want to.
this is the screenshot from my real device
from the scrshot, i just want to grab the "Head northeast on Jalan Darmo Kali toward Jalan Darmoerjo 4" informations. just it.
can u help me to solve my problems?
thanks in advance, and sorry for my english, thanks :)

Something like the following should do what you want.
NodeList nlPlace = doc.getElementsByTagName("Placemark");
for(int p = 0; p < nlPlace.getLength(); p++){
Element place = (Element)nlPlace.item(p);
Element name = (Element)place.getElementsByTagName("name");
//.....
}
}

Related

How to parse this xml file?

Hi i want to parse this
<entry>
<id>http://306721</id>
<title type='text'>MY New</title>
<photo:id>513306721</gphoto:id>
<photo:name>MYNew</gphoto:name>
<photo:numphotos>9</gphoto:numphotos>
<media:group>
<media:content url='http:Ya4MIz9Y/MYNew.jpg' medium='image' type='image/jpeg' />
<media:keywords />
<media:thumbnail url='htt0-c/MYNew.jpg' height='160' width='160' />
<media:title type='plain'>MY New</media:title>
</media:group>
</entry>
i am able to parsing this file, and also able to read some values from the above xml document like this
Document doc = db.parse(is);
NodeList entries = doc.getElementsByTagName("entry");
for (int i = 0; i < entries.getLength(); i++) {
Element element = (Element) entries.item(i);
albumIds.add(getCharacterDataFromElement((Element) element
.getElementsByTagName("photo:id").item(0)));
}
in the above code i am reading gphoto:id like this i am reading photo:name and photo:numphotos.
Now i want to read url from the media:thumbnail those are available in the media:group.. Can any one help me on this how to read this.
Please see below link of my SO Question, it will solve your problem and if you have any query regarding that then tell me.
XML Parsing Using DOM Parser
Add those values in to object(using java pojo) and add that object into ArrayList
try to use Below Code. Hope it will help you.
Document doc = db.parse(is);
NodeList entries = doc.getElementsByTagName("entry");
for (int i = 0; i < entries.getLength(); i++) {
Element element = (Element) entries.item(i);
albumIds.add(getCharacterDataFromElement((Element) element
.getElementsByTagName("gphoto:id").item(0)));
NodeList nodelist_group = doc.getElementsByTagName("media:group");
for (int j = 0; j < nodelist_group.getLength(); j++) {
Element element = (Element) nodelist_group.item(j);
NodeList nodelist_content = doc.getElementsByTagName("media:content");
URLS.add(nodelist_content.getAttribute('url'))); }}

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 :)

I need to parse the XML file in my android application. I dont know how many nodes it got

My activity class look like this:
AssetManager assetmgr= getAssets();
String list[] = assetmgr.list("subdir");
if (list != null) {
DocumentBuilder builder =DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(getAssets().open("subdir/fullSurvey.xml"));
xml object class looks like this:
NodeList root = doc.getElementsByTagName("root");
NodeList nlQuestions = root.item(0).getChildNodes();
QuestionObject[] allQuestions = new QuestionObject[nlQuestions.getLength()];
for (int i = 0; i < nlQuestions.getLength(); i++) {
Node question = nlQuestions.item(i);
NodeList childNodes = question.getChildNodes();
QuestionObject x = new QuestionObject();
for (int j = 0; j < childNodes.getLength(); j++) {
Node child = childNodes.item(j);
if (child.getNodeName() !="#text") {
Questions t = Questions.valueOf(child.getNodeName());
// etc.
I dont know how to parse xml file according to attribute value
I don't know that many XML parsers will traverse according to attribute values. Most deal with nodes, nodelists, etc. You may have to read in all the nodes and hash by the attribute values yourself.

Android 4.0 ice cream sandwich Parser Errors

I have made a application which reads information from an Api.
Link : http://api.amp.active.com/camping/campground/details?contractCode=CO&parkId=50032&api_key=2chxq68efd4azrpygt5hh2qu
Following is my code :
NodeList list = element.getElementsByTagName("detailDescription");
Log.i("ZealDeveloper","I M In detail "+list.getLength());
if(list != null && list.getLength() > 0){
for(int i = 0; i < list.getLength(); i++){
entry = (Element) list.item(i);
description = entry.getAttribute("description");
drivingDirection = entry.getAttribute("drivingDirection");
latitude=entry.getAttribute("latitude");
longitude=entry.getAttribute("longitude");
}
}
NodeList list1 = element.getElementsByTagName("amenity");
Log.i("ZealDeveloper","I M In 2 "+list1.getLength());
if(list1 != null && list1.getLength() > 0){
for(int i = 0; i < list1.getLength(); i++){
entry = (Element) list1.item(i);
nameAmenity = entry.getAttribute("name");
listAmenity.add(nameAmenity);
}
arrAmenity = listAmenity.toArray(new String[listAmenity.size()]);
StringBuilder builder = new StringBuilder();
for ( int i = 0; i < arrAmenity.length; i++ ){
builder.append(arrAmenity[i]+"\n");
}
txtAmenity.setText(builder);
}
#
I am Getting list.getLength() as 0(was getting 1 in pervious versions of android) , so the parser this condition. For amenity tag i m getting the desired list size.
The only reason I can think of is that "detailDescription" is already root of the document so probably element is the tag you are seeking for. It doesn't have any children with the name "detailDescription" so getElementsByTagName("detailDescription") returns empty list. So change first half of your code as follows:
Log.i("ZealDeveloper","I M In detail " + element.getTagName());
if(element.getTagName().equalsIgnoreCase("detailDescription")) {
description = element.getAttribute("description");
drivingDirection = element.getAttribute("drivingDirection");
latitude = element.getAttribute("latitude");
longitude = element.getAttribute("longitude");
}
/* rest of your code...*/
I had the same issue. I switch from DOM to XPath. More here

Dom parsing in Android

If I parse the tag that contains <p>Some Text</p> tag, I get a null pointer exception.
My RSS feed is as follows:
<quaddeals_conditions><p>Limit one QuadDeal</p></quaddeals_conditions>
My code is:
if (name.equalsIgnoreCase("quaddeals_conditions")) {
property.normalize();
conditions = property.getFirstChild().getNodeValue();
}
You have an element inside an element .
Therefore retrieve all quaddeals and then iterate each one and retrieve from it the p element:
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(this.inputStream);
Element root = dom.getDocumentElement();
// snip
NodeList items = root.getElementsByTagName("quaddeals_conditions");
for (int i = 0; i < items.getLength(); i++) {
Node item = items.item(i);
NodeList properties = item.getChildNodes();
for (int j = 0; j < properties.getLength(); j++) {
Node property = properties.item(j);
String name = property.getNodeName();
if (name.equalsIgnoreCase("p")) {
property.getFirstChild().getNodeValue(); // Your paragraph data
}
}
}
Hope this helps.
is "name" not NULL? I dont see you check for that.
It's good coding practice to compare the other way if possible:
if ("quaddeals_conditions".equalsIgnoreCase(name))...
So even if "name" is NULL, you don't get a NullPointerException.
Always check for not null before accessing some object member.

Categories

Resources