使用SAX黑莓XML解析 [英] XML parsing using SAX for Blackberry

查看:167
本文介绍了使用SAX黑莓XML解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解析下面的XML文件:

I want to parse following xml file:

<login>
    <address id="1">
        <username>mahesh</username>
        <password>1234</password>
    </address>

    <address id="2">
        <username>admin</username>
        <password>admin</password>
    </address>  

    <address id="3">
        <username>a</username>
        <password>a</password>
    </address>  
</login>

任何一个可以帮助我吗?给我一些样本code。使用SAX解析器解析它。我想从HttpConnection的方法,此文件。我在BB的发展是新。

Can any one help me? Give me some sample code to parse it using SAX parser. I want to get this file from httpConnection method. I'm new in BB development.

推荐答案

您可以使用DOM和放大器; SAX解析器的XML解析。

You Can use Dom & Sax parsers for XML Parsing .

有是调用从HTTP请求和放大器XML中的code段;比使用SAX解析器解析它。

There is a Code snippet for Calling Xml from HTTP request & than to parse it using Sax Parser.

SAXParserImpl saxparser = new SAXParserImpl();
ListParser receivedListHandler=new  ListParser();
static DataInputStream din = null;
public static String response;


    HttpConnection hc = null;
        OutputStream os;
           try
           {  
               final String url ="<Enter URL for Xml Http Address>"+ InitClass.getConnectionString()+";ConnectionTimeout=25000";


               hc = (HttpConnection)Connector.open(url);

               os = hc.openOutputStream();
               os.write(postDataBytes);

               if (hc.getResponseCode() == 200)
                    din = hc.openDataInputStream();
                else
                    response = "" + hc.getResponseCode();
                saxparser.parse(din, receivedListHandler);
           }
           catch (Exception e) 
           {

           }
           finally 
           {
              try 
              {
                  if(din!=null)
                      din.close();
                  din = null;
                  if(hc!=null)
                      hc.close();
                  hc = null;
              }
              catch (Exception e) {   }
           }

/ *解析类* /

 public class ListParser extends DefaultHandler 
{
private String Key="";
private  Hashtable ht=new Hashtable();
vector vec = new Vector();
public ListParser()
{

}
/**
* Gets called, whenever a Xml is start .
*/
public void startDocument() throws SAXException 
{

} 
/**
* Gets called, whenever a Xml is End .
*/
public void endDocument() throws SAXException 
{ 


} 
/**
* Gets called, whenever a new tag is found.
*/
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException 
{
    if(name.equals("address"))
    {
        ht = null;
        ht = new Hashtable();
    }
    else if(name.equals("login"))
    {

    }
    Key=name;
}

/**
* Gets called, whenever a closed tag is found.
*/
public void endElement(String uri, String localName, String name) throws SAXException
{
    if(name.equals("address"))
    {
        vec.addElement(ht);
    }
}
public void characters(char[] ch, int start, int length) throws SAXException 
{
    String element=new String(ch, start, length);
    ht.put(Key, element);
}
}

它会分析XML并会为您在向量VEC数据哈希表,每XML标签。

It will parse your XML and Will provide you data in vector vec in hashtables for per XML tag.

这篇关于使用SAX黑莓XML解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆