I'm consuming a ASP.NET Web API method that returns data in xml format. Everything was fine until I had to parse the byte array I got ,resulting from the openInputStream. Everyone says use this or that library,but unfortunately there isn't much info and the only decent example I found was from a deprecated library called KXML ,in which the author read a physical document (obviously not my case). Personally I wanted to use KXML2, but I this point I'm desperate and open to the very first solution that lets me read XML in the easiest possible way.
Here's is the code I use to consume the Web API method :
HttpConnection connection = null;
InputStream is = null;
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] response = null;
try {
connection = (HttpConnection)Connector.open("http://myminimarket/api/customers/GetCustomers", Connector.READ);
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.1");
if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
is = connection.openInputStream();
if (is != null) {
int ch = -1;
while ((ch = is.read()) != -1) {
bos.write(ch);
}
response = bos.toByteArray();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
}
if (is != null) {
is.close();
is = null;
}
if (connection != null) {
connection.close();
connection = null;
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
And here's is a sample of XML result I got from the method GetCustomers:
<ArrayOfCustomer xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WSWebAPI.Helpers">
<Customer>
<codigoCli>30</codigoCli>
<direccion>MCDO. SAN MARTIN PSTO. Nº 06</direccion>
<nroID>26626315</nroID>
<nroTelef>365548</nroTelef>
<razonSocial>ABANTO CASTAÑEDA, PAULA</razonSocial>
<tipoPersona>N</tipoPersona>
</Customer>
<codigoCli>61</codigoCli>
<direccion>
JR. SANTA TERESA DE JUORNET MZA. L. LOTE 11 (FRENTE AL QUINDE-COSTADO DE FARMACIA)
</direccion>
<nroID>10414741067</nroID>
<nroTelef/>
<razonSocial>ACUÑA SIFUENTES, ILZE SOLEDAD</razonSocial>
<tipoPersona>N</tipoPersona>
</Customer>
<Customer>
<codigoCli>69</codigoCli>
<direccion>JR. JOSE GALVEZ Nº 478</direccion>
<nroID>15586005</nroID>
<nroTelef/>
<razonSocial>AEDO YANQUI, MARGARITA</razonSocial>
<tipoPersona>N</tipoPersona>
</Customer>
<Customer>
<codigoCli>115</codigoCli>
<direccion>JR. AMALIA PUGA Nº 1008 TELEF. 367878</direccion>
<nroID>10266028356</nroID>
<nroTelef/>
<razonSocial>ALARCON ZEGARRA, EDULFO</razonSocial>
<tipoPersona>N</tipoPersona>
</Customer>
With these details, I would like to find a way to display something like this:
Customer # 1:
codigoCli: 30
direccion : MCDO. SAN MARTIN PSTO. Nº 06
nroID : 26626315
nroTelef: 365548
razonSocial: ABANTO CASTAÑEDA, PAULA
tipoPersona: N
Customer # 2:
.....
I really hope you can understand my situation, being a .net developer, it is really frustrating not finding much info on topic like this one.
Any assistance you can provide would be greatly appreciated.
Thanks in advance.
You can use setInput(new ByteArrayInputStream(response), null /null for autodetection, or specify proper encoding id string/) method to parse the xml response. Or what is the problem with kxml2 exactly?