user469999
user469999

Reputation: 2161

Parse XML using SAX parser in BlackBerry

I am looking for a SAX parser code in blackberry for parsing a XML document, retrieve the XML document and update the XML document. I am new to the SAX parser.

My XML string is of following type:

<users>
   <user  uid = "1"  dispname="Yogesh C" statid="1" statmsg="1">Yogesh Chaudhari</user>
</users>

I have to parse above string with SAX parser.

Upvotes: 0

Views: 3026

Answers (1)

Ray Vahey
Ray Vahey

Reputation: 3065

Your series of questions on this subject seem to add up to: Can I have code for a readable/writable database that uses XML formatted files on the SDCard for storage?

That's more than I'm qualified to answer, but here is working sample code that I used when I was testing XML on BB. Hopefully this will get you started, and can I have the database code when you're finished? ;)

public class XmlManager {

    protected Document document;

    //protected Log log = new Log("XmlManager");

    public XmlManager(String file) {
       FileConnection fc = null;
       InputStream inputStream = null;
       try {
           DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
           DocumentBuilder builder = factory.newDocumentBuilder();
           fc = (FileConnection)Connector.open(file, Connector.READ);
           inputStream = fc.openInputStream();  
           document = builder.parse( inputStream );
       } catch (Exception e) {
           //log.error("builder.parse", e);   
       } finally {
           try {
               if (fc!=null) fc.close();
               if (inputStream!=null) inputStream.close();
           } catch (Exception e) {
               //log.error("close", e);
           } 
       }
    }

    public String readApiString(String tag) {       
        Element root=document.getDocumentElement();
        NodeList list = root.getElementsByTagName(tag);
        return(list.item(0).getFirstChild().getNodeValue());
    }

example xml:

<myuniquetagname>foo</myuniquetagname>

example usage:

XmlManager xmlManager = new XmlManager("file:///SDCard/BlackBerry/documents/myfile.xml");
String foo = xmlManager.readApiString("myuniquetagname");

Upvotes: 1

Related Questions