package cn.itcast.xml.jsoup; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import java.io.File; import java.io.IOException; import java.net.URL; /** * Jsoup对象功能 */ public class JsoupDemo2 { public static void main(String[] args) throws IOException { String path = JsoupDemo2.class.getClassLoader().getResource("student1.xml").getPath(); /* 1. Jsoup:工具类,可以解析html或xml文档,返回Document * parse:解析html或xml文档,返回Document */ //1.1 parse(File in, String charsetName):解析xml或html文件的。 Document document = Jsoup.parse(new File(path), "UTF-8"); System.out.println(document);//打印xml的文件内容 //1.2 parse(String html):解析xml或html字符串 String xml = "\n" + "\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " zhangsan\n" + " \n" + " \n" + " abc\n" + " \n" + " \n" + " hehe\n" + " \n" + " \n" + " \n" + " \n" + " lisi\n" + " \n" + " \n" + " 24\n" + " \n" + " \n" + " female\n" + " \n" + " \n" + " \n" + " \n" + ""; Document document2 = Jsoup.parse(xml); System.out.println(document2); //1.3 parse(URL url, int timeoutMillis):通过网络路径获取指定的html或xml的文档对象 URL url = new URL("https://www.baidu.com/baidu?tn=34046034_10_dg&ie=utf-8&wd=csdn");//代表网路中的一个资源路径 Document document3 = Jsoup.parse(url, 3000); System.out.println(document3);//解析成HTML文档 } }