如何SAXParse网站?

时间:2014-02-18 22:29:58

标签: java android parsing xml-parsing saxparser

我是Android开发的新手。我一直在读一本书要学习:首先是Android开发 - Jonathan Simon。我遇到了一个SAXParser实现,虽然代码似乎已经过时,在线教程也已过时或者没有显示如何从网站解析。

SAXParser.java

    package com.example.saxparser;

    import java.io.InputStream;
    import java.net.URL;

    import javax.xml.parsers.SAXParserFactory;

    import org.xml.sax.Attributes;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    import org.xml.sax.XMLReader;
    import org.xml.sax.helpers.DefaultHandler;

    public class SAXParser extends DefaultHandler {
private String url = "http://www.w3schools.com/xml/note.xml";
private boolean inTitle = false;
private boolean inDescription = false;
private boolean inItem = false;
private String title = null;
private StringBuffer description = new StringBuffer();

public void processFeed() {
    try {
        SAXParserFactory factory =
                SAXParserFactory.newInstance();
        javax.xml.parsers.SAXParser parser = factory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        reader.setContentHandler(this);
        InputStream inputStream = new URL(url).openStream();
        reader.parse(new InputSource(inputStream));
    } catch (Exception e) {}
}

public void startElements(String url, String localName, String qName, Attributes attributes) throws SAXException {
    if (localName.startsWith("to")) { inItem = true; }
    else if (inItem) {
        if (localName.equals("from")) { inTitle = true; }
        else { inTitle = false; }

        if (localName.equals("heading")) { inDescription = true; }
        else { inDescription = false; }
    }
}

public void characters(char ch[], int start, int length) {
    String chars = new String(ch).substring(start, start + length);
    if (inTitle && title == null) { title = chars; }
    if (inDescription) { description.append(chars); }
}

public String getTitle() { return title; }
public StringBuffer getDescription() { return description; }
 }

MainActivity.java

 package com.example.saxparser;

 import android.os.Bundle;
 import android.app.Activity;
 import android.view.Menu;
 import android.widget.TextView;

 public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SAXParser handler = new SAXParser();
    handler.processFeed();

    resetDisplay(handler.getTitle(), handler.getDescription());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void resetDisplay(String title, StringBuffer stringBuffer) {
    TextView titleView = (TextView) findViewById(R.id.imageTitle);
    titleView.setText(title);
    TextView descriptionView = (TextView) findViewById(R.id.imageDescription);
    descriptionView.setText(stringBuffer);
}

}

1 个答案:

答案 0 :(得分:0)

我也是Android开发领域的新手,到目前为止我知道SAX可以用于XML文件解析器,但我不确定你是否可以解析整个HTML。请记住,尽管HTML是SGML的一个子集(因此可以认为它在SAX中使用相同的方法进行解析),但它可能很棘手(实际上XML可以被认为是HTML的表兄和SGML的侄子) )。你可能会发现自己在动态加载东西方面遇到麻烦(正如Dave Newton所说)。也许如果你再详细解释一下你的目标,我们都可以给你一个更详细的解释/建议(或者更深入地了解Pity提出的建议),例如......是否提供你要解析的提供XML API的网站?我想这不是因为您考虑解析HTML而不是解析XML API响应。请提供更多信息。