0

I am stuck at parsing a xml file. I have the following code of an activity:

Document doc = Jsoup.parse(sb.toString(), "", Parser.xmlParser());

for (Element e : doc.select("Placemark")) {
    Log.d(TAG, "name is " + e.attr("name")+ "cordinates is" + e.attr("coordinates"));

I have this xml example:

<Placemark>
    <name>international route for military only</name>
    <styleUrl>#m_ylw-pushpin0</styleUrl>
    <LineString>
        <tessellate>1</tessellate>
        <coordinates>
            34.9524993959688,32.7730878314906,0 34.95166998854646,32.77308993256781,0 34.95125522754987,32.77309097997785,0 34.95070215058794,32.77309237599226,0 34.94918083131482,32.77309620394223,0 34.94752038951339,32.77309850484357,0 34.94655149233169,32.77309830859218,0 34.94572098730102,32.77309813906572,0 34.94502892287641,32.77309798764293,0 34.94419838488641,32.77309782117877,0 34.94309104377032,32.77309757681172,0 34.94226051634391,32.77309738973906,0 34.94115313434187,32.77309712978963,0 34.94018416814745,32.77309689649781,0 34.93935361177677,32.77309669003321,0 34.93866147471769,32.77309651419306,0 34.93769247332618,32.77309626314753,0 34.93658505149507,32.77309586239551,0 34.93533920626601,32.77309542577984,0 34.93450864230719,32.77309510022808,0 34.93340122016876,32.77309465949123,0 34.93160165271385,32.77309392217991,0 34.92883324477039,32.7728599214421,0 34.92385060482191,32.77169369268832,0 34.91540783742491,32.76947803887981,0 34.90793372651729,32.7673788666683,0 34.90322739422959,32.76621216804099,0 34.89893646854004,32.76481273889556,0 34.89533713275235,32.76387923817537,0 34.89339945743092,32.76306307317424,0 34.89215360021323,32.76271299419985,0 
        </coordinates>
    </LineString>
</Placemark>

I get the parsing correctly when I am printing the doc. But when I am trying to grab the name and coordinates of the placemark by accessing its attribute field, it gives me an empty string.

It seems when I use doc.select("Placemark"), it only takes the strings inside the tag and it ignores the tag itself.

1 Answer 1

1

Use e.select("name").text()

for (Element e : doc.select("Placemark")) { 
    Log.d(TAG, "name is " + e.select("name").text()+ "cordinates is" + e.select("coordinates").text()); 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Good luck. If my answer helped you, please accepting it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.