Reputation: 15552
I am new to Android Development,and want to parse the XML and bind to Google MapView. However, i cannot read the Text between element.
The XML
<?xml version="1.0" encoding="utf8"?>
<table name="clinicaddress">
<row>
<GeoPoint>22.3852860,113.9664120</GeoPoint>
</row>
<row>
<GeoPoint>22.336950,114.1578720</GeoPoint>
</row>
</table>
The Code:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
try {
inputStream = assetManager.open("clinics.xml");
String xmlRecords = readTextFile(inputStream);
//Log.e("Clinics XML", xmlRecords);
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlRecords));
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(is);
doc.getDocumentElement ().normalize ();
NodeList listOfClinics = doc.getElementsByTagName("row");
Log.e("listOfClinics Length" , String.valueOf(listOfClinics.getLength()));
//Loop the XML
for (int x = 0; x < listOfClinics.getLength(); x++) {
Node ClinicNode = listOfClinics.item(x);
NodeList ClinicInfo = ClinicNode.getChildNodes();
for (int y = 0; y < ClinicInfo.getLength(); y++) {
Node info = ClinicInfo.item(y);
Log.e(info.getNodeName() , info.getNodeValue()); //<--Error on info.getNodeValue()
}
}
//End Loop XML
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
So, what wrong with getNodeValue()???
And one more question, how can i set the Eclipse like Visual Studio, If any error, it will break to the line on source line file, instead of just print out the stack message on debug windows.
UPDATED 1. I found this post: org.w3c.dom.Node with Android version less than 2.2 org.w3c.dom.Node with Android version less than 2.2
node.getTextContext() is the same as node.getFirstChild().getNodeValue().
However, i try this, it is goes error too.
Upvotes: 4
Views: 28008
Reputation: 15552
Finally, i get this works.
I use info.getFirstChild().getNodeValue()
for (int y = 0; y < ClinicInfo.getLength(); y++) {
Node info = ClinicInfo.item(y);
if (info.hasChildNodes()) {
Log.e(info.getNodeName(), info.getFirstChild().getNodeValue());
}
}
Line 3 is important, i Log the hasChildNodes() and watch in LogCat, don't know why it contains a node named "#text" and no childnodes (which is false on logcat).
But i believe my XML is correct format. After Google, found many explanation. https://www.google.com/search?q=xml+%23text
Upvotes: 14
Reputation: 1900
Text are just children of the node. you need to loop over all the children of the GeoPoint Node, check the node type to be Node.TEXT_NODE and concatenate the texts
Upvotes: 2
Reputation: 16392
Use the getTextContent() method.
node.getTextContent()
The getTextContent() method returns the text of all of the nested tags.
Upvotes: 9