XML parsing in Android
Please tell me, I need to parse a simple XML'ku vida:
<?xml version="1.0" encoding="UTF-8"?>
<result>
<status code="200" />
<building number="434" part="0" url="http://www.piccadillyrecords.com/mp3/Sum%2041%20-%20No%20Brains.mp3">Gu</building>
</result>
Can you tell me how to make it easier? Mb is there some simple class for parisng? Somewhere I saw an analog of php'shnoy simplexml, but now I can't find it
4
4 answers
I would write a simple parser based on SAX like this (written on my knees, so please do not throw tomatoes!)
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
xpp = factory.newPullParser();
StringReader sw=new StringReader(s); //s содержит ваш XML
xpp.setInput(sw); //подаем на вход парсера
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if (eventType == XmlPullParser.START_TAG) //начальный тег
{
inBuilding=false;
if(xpp.getName().compareTo("status")==0)
code = xpp.getAttributeValue(null, "code"); //читаем атрибут code
else if(xpp.getName().compareTo("building")==0)
{
number = xpp.getAttributeValue(null, "number");
part = xpp.getAttributeValue(null, "part");
url = xpp.getAttributeValue(null, "url");
inBuilding=true;
}
}
else if(eventType==XmlPullParser.TEXT)
{
if(inBuilding) //если мы внутри тега building
buildingValue=xpp.getText();
}
eventType=xpp.next();
}
Naturally, the values of attributes, fields, and so on. after reading it, you need to put it somewhere. Well, you're already there yourself. :)
3
Author: Barmaley, 2012-04-24 05:18:39
Java has a lot of tools for working with XML.
- Working with XML in Android
- Using XML and JSON with Android: Part 1. Advantages of JSON and XML for Android applications
- Using XML and JSON with Android: Part 2. Creating hybrid Android apps using JSON
Example of deserialization using Simple
Serializer s = new Persister();
Example e = s.read(Example.class, new File("example.xml"));
0
Author: Привет, 2012-04-23 17:42:16
XML Pull Parser is an interface that defines parsing functionality...
0
Author: Gorets, 2012-04-23 17:43:24
Read it here: http://www.ibm.com/developerworks/opensource/library/x-android/
Another option:
//вспомогательный метод - получает содержимое тэга
String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
void test() {
File xmlFile = new File(PathToFile);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
doc.getDocumentElement().getNodeName(); //имя корневого тэга
NodeList nList = doc.getElementsByTagName("building"); //получаем все теги building
for (int nodeIndex = 0; nodeIndex < nList.getLength(); temp++) { //пробегаем тэги
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
getTagValue("building", eElement); //получить содержимое текущего тэга
eElement.getAttribute("number"); //получить значение аттрибута
}
}
}
0
Author: megacoder, 2012-04-23 18:00:45