Android Kxml parsing Wbxml

Source: Internet
Author: User
Tags try catch

 wap binary xml defines the XML fragment to express the synchronization server address, remote database name, login account, and so on one, two access methods: The current Kxml supports two WAP formats: WBXML/WML. There are two ways to parse wbxml:1. Use J2ME to convert Wbxml to XML; 2. Use Kxml to parse the Wbxml stream directly.    Here I discuss the use of the second method to implement the client code parsing wbxml, of course, the use of kxml. Second, Kxml implementation method: First need to be located in the Web server application through the Open WAP gateway (about Jwap: see http://jwap.sourceforge.net/) Send WML file to j2me client. Before the WAP gateway sends the data j2me client the WAP gateway converts the WML file to the Wbxml file.    The following code shows how j2me client receives wbxml data, parses the data, and displays useful data on the phone screen.    It is important to note that the kxml v1.0 version used in this routine may be different from the kxml v2.0 version, and the developer can refer to the KXML2 manual.  import java.io.*;  import org.kxml.*;  import org.kxml.parser.*;  import org.kxml.wap.*;  import javax.microedition.lcdui.*;  import javax.microedition.midlet.*;  import javax.microedition.io.*; Public class wbxmltest extends midlet implements commandlistener { private  Display display = null;   private List menu = null; private form form =&Nbsp;null;  private string incomingtext =  "";  Static final command okcommand = new command ("OK", Command.ok, 1);  Static final command exitcommand = new command ("Exit", Command.exit, 0);  this is a hard coded wsp message that contains// address   of web server where our jsp page is located. byte[] message ={(Byte) ' 1 ', (byte) 0x40, (Byte) 0x3d, (byte) ' H ', (byte) ' t ', (byte) ' t ', (Byte) ' P ', (Byte) ': ', ( byte) '/', (Byte) '/', (Byte) ' L ', (byte) ' O ', (byte) ' C ', (byte) ' A ', (byte) ' L ', (Byte) ' H ', (byte) ' O ', (byte) ' s ', (byte) ' t ', ( byte) ': ', (Byte) ' 8 ', (byte) ' 0 ', (byte) ' 8 ', (byte) ' 0 ', (byte) '/', (Byte) ' E ', (byte) ' X ', (Byte) ' A ', (byte) ' m ', (Byte) ' P ', ( byte) ' L ', (byte) ' E ', (byte) ' s ', (byte) '/', (Byte) ' J ', (Byte) ' s ', (Byte) ' P ', (Byte) '/', (Byte) ' F ', (byte) ' I ', (byte) ' N ', ( byte) ' A ', (byte) ' L ', (byte) ' F ', (byte) ' I ', (byte) ' L ', (byte) ' E ', (byte) ' s ', (byte) '/', (byte) ' D ', (Byte) ' A ', (byte) ' t ', (Byte) '. ', (Byte) ' J ', (Byte) ' s ', (Byte) ' P ', (Byte) 0x80, (Byte) 0x94, (Byte) 0x88, (byte)  ) 0x81, (Byte) 0x6A, (Byte) 0x04, (Byte) 0x83, (byte) 0x99};  memory space to receive message.   byte[] msg = new byte [256]; Public void pauseapp ()  { /* ----- */ } public void destroyapp ( boolean unconditional) { notifydestroyed ();  } public void startapp ()  {display   = display.getdisplay (this);  This.mainmenu (); }//startapp//displays the menu screen private void mainmenu ()  {menu =   New list (" send request",  choice.implicit);  Menu.append (" send message", null);  Menu.addcommand (Okcommand);  Menu.setcommandlistener (this);  Display.setcurrent (menu);  }//mainmenu//display the reply from wapgateway  (Jwap). Private void showreply ()  {Form = new form (  "incoming message"  );    Form.append ("the price = "  + incomingtext);  Form.addcommand (Exitcommand);  Form.setcommandlistener (this); Display.setcurrent (form);  }//showreply// makes a wsp connection with a wapgateway,// Sends   a message and receives the reply.  Public void getconnect ()  {datagram dgram =null;  datagramconnection dc=null;  try {dc =  (datagramconnection) connector.open  ("datagram://127.0.0.1:9200");  Dgram = dc.newdatagram (message, message.length);  try{dc.send (Dgram);} catch  (interruptedioexception e) {e.printstacktrace ();  } dgram = dc.newdatagram  (  Msg,msg.length);  try{dc.receive (Dgram);}  catch  (interruptedioexception e) {e.printstacktrace ();}  catch ( ioexception e) {e.printstacktrace ();} // this is the most interesting part.  INCOMINGTEXT&NBSP;=&NBSP;THIS.GETINCOMINGTEXTOFWMLC (Dgram.getdata ());  This.showreply ();  Dc.close (); }//try catch  (Illegalargumentexception ie) {ie.printstacktrace ();  } catch  ( CONNECTIONNOTFOUNDEXCEPTION&NBSP;CNF) {cnf.printstacktrace ();  } catch  (Ioexception e) {  E.printstacktrace ();} }//getconnect () private string getincomingtextofwmlc  ( byte[] wmlc )  {try   {// remove wsp header.   we know it is 19 bytes for our case.  but for real world applications,// this should be dynamically   deteced. for  ( int j = 0; j < wmlc.length-19; j++ ) wmlc[j] =   wmlc[j+19];  Wmlparser parser = new wmlparser (New bytearrayinputstream (WMLC)); while (true)  {try {parseevent parseevent = parser.read ();  if  ( parseevent.gettype ()  == Xml.START_TAG )  {attribute attr =  Parseevent.getattribute ("value");  if  ( attr != null ) return attr.getvalue (); }//if}//try catch  ( ioexception e)  {}}//while}//try catch  ( IOException e) & nbsp  { e.printstacktrace ();  } return  "Error";  }//GETINCOMINGTEXTOFWMLC public void commandaction (command c, displayable d)  {  String commandlabel = c.getlabel ();  if  (Commandlabel.equals ("Exit")) Destroyapp (false);  else if  (commandlabel.equals ("OK")) GetConnect ();    }//commandaction}//class wbxmltest for demonstration purposes, in addition to establishing a web server, it is necessary to establish a jwap server in this machine. Third, code Description: The above code sends a data connection request to the local Jwap server URL: "datagram://127.0.0.1:9200", and sends a hard-coded WSP (wireless session  protocol) pleaseAsk: http://localhost:8080/examples/jsp/finalfiles/Dat.jsp, then wait and read Jwap server's response, The data is extracted using kxml parsing after receiving the response information (the attribute value of the element property named "value").    After parsing is complete, the data is displayed on the phone screen. The getconnect  method in the code establishes a connection to the Jwap server and sends a request to jwap server, requesting access to http://localhost:8080 on Web server /examples/jsp/finalfiles/dat.jsp, the GetConnect method calls the GETINCOMINGTEXTOFWMLC method to extract the received Wbxml data after receiving a request from Jwap server. Because the WAP protocol stack is used for communication between J2me client and Jwap server, the data received by J2me client contains the WSP header,    The WSP header is first removed from the GETINCOMINGTEXTOFWMLC method. The GETINCOMINGTEXTOFWMLC method then uses the Kxml event resolution mechanism for 4 steps: 1. A byte array that is passed in to hold wbxml data constructs the wmlparser  object; 2. Call Wmlparser's Read method and find the place where the first tag starts; 3. Reads the value of the "Value" property; 4.    Go back to the 2nd step and cycle between 2 and 3 until Start_tag is found. Iv. Data Flow: While the Jwap Gateway receives a hard-coded request from J2me client, the request is forwarded to Web server, web server in this routine is http://localhost : 8080. Web server after receiving the request, a hard-coded WML file is used as a response: <?xml version= "1.0"?> <! doctype wml public  "-//wapforum//dtd wml 1.1//en"   "http://www.wapforum.org/DTD/ Wml_1.1.xml "> <%@ page language= "java"  contentType=  "TEXT/VND.WAP.WML"  %> <wml> < Card id= "C0"  newcontext= "false"  ordered= "false" > <input type= "Price"  value= " 15224 " emptyok=" false "/> </card> </wml> when Jwap Gateway receives this web server WML file,    Convert it to wbxml format and modify its content-type encoding to Wbxml, and finally send the converted Wbxml format data to j2me client. Summary: The use of the Kxml method to avoid the conversion between XML and Wbxml, wbxml file format reduces the size of the XML file, not only can wbxml for WAP devices, but also for web-based programs and wireless devices communication and data exchange.  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.