SAX XML Parsing in android?

I wouldn't save them in a vector necessarily only if you really have to for a special purpose. I'd save them in a HashMap instead so you can reference them by their keys instead I need to see your XML structure to help you with an in depth answer EDIT: Here is your answer. Late but still Given your XML structure looks like this?

Xml version="1.0" encoding="UTF-8"? > Radio outlineMap; private HashMap outlineData; private String key; public void startDocument() throws SAXException { outlineMap = new HashMap(); } public void endDocument() throws SAXException { AnotherClass. SetHashMap(outlineMap); } public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { if(qName.

EqualsIgnoreCase("OUTLINE")) { outlineData = new HashMap(); key = atts. GetValue("key"); outlineData. Put("type", atts.

GetValue("type")); outlineData. Put("text", atts. GetValue("text")); outlineData.

Put("URL", atts. GetValue("URL")); } } public void endElement(String uri, String localName, String qName) throws SAXException { if(qName. EqualsIgnoreCase("OUTLINE")) { outlineMap.

Put(key, outlineData); } } } The HashMap outlineMap will hold all your outline entries and the HashMap outlineData will hold the attributes in every single outline tag. The String key is defined because we need it to get the keys and set them correctly for every outlineData As you can see outlineMap is always declared in the startDocument() method this way we ensure that every time your parse using this handler you will have an empty new HashMap In the startElement() method we check if the qualified name of the tag equals to OUTLINE while ignoring the case of it. If this tag occurs we declare a new HashMap so we can hold each attribute set of every outline tag.

Then we assign a value to our key string by parsing the key value of the attribute key of the outline tag. Then we pass every other interesting attribute to our outlineData HashMap using the put() method. This method accepts only a string as a key and a string as a value by our definition Now we move on to our endElement() method which also checks for the occurrence of OUTLINE again ignoring the case.

If it occurs we set the content of your first outlineMap entry by setting the key string from earlier as the key and the outlineData HashMap as the value. Here our HashMap accepts a string as its key and an object as its value (it accepts virtually everything as its value as everything in Java is an object in fact) And now you have your ready to use HashMap filled with the parsed data In my example I pass our final HashMap outlineMap to a setter in another class in the endDocument() method. That means when the parsing is done Here is a short explanation how the SAX parser uses this methods so you have a better clue of whats happening ON DOCUMENT START startDocument() gets called ON STARTING TAG : or startElement() gets called when a starting tag appears.

Here you can decide which tag to look at and which attributes to parse. ON INNERTAG DATA : DATA characters() gets called building a char array of characters. ON END TAG : endElement() gets called when the ending tag appears.

ON DOCUMENT END endDocument() gets called Of course there are several other methods available but for you this methods are the more interesting ones right now. The characters method might not be that interesting for your actual needs tho I hope it helps. If you need to know more just ask by comment.

I wouldn't save them in a vector necessarily only if you really have to for a special purpose. I'd save them in a HashMap instead so you can reference them by their keys instead. I need to see your XML structure to help you with an in depth answer.

EDIT: Here is your answer. Late but still. Given your XML structure looks like this.

Radio To get all the data you need parsed into a useful HashMap the Handler could look like this. Import java.util. HashMap; import org.xml.sax.

Attributes; import org.xml.sax. SAXException; import org.xml.sax.helpers. DefaultHandler; public class MyContentHandler extends DefaultHandler { private HashMap outlineMap; private HashMap outlineData; private String key; public void startDocument() throws SAXException { outlineMap = new HashMap(); } public void endDocument() throws SAXException { AnotherClass.

SetHashMap(outlineMap); } public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { if(qName. EqualsIgnoreCase("OUTLINE")) { outlineData = new HashMap(); key = atts. GetValue("key"); outlineData.

Put("type", atts. GetValue("type")); outlineData. Put("text", atts.

GetValue("text")); outlineData. Put("URL", atts. GetValue("URL")); } } public void endElement(String uri, String localName, String qName) throws SAXException { if(qName.

EqualsIgnoreCase("OUTLINE")) { outlineMap. Put(key, outlineData); } } } The HashMap outlineMap will hold all your outline entries and the HashMap outlineData will hold the attributes in every single outline tag. The String key is defined because we need it to get the keys and set them correctly for every outlineData.As you can see outlineMap is always declared in the startDocument() method this way we ensure that every time your parse using this handler you will have an empty new HashMap.

In the startElement() method we check if the qualified name of the tag equals to OUTLINE while ignoring the case of it. If this tag occurs we declare a new HashMap so we can hold each attribute set of every outline tag. Then we assign a value to our key string by parsing the key value of the attribute key of the outline tag.

Then we pass every other interesting attribute to our outlineData HashMap using the put() method. This method accepts only a string as a key and a string as a value by our definition. Now we move on to our endElement() method which also checks for the occurrence of OUTLINE again ignoring the case.

If it occurs we set the content of your first outlineMap entry by setting the key string from earlier as the key and the outlineData HashMap as the value. Here our HashMap accepts a string as its key and an object as its value (it accepts virtually everything as its value as everything in Java is an object in fact). And now you have your ready to use HashMap filled with the parsed data.

In my example I pass our final HashMap outlineMap to a setter in another class in the endDocument() method. That means when the parsing is done. Here is a short explanation how the SAX parser uses this methods so you have a better clue of whats happening.

ON DOCUMENT START startDocument() gets called ON STARTING TAG : or startElement() gets called when a starting tag appears. Here you can decide which tag to look at and which attributes to parse. ON INNERTAG DATA : DATA characters() gets called building a char array of characters.

ON END TAG : endElement() gets called when the ending tag appears. ON DOCUMENT END endDocument() gets called Of course there are several other methods available but for you this methods are the more interesting ones right now. The characters method might not be that interesting for your actual needs tho.

I hope it helps. If you need to know more just ask by comment.

This is Generic xml parser go to the link androidosbeginning.blogspot.com/2010/09/... hope this will solve the issue you are facing.

Replaced by the XMLReader. And interface as well as supporting new methods. A variety of input sources.

Files, URLs, and SAX InputSources.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions