{
//url for parsing
URL url = new URL("Your URL");
//reading the data at the specified url
//For the local file
//input = getClass().getResourceAsStream("/res/raw/your.xml");
input = url.openStream();
//Creating object for SAXPARSER FACTORY
SAXParserFactory spf = SAXParserFactory.newInstance();
//CREATION OF OBJECT FOR SAXPARSER
SAXParser sp = spf.newSAXParser();
//READING AND PARSING
XMLReader xr = sp.getXMLReader();
SaxHandler myXMLHandler = new SaxHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(input));
}
catch(Exception e)
{
e.printStackTrace();
}
SaxHandler.java
public class SaxHandler extends DefaultHandler
{
Boolean currentElement = false;
String currentValue = null;
NewsItem objNewsItem;
boolean test_Des = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
currentValue = "";
currentElement = true;
if (localName.equalsIgnoreCase("START TAB"))
{
//VECTOR CREATION
//OBJECT CREATION
}
}
@Override
public void endElement(String uri, String localName, String qName)throws SAXException
{
//HERE YOU HAVE TO INSERT TAG NAMES THAT YOU HAVE TO READ
currentElement = false;
if (localName.equalsIgnoreCase("TAG NAME"))
{
VAR = currentValue;
}
else if(localName.equalsIgnoreCase("TAG NAME"))
{
VAR = currentValue;
}
else if(localName.equalsIgnoreCase("TAG NAME"))
{
YOURVECTOR.add(objNewsItem);
}
}
@Override
public void characters(char[] ch, int start, int length)throws SAXException
{
if (currentElement)
{
currentValue += new String(ch, start, length);
}
}
}
No comments:
Post a Comment