Alexander

Thanks for sharing this!

1 - any objections to adding this to the Wiki?
(I don't mind doing it on your behalf)
2 - is there a public site for this?  I have  a P800
and it would be really cool to see Cocoon in 
action this way!

Derek

>>> [EMAIL PROTECTED] 2004/07/20 11:01:43 PM >>>
Hello, everybody

Remember my doubts about using Cocoon to feed Midlets with data? Well,
I 
found the solution.
Thanks to numerous hints from this group I could realize that the
Midlet 
on the mobile phone receives and parses XML correctly.

To parse XML in J2ME, use kXML. It works really fine! (see: 
http://kxml.objectweb.org/software/downloads/ to obtain the package and

the docs)

Let me explain how I did it:

On the server side:
1. In a pipeline: Action to lookup the data (EJB - J2EE to JBoss) and 
pass the object of interest to the request with 
request.setAttribute(<name>, <object>)
2. Generate xsp, using a logicsheet which creates XML from the object
3. Serialize (so skip Transform!) to XML.

At the mobile phone:
1. Open a HttpConnection for the URL for which the pipeline-match is 
valid (will execute the pipeline, mentioned above)
2. Create a DataInputStream, using the HttpConnection object from step
1
3. Read the XML, produced by the server , using a read-method of the 
DataInputStream object in a byte-array
4. Create a parser object, using the byte-array.

Putting it all together, in the Midlet, you should create a method like

this:

  private XmlParser getParser(String url) {
    byte[] xmlByteArray = new byte[2048]; // Assume this size as the
max 
stream size we can receive
    XmlParser parser = null;

    // Get the XML data as byte array:
    try {
      conn = (HttpConnection)Connector.open(url); // Request the
servlet
      dis = new DataInputStream(conn.openDataInputStream());
      dis.read(xmlByteArray); // Read the byte array NOW

      // Create a parser to process the xml tags:
      ByteArrayInputStream xmlStream = new 
ByteArrayInputStream(xmlByteArray); // Create the input stream
      InputStreamReader xmlReader = new InputStreamReader(xmlStream);
// 
Give it to a stream reader
      parser = new XmlParser(xmlReader); // Create a parser
    } catch (IOException ex) {
      return null;
    }
    return parser;
  }

The URL points to the pipeline at the server, which will provide the 
data in XML.format.

With the parser, you can parse the contents. Here is an example:

   protected MiArticle getArticleFromXML(String barcode) {
    MiArticle art = new MiArticle();
    String url = this.baseURLXML + this.articleByBarcodeXML + 
"?Barcode=" + barcode; // Assemble URL
    String tag = "";

    try {

      // Get results and create parser
      XmlParser parser = this.getParser(url); // Here I call the above

method

      // Parse the xml to get the article data:
      boolean endFlag = false; // Reset end read flag
      do {
        ParseEvent event = parser.read(); // Read a tag
        ParseEvent pe; // Declare a second parse event to read the
contents
        switch (event.getType()) {

          case Xml.START_TAG:
            tag = event.getName(); // To visualize...
            if (tag.compareTo("Codi") == 0) { // Codi found
              pe = parser.read();
              art.setCodi(Integer.parseInt(pe.getText()));
            }
            if (tag.compareTo("Barcode") == 0) { // Barcode found
              pe = parser.read();
              art.setBarcode(pe.getText());
            }
            if (tag.compareTo("Nom") == 0) { // Name found
              pe = parser.read();
              art.setNom(pe.getText());
            }
            break;

          case Xml.END_DOCUMENT:
            endFlag = true; // Mark exit
            break;

          default:

        } // End switch
      } while (!endFlag);

    } catch (IOException ex) {
      System.out.println("IOException. Details: [" + ex.toString() +
"].");
    } catch (Exception ex) {
      System.out.println("Exception. Details: [" + ex.toString() +
"].");
    }

    if (art.hasValidData()) {
      return art;
    } else {
      return null;
    }

  }


As you see, I am filling in the blanks of an object of class MiArticle,

using tags "Codi", "Barcode" and "Nom" in the XML, received from the
server.
After this action, you can display the data at the mobile's screen. In

this way, Cocoon serves me from J2EE / RMI via XML request/response to

J2ME MIDP at a mobile phone...

Some observations:

- Works with Sony/Ericsson P800. Is NOT slower, compared with the 
"classic" way, using writeUTF() / readUTF()
- Works with PALM Tungsten, with the P800 as modem. Takes a little more

time as the classic way, but this is acceptable.
- Other devices to be checked tomorrow.

In both cases, I used GPRS (about 50 kb/sec) to our private APN to the

STA's LAN.

Special thanks to those people who have taken some time to answer my 
message. Attention: The "clue" to the solution was found in the article

at http://www-106.ibm.com/developerworks/library/wi-parsexml/ 

If somebody should be interested in more detailed info about this 
solution, please contact me. Other observations are welcome too.

Best regards to everybody
Alexander



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED] 
For additional commands, e-mail: [EMAIL PROTECTED] 


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
MailScanner thanks transtec Computers for their support.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to