How would I only return/parse the node elements that have a specific value.
Below is my simple XML but I only need the time
, meeting_name
and host_name
from the meeting_item
's that have a date of 2012-03-09
:
<meeting_item>
<time>12:30</time>
<date>2012-03-09</date>
<meeting_name>build a shop 4</meeting_name>
<host_name>Bob Jones</host_name>
</meeting_item>
<meeting_item>
<time>11:30</time>
<date>2012-03-23</date>
<meeting_name>build a shop advance</meeting_name>
<host_name>Judy Washington</host_name>
</meeting_item>
<meeting_item>
<time>11:30</time>
<date>2012-03-23</date>
<meeting_name>build a shop 2</meeting_name>
<host_name>Tom Smith</host_name>
</meeting_item>
<meeting_item>
<time>11:30</time>
<date>2012-04-06</date>
<meeting_name>build a shop 3</meeting_name>
<host_name>Tom Green</host_name>
</meeting_item>
Below is my parsing code so far but this just gets everything in the file:
String xml = XMLfunctions.getXML(items);
Log.d(TAG, xml);
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);
if((numResults <= 0)){
Toast.makeText(MeetingManager.this, "NOT GETTING XML", Toast.LENGTH_LONG).show();
finish();
}
Element docElem = doc.getDocumentElement();
NodeList nl = docElem.getElementsByTagName("Meeting_Data");
Element elem = (Element)nl.item(1);
NodeList nodes = elem.getElementsByTagName("meeting_item");
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>(); 开发者_如何学编程
Element e = (Element)nodes.item(i);
map.put("time", XMLfunctions.getValue(e, "time"));
map.put("name", XMLfunctions.getValue(e, "meeting_name"));
map.put("hostname", XMLfunctions.getValue(e, "host_name"));
mylist.add(map);
}
ListAdapter adapter = new SimpleAdapter(MeetingManager.this, mylist , R.layout.listlayout,
new String[] {"time", "name", "hostname" },
new int[] { R.id.time, R.id.meeting_name, R.id.host });
setListAdapter(adapter);
Also in my logCat Log.d(TAG, xml); doesn't make it all the way through the file. And sometimes throws and error about not reading all of the file. Is there a limit I can httpGet?
If you only want to parse a portion of the xml file I recomend you use something like the XmlPullParser or the SaxParser. As the dom parser will try to create a complete tree of objects based on your xml data. To do this with xml pull parser, your parsing code would look like
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(is,"UTF-8");
int eventType = xpp.getEventType();
outerloop:
while (eventType != XmlPullParser.END_DOCUMENT) {
switch(eventType){
case XmlPullParser.START_DOCUMENT:
Log.e("Start","Start");
break;
case XmlPullParser.START_TAG:
Log.e("StartTag", xpp.getName());
break;
case XmlPullParser.END_TAG:
Log.e("EndTag", xpp.getName());
//When you need to stop parsing use this
break outerloop;
break;
}
eventType = xpp.next();
}
精彩评论