Created
February 25, 2016 06:51
-
-
Save minustore/1efa4bf63427be6d0e94 to your computer and use it in GitHub Desktop.
parser html href use xmlpullparser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.xmlpull.v1.XmlPullParser; | |
import org.xmlpull.v1.XmlPullParserException; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by John on 2016/2/25. | |
*/ | |
public class HtmlXmlParser { | |
public List<Href> readHtml(XmlPullParser parser) throws XmlPullParserException, IOException { | |
List<Href> list = new ArrayList<Href>(); | |
//skip first tag | |
while(parser.next() != XmlPullParser.END_DOCUMENT){ | |
if(parser.getEventType() == XmlPullParser.START_TAG ) { // only get Start tag: <a href="xxx"> | |
String tagname = parser.getName(); | |
if (tagname.equals("a")) { | |
String AttributeValue = parser.getAttributeValue(0); | |
String text = parser.nextText(); | |
list.add(new Href(AttributeValue, text)); // return a list | |
} | |
} | |
} | |
return list; | |
} | |
public static class Href { | |
public final String link; | |
public final String name; | |
private Href(String link, String name) { | |
this.link = link; | |
this.name = name; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment