based xml file" that return some value. And depending on how the system is confinured it answer diffeterent..
and i beleave that SAX or DOM in the way to go for the "get node and under child"..
but what do you think is the easyest to learn? (the xmlfile is "verry" small, max 100 lines ant normal ~20) SAX or DOM? (and add this in a singel textview) (but later in some strings)
i use now:
private CharSequence getNode(String, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
// and FIY: in a asynctask-back:.."in a try-block"
URL url = new URL(Url[0]);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// Download the XML file
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
// Locate the Tag Name
nodelist = doc.getElementsByTagName("response");
//
// ******** NOW:
//and then in asynctask-post:..
for (int temp = 0; temp < nodelist.getLength(); temp++) {
Node nNode = nodelist.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
textView.setText(textView.getText() + getNode("status
+ getNode2("time", eElement) + "\n"); //for read the <status>1</status> (se XML-exempel
bellow)
textView.setText(textView.getText() + getNode("time", eElement) + "\n");
//this give me the time-value of: <faultoldlist> <row> : 2013/12/16 15:11:16
}
}
- But the problem is that I can't understand how to get for exampel: timevalue in the in the SECOND , not only the first element
I beleave I look at the first element only :-(
here is some XML-exempel:
<respons>
<status>1</status>
<datetime>2015/01/09 16:25</datetime>
<unitstatus>
<unittotal>40</unittotal> //1 to 40
<units>
<unit>
<id>00</id>
<version>5.5.0</version>
<name>
<![CDATA[ unit 0 ]]>
</name>
<systemcurrent>2000mA</systemcurrent>
<chargecurrent>0mA</chargecurrent>
<emp>28°C</temp>
</unit>
// ....
<unit>
<id>40</id>
<version>5.5.0</version>
<name>
<![CDATA[ unit 1 ]]>
</name>
<systemcurrent>2000mA</systemcurrent>
<chargecurrent>0mA</chargecurrent>
<temp>28°C</temp>
</unit>
//units can have only 1 unit or up to 40 units ID nr can be "vaiated", ex 3 02,04,10
</units>
</unitstatus>
<systemstatus>
<fault1nr>0</fault1nr>
<faultanr>0</faultanr>
</systemstatus>
<faultoldlist >
<row>
<time>2013/12/16 15:11:16</time>
<type>
<![CDATA[ Error1 ]]>
</type>
</row>
<row>
<time>2013/12/16 15:11:10</time>
<type>
<![CDATA[ Error123 ]]>
</type>
</row>
<row>
<time>2013/12/16 15:11:13</time>
<type>
<![CDATA[ Error211 ]]>
</type>
</row>
</response>
No comments:
Post a Comment