1

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.

Axel
  • 1,674
  • 4
  • 26
  • 38
  • Did you take a look on this answer? Here I've explained how to parse an XML fron Java2ME [Parsing XML](http://stackoverflow.com/questions/9175745/xml-parsing-not-working-on-android-build-of-lwuit-app) – Mun0n May 19 '14 at 15:19
  • Thanks. It looks great and it might be what I'm looking for. The problem is that for some reason the execution stops at this point : `XMLParser myParser = new XMLParser();` – Axel May 21 '14 at 23:30
  • Does it show any exception?? – Mun0n May 22 '14 at 08:42
  • Yes now that you mention it. This is what I get from the output window "Uncaught exception: java.lang.NoClassDefFoundError: com/sun/lwuit/xml/XMLParser - MIDkxml.lookUp(MIDkxml.java:112) - MIDkxml.run(MIDkxml.java:65) at java.lang.Thread.run(Thread.java:744)" – Axel May 23 '14 at 17:19
  • I'm really interested in using lwuit, because we don't need to create classes to read XML files – eddy May 23 '14 at 17:22
  • It's a very strange exception, did you have the last LWUIT version?? – Mun0n May 24 '14 at 16:04

1 Answers1

2

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?

Ales
  • 168
  • 9
  • I found this tutorial http://developer.nokia.com/community/wiki/XML_Parser_in_Java_ME , but the XML is read from an existing file – Axel May 19 '14 at 20:14
  • @alex another thing I don't understand in that tutorial, being a completely newbie in the java world, is the "Utility" thing – Axel May 19 '14 at 20:20
  • Here is a sample code that uses kxml2 to parse your XML, it is too big for comment, so here is a link: [Kxml2Demo.java](http://to.neznam.sweb.cz/Kxml2Demo.java). Method `GetCustomer` reads content from a [file](http://to.neznam.sweb.cz/spanishWS.xml) and returns byte array to show how to use it as the input for the parser. – Ales May 20 '14 at 11:32
  • Thanks a lot that's much more than what I expected to get. But tell me, is it for a MIDLET?? I ask because from the little I know the program should follow an structure like having an startApp() method for example. – Axel May 22 '14 at 00:10
  • Sure you can use the parsing code code in a midlet. There is `main(string[])` method in the example just so I could test it on Windows command line :). I use KXML2 to parse XML like this in my j2me application (midlet). I believe the example builds against and runs with just CLDC-1.1 and kxml2. – Ales May 22 '14 at 11:02