xml_解析_Jsoup_快速入门
快速入门:
1.导入jar包:
2.获取Document对象:
3.获取对应的标签Element对象:
4.获取数据
public class JsoupDemo1 { public static void main(String[] args) throws IOException { //2.获取Document对象 //2.1获取student.xml的path String path = JsoupDemo1.class.getClassLoader().getResource("student.xml").getPath(); //2.2解析xml文档,加载文档今内存,获取dom树-->Document Document document = Jsoup.parse(new File(path), "utf-8"); //3.获取元素对象 Element Elements elements = document.getElementsByTag("name"); System.out.println(elements.size()); //3.1获取第一个name的Element对象 Element element = elements.get(0); //3.2获取护具 String name = element.text(); System.out.println(name); } }
xml_解析_Jsoup_Jsoup对象
对象的使用:
1.Jsoup:工具类,可以解析html或xml文档,返回Document
parse:解析html或xml文档,返回Document
parse(File in,String charsetName):解析xml或html文件的
parse(String html):解析xml或html字符串
parse(URL url,int timeoutMillis):通过网络路径获取指定的html或xml的文档对象
public class JsoupDemo02 { public static void main(String[] args) throws IOException { //获取Document对象,根据xml文档获取 //获取student.xml的path String path = JsoupDemo02.class.getClassLoader().getResource("student1.xml").getPath(); //解析xml文档,加载文档进内存,获取dom树—Document Document document = Jsoup.parse(new File(path), "utf-8"); //2、parse(String html):解析xml或html字符串 String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + "\n" + "<students>\n" + "\t<student number=\"aaa_0001\">\n" + "\t\t<name>张三</name>\n" + "\t\t<age>23</age>\n" + "\t\t<sex>male</sex>\n" + "\t</student>\n" + "\t<student number=\"aaa_0002\">\n" + "\t\t<name>李四</name>\n" + "\t\t<age>31</age>\n" + "\t\t<sex>female</sex>\n" + "\t</student>\n" + "</students>"; Document parse = Jsoup.parse(str); System.out.println(parse); //parse(URL url,int timeoutMillis):通过网络路径获取指定的html或xml的文档对象 URL url = new URL("https://baike.baidu.com/item/jsoup/9012509?fr=aladdin"); Document parse1 = Jsoup.parse(url, 10000); System.out.println(parse1); } }
2.Document:文档对象,代表内存中的dom树
3.Element:元素Element对象的集合,可以当做ArrayList<Element>来使用
4.Node:节点对象
标签:xml,入门,parse,Jsoup,html,Document,String From: https://www.cnblogs.com/qihaokuan/p/16933379.html