j2me: How to parse a byte array into xml and then read and display specific data from that XML
Asked Answered
O

1

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.

Outguard answered 19/5, 2014 at 5:21 Comment(6)
Did you take a look on this answer? Here I've explained how to parse an XML fron Java2ME Parsing XMLVoronezh
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();Outguard
Does it show any exception??Voronezh
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)"Outguard
I'm really interested in using lwuit, because we don't need to create classes to read XML filesCommissariat
It's a very strange exception, did you have the last LWUIT version??Voronezh
I
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?

Isherwood answered 19/5, 2014 at 13:23 Comment(5)
I found this tutorial developer.nokia.com/community/wiki/XML_Parser_in_Java_ME , but the XML is read from an existing fileOutguard
@alex another thing I don't understand in that tutorial, being a completely newbie in the java world, is the "Utility" thingOutguard
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. Method GetCustomer reads content from a file and returns byte array to show how to use it as the input for the parser.Isherwood
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.Outguard
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.Isherwood

© 2022 - 2024 — McMap. All rights reserved.