Answer the question
In order to leave comments, you need to log in
Parsing complexly structured XML data in an Android application?
Hello again dear habrchane.
Please help me parse the complex structured xml. I'm relatively new to Java, this is my first application.
The bottom line is that a request is sent to the service, and the response is returned in the form of xml data. The file contains a lot of data, one request returns from 100kb of xml data.
Maybe someone has already worked with this kind of tasks, and tell me how to implement it.
I will be very grateful.
Here is an abbreviated sample response from the server:
<?xml version="1.0" encoding="windows-1251"?>
<server xmlns:f="http://www.mysite.ru" >
<call-r>
<f:getProjects u="1" >
<v-r>
<v>
<v k="id" >1174522</v>
<v k="name" >Заголовок</v>
<v k="descr" >
<![CDATA[Какой то текст]]>
</v>
<v k="category" >2</v>
<v k="subcategory" >27</v>
<v k="city" ></v>
<v k="country" ></v>
<v k="cost" >10000</v>
<v k="kind" >1</v>
<v k="offers_count" >0</v>
<v k="pro_only" >1</v>
<v k="post_date" >1338117862</v>
<v k="currency" >2</v>
<v k="logo" ></v>
<v k="is_pro" >0</v>
<v k="priceby" >4</v>
<v k="prefer_sbr" >0</v>
<v k="link" >ссылка на файл</v>
<v k="link" >ссылка на файл</v>
<v k="link" >ссылка на файл</v>
</v>
<v>
<v k="id" >1174522</v>
<v k="name" >Заголовок</v>
<v k="descr" >
<![CDATA[Какой то текст]]>
</v>
<v k="category" >2</v>
<v k="subcategory" >27</v>
<v k="city" ></v>
<v k="country" ></v>
<v k="cost" >10000</v>
<v k="kind" >1</v>
<v k="offers_count" >0</v>
<v k="pro_only" >1</v>
<v k="post_date" >1338117862</v>
<v k="currency" >2</v>
<v k="logo" ></v>
<v k="is_pro" >0</v>
<v k="priceby" >4</v>
<v k="prefer_sbr" >0</v>
<v k="link" >ссылка на файл</v>
<v k="link" >ссылка на файл</v>
<v k="link" >ссылка на файл</v>
</v>
</v-r>
</f:getProjects>
<f:getProjects u="2" >
<v-r>
<v>
<v k="id" >1174522</v>
<v k="name" >Заголовок</v>
<v k="descr" >
<![CDATA[Какой то текст]]>
</v>
<v k="category" >2</v>
<v k="subcategory" >27</v>
<v k="city" ></v>
<v k="country" ></v>
<v k="cost" >10000</v>
<v k="kind" >1</v>
<v k="offers_count" >0</v>
<v k="pro_only" >1</v>
<v k="post_date" >1338117862</v>
<v k="currency" >2</v>
<v k="logo" ></v>
<v k="is_pro" >0</v>
<v k="priceby" >4</v>
<v k="prefer_sbr" >0</v>
<v k="link" >ссылка на файл</v>
<v k="link" >ссылка на файл</v>
<v k="link" >ссылка на файл</v>
</v>
<v>
<v k="id" >1174522</v>
<v k="name" >Заголовок</v>
<v k="descr" >
<![CDATA[Какой то текст]]>
</v>
<v k="category" >2</v>
<v k="subcategory" >27</v>
<v k="city" ></v>
<v k="country" ></v>
<v k="cost" >10000</v>
<v k="kind" >1</v>
<v k="offers_count" >0</v>
<v k="pro_only" >1</v>
<v k="post_date" >1338117862</v>
<v k="currency" >2</v>
<v k="logo" ></v>
<v k="is_pro" >0</v>
<v k="priceby" >4</v>
<v k="prefer_sbr" >0</v>
<v k="link" >ссылка на файл</v>
<v k="link" >ссылка на файл</v>
<v k="link" >ссылка на файл</v>
</v>
</v-r>
</f:getProjects>
<f:getMessFolders>
<v-r>
<v>
<v k="id" >24905</v>
<v k="fname" >offtop</v>
</v>
</v-r>
</f:getMessFolders>
</call-r>
</server>
Answer the question
In order to leave comments, you need to log in
I would advise you to look towards Simple Xml.
simple.sourceforge.net/
It's really very simple, the examples cover your case as well.
Here are the options for parsing xml on android:
www.ibm.com/developerworks/opensource/library/x-android/
Look towards http://developer.android.com/reference/javax/xml/xpath/package-summary.html
Have a look at JAXB
Let's say we want to read a constants.xml file like this: ideone.com/VbGI8
Create a Constants class: ideone.com/EuTNu
Create a Constant class: ideone.com/IuKqU
An example of reading a file: ideone.com/IvDaU
The first way: as written above - use JAXB, the java classes themselves can be generated according to the XML schema (if any).
The second way: use SAX - a stream parser, suitable for parsing very large files (the main differences from JAXB are that the file being read is not fully loaded into memory, which makes it possible to work with very large files, > GB and XML tag mapping falls on the programmer).
If you only need one tag, then it is definitely better to use SAX - it will be much more efficient.
Example: SAX example in google
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question