【问题标题】:XML parsing using SAX for Blackberry使用 SAX for Blackberry 解析 XML
【发布时间】:2012-08-10 12:36:24
【问题描述】:

我想解析以下 xml 文件:

<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>

谁能帮帮我?给我一些示例代码来使用 SAX 解析器解析它。我想从 httpConnection 方法获取这个文件。我是 BB 开发的新手。

【问题讨论】:

标签: blackberry


【解决方案1】:

您可以使用 Dom 和 Sax 解析器进行 XML 解析。

有一个代码 sn-p 用于从 HTTP 请求调用 Xml,而不是使用 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,并在每个 XML 标记的哈希表中为您提供向量 vec 数据。

【讨论】:

  • 嗨,shashank,当我使用上面的代码解析我的 xml 文件时,我没有得到向量上的最后一个节点值,也没有得到相应地址节点的 id 值,你能给我它的详细代码吗
  • 您是否根据您的 XML 更改了地址和登录名?您需要更改以获取其中的值。
猜你喜欢
  • 2011-05-31
  • 2012-08-27
  • 2012-01-11
  • 1970-01-01
  • 2011-04-30
  • 2011-06-17
  • 2017-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多