J2me: how to parse an array of bytes in xml and then read and display specific data from this XML

I use the ASP.NET web API method, which returns the data in xml format. Everything was fine until I had to parse the byte array obtained from openInputStream. Everyone says that they use this or that library, but, unfortunately, there is not much information, and the only worthy example I found is an outdated library called KXML, in which the author reads a physical document (obviously not my case) . Personally, I wanted to use KXML2, but this moment I am desperate and open to the very first solution, which allows me to read XML as easily as possible.

Here is the code I use to use 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 is a sample XML result that I got from the GetCustomers method:

 <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:

Client number 1:

codigoCli: 30

direccion: MCDO. SAN MARTIN PSTO. Number 06

nroID: 26626315

nroTelef: 365548

razonSocial: ABANTO CASTAÑEDA, PAULA

tipoPersona: N


Client number 2:

.....

I really hope that you can understand my situation as a .net developer, it really disappoints, they don’t find much information on a topic like this.

Any help you can provide would be greatly appreciated.

Thanks in advance.

+1
source share
1 answer

You can use setInput (new ByteArrayInputStream (response), null / null for auto-detection, or specify the correct type of id /) encoding to parse the xml response. Or what is the problem with kxml2?

+2
source

All Articles