Parse XML & Retrieve Info Several layers of Nodes Deep Java/Android

后端 未结 2 1343
梦毁少年i
梦毁少年i 2021-01-28 01:07

I am working from an example provided by my professor which gets data from a weather forecast site and parses the XML file to show the weather conditions in a list. My program

相关标签:
2条回答
  • 2021-01-28 01:42

    Use SAX and only pay attention to the html_instructions tag. Your handler will be called with startElement() for each element and is passed in the element's name. Compare that name to "html_instructions". When you have a match, pay attention to all processed nodes until the corresponding endElement() call.

    0 讨论(0)
  • 2021-01-28 01:49

    So basically using the SAX parser is a good choice for you (it is fast, allows you to filter out all the unnecessary data, consumes low memory). When working with SAX for the first time, you may find the following example useful. I do not say the code is perfect (it misses e.g. exception handling, safe stream closing, etc.), but it could be a good start point for you.

    
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    
    public class Test {
    
      private static final String HTML_INSTRUCTIONS = "html_instructions";
    
      public static void main(String[] args) throws Exception {
        final List htmlInstructions = new ArrayList();
    
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        DefaultHandler dh = new DefaultHandler() {
          private boolean isHtmlInstructions = false;
          private StringBuilder sb = new StringBuilder();
          @Override
          public void startElement(String uri, String localName, String name,
              Attributes attributes) throws SAXException {
            super.startElement(uri, localName, name, attributes);
            if (HTML_INSTRUCTIONS.equals(name)) {
              isHtmlInstructions = true;
            }
          }
    
          @Override
          public void characters(char ch[], int start, int length)
          throws SAXException {
            if (isHtmlInstructions) {
              sb.append(ch, start, length);
            }
          }
    
          @Override
          public void endElement(String uri, String localName, String name)
              throws SAXException {
            super.endElement(uri, localName, name);
            if (HTML_INSTRUCTIONS.equals(name)) {
              htmlInstructions.add(sb.toString());
              sb.delete(0, sb.length());
              isHtmlInstructions = false;
            }
          }
        };
    
        InputStream is = new FileInputStream("test.xml");
        sp.parse(is, dh);
        for (String htmlInstruction : htmlInstructions) {
          System.out.println(htmlInstruction);
        }
    
      }
    
    }
    
    

    The output should look like this:

    
    Head <b>east on <b>S Street Viaduct</b>
    
    
    0 讨论(0)
提交回复
热议问题