SAX XML Parsing in android

2020-04-13 17:09发布

问题:

XML Code is

  <?xml version="1.0" encoding="UTF-8" ?> 
  <opml version="1">
  <head>
  <title>Radio</title> 
  <status>200</status> 
  </head>
  <body>
  <outline type="link" text="Local" URL="http://..............." key="local" /> 
  <outline type="link" text="Music" URL="http://.............." key="music" /> 
  <outline type="link" text="walk" URL="http://...................." key="walk" /> 
  <outline type="link" text="Sports" URL="http://..........." key="sports" /> 
  <outline type="link" text="Place" URL="http://..............." key="Place" /> 
  <outline type="link" text="Verbal" URL="http://............." key="Verbal" /> 
  <outline type="link" text="Podcasts" URL="http://....................." key="podcast" /> 
  </body>
  </opml>

回答1:

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" ?>
<opml version="1">
    <head>
        <title>Radio</title>
    </head>
    <body>
        <outline type="link" text="Local" URL="http://google.at" key="local" />
        <outline type="link" text="Music" URL="http://google.at" key="music" />
        <outline type="link" text="walk" URL="http://google.at" key="walk" />
        <outline type="link" text="Sports" URL="http://google.at" key="sports" />
        <outline type="link" text="Place" URL="http://google.at" key="Place" />
        <outline type="link" text="Verbal" URL="http://google.at" key="Verbal" />
        <outline type="link" text="Podcasts" URL="http://google.at" key="podcast" />
    </body>
</opml>

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<String, Object> outlineMap;
    private HashMap<String, String> outlineData;
    private String key;

    public void startDocument() throws SAXException {
        outlineMap = new HashMap<String, Object>();
    }

    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<String, String>();
            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 : <TAG> or <TAG attribute="123">
    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 : </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.



回答2:

This is Generic xml parser

go to the link http://androidosbeginning.blogspot.com/2010/09/generic-xml-parsing-in-android.html

hope this will solve the issue you are facing.



回答3:

SAX: Simple xml parsing. It parses node by node Traversing is from top to bottom Low memory usage Back navigation is not possible with sax.

//implementing required handlers 
 public class SaxParse extends DefaultHandler{      }      
//new instance of saxParserFactory      
SAXParserFactory factory=SAXParserFactory.newInstance();
//NEW INSTANCE OF SAX PARSER      
SAXParser saxparser=factory.newSAXParser();      
//Parsing xml document
SAXParser.parse(new File(file to be parsed), new SAXXMLParserImpl());