Start a Project

XML parsing using SAX parser in android

Extensible Markup Language (XML) is a set of rules for encoding documents in machine-readable form. XML is a popular format for sharing data on the internet. In android, you can get XML in response from a website and then you have to traverse the XML in order to get required data. XML can be parsed by many ways forexample XML pull parser and SAX parser, DOM parser etc.

SAX and pull parser are event based approach while DOM parser is object based approach. But Sax is used when we want to paese the whole XML.

SAX Parser:

Firstly you shuld have a xml in form of a string. If you dont then you can create so using StringWriter class and TransformerFactory class.

You have to mention the node element( org.w3c.dom.Element) which you want to convert in string and the above code will give you the string including node element.

So now you have your XML in form of a string named XML_string. Thus you can parse the xml as you need using Xml document builder and convert it into DOM document so that traversing can be done to find the required information (tags) .

The string Required_Data will contain the required value of that node. If there are more than one tag with the same name then loop can be applied to get all the values or the required tag values in a List.

Exit mobile version