获取Element对象
getElementById(string id)∶根据id属性值获取唯一的element对象
getElementsByTag(string tagName):根据标签名称获取元素对象集合
getElementsByAttribute(string key):根据属性名称获取元秦对象集合
getElementsByAttributevalue(string key,string value):根据对应的属性名和属性值获取元秦对象集合
public static void main(String[] args) throws IOException { String path = JsoupDemo3.class.getClassLoader().getResource("student.xml").getPath(); Document document = Jsoup.parse(new File(path), "utf-8"); Elements elements = document.getElementsByTag("student"); System.out.println(elements); System.out.println("--------"); //获取属性名为id的元数对象 Elements id = document.getElementsByAttribute("id"); System.out.println(id); //获取 Elements elementsByAttributeValue = document.getElementsByAttributeValue("number", "heima_0001"); System.out.println(elementsByAttributeValue); Element example = document.getElementById("example"); System.out.println(example); }
xml_解析_jsoup_Element对象
Elements:元素Element对像的集合,可以当做ArrayList<Element>来使用
Element:元素对象
getElementById(string id)∶根据id属性值获取唯一的element对象
getElementsByTag(string tagName):根据标签名称获取元素对象集合
getElementsByAttribute(string key):根据属性名称获取元秦对象集合
getElementsByAttributevalue(string key,string value):根据对应的属性名和属性值获取元秦对象集合
获取属性值
String attr(String key) :根据属性名获取属性值
获取文本内容
string text():获取文本内容
string html():获取标签体的所有内容(包括字标签的字符串内容)
node:结点对象
是Document和Element的父类
public static void main(String[] args) throws IOException { String path = JsoupDemo4.class.getClassLoader().getResource("student.xml").getPath(); Document document = Jsoup.parse(new File(path), "utf-8"); Elements name = document.getElementsByTag("name"); System.out.println(name.size()); Element student = document.getElementsByTag("student").get(0); Elements name1 = student.getElementsByTag("name"); System.out.println(name1.size()); //获取student对象的属性值 String number = student.attr("NUMBER"); System.out.println(number); String text = name1.text(); String html = name1.html(); System.out.println(text); System.out.println(html); }
浙公网安备 33010602011771号