Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get element text in node in Java?

Tags:

java

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()???

enter image description here

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.

like image 990
Cheung Avatar asked Nov 28 '22 09:11

Cheung


1 Answers

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

enter image description here

like image 185
Cheung Avatar answered Dec 17 '22 14:12

Cheung