Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
Posts: 8
Registered: ‎02-29-2012
My Device: 9800 0S 6
My Carrier: bell
Accepted Solution

Xml parsing using sax parser

Hi....

I would like to post this example to help pasring xml or live feed from a URL.

 





SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();


DefaultHandler handler = new DefaultHandler() {




public void startElement(String uri, String localName,String qName,
Attributes attributes) throws SAXException {
if(qName.equalsIgnoreCase(" an element from xml"))
{

vector.addElement(attributes.getValue("attribute of an element"));


}

else if(qName.equalsIgnoreCase("second element fro xml"))
{

attributteValue= attributes.getValue("title");



}

public void endElement(String uri, String localName, String qName)
throws SAXException {


currentElement = false;


if (qName.equalsIgnoreCase("element end"))
{
//do something
}

 

}



};
StreamConnection s = null;

s=(StreamConnection)Connector.open("Enter the url that provides xml file");
HttpConnection httpConn = (HttpConnection)s;

if( httpConn.getResponseCode()==400)

{
Dialog.alert("noaccess to internett");

}




InputStream input = null;
input = s.openInputStream();


Reader reader = new InputStreamReader(input,"UTF-8");
InputSource is = new InputSource(reader);
is.setEncoding("UTF-8");

saxParser.parse(is, handler);
}

 

 

 

add the required elements from xml file during parsing to vectors and do something.

 

 

SAX (Simple API for XML) is an event-based sequential access parser API developed by the XML-DEV mailing list for XML documents. SAX provides a mechanism for reading data from an XML document that is an alternative to that provided by the Document Object Model (DOM). Where the DOM operates on the document as a whole, SAX parsers operate on each piece of the XML document sequentially.

 

SAX parsers have some benefits over DOM-style parsers. A SAX parser only needs to report each parsing event as it happens, and normally discards almost all of that information once reported (it does, however, keep some things, for example a list of all elements that have not been closed yet, in order to catch later errors such as end-tags in the wrong order). Thus, the minimum memory required for a SAX parser is proportional to the maximum depth of the XML file (i.e., of the XML tree) and the maximum data involved in a single XML event (such as the name and attributes of a single start-tag, or the content of a processing instruction, etc).

This much memory is usually considered negligible. A DOM parser, in contrast, typically builds a tree representation of the entire document in memory to begin with, thus using memory that increases with the entire document length. This takes considerable time and space for large documents (memory allocation and data-structure construction take time). The compensating advantage, of course, is that once loaded any part of the document can be accessed in any order.

 

I hope this post was useful.



 

 

 

Highlighted
Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Xml parsing using sax parser

welcome to the support forums.

thank you for contributing. there is a minor issue though: all blocking operations should be done on a separate thread. you cannot use Dialog.alert on a thread without using invokelater or syncing on the eventlock, you should correct that.

in addition there are a lot of response codes, and you should check for 200 to continue, otherwise trigger an error.

there is also an xmldemo in the samples that ship with the eclipse plugin or JDE, but it uses DocumentBuilder, not Sax.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter