jsoup
工具类,可以解析xml或html文档,返回document
//1.获取student.xml的path String path = JsoupDemo02.class.getClassLoader().getResource("student.xml").getPath(); //2.解析xml文档,加载文档进内存,获取dom树 Document document = Jsoup.parse(new File(path), "utf-8"); System.out.println(document); //2.parse (String html):解析xml或html字符串 String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + "\n" + "<students>\n" + " <student number=\"s001\">\n" + " <name>zs</name>\n" + " <age>23</age>\n" + " <sex>male</sex>\n" + " </student>\n" + " \n" + "</students>"; Document document1 = Jsoup.parse(str); System.out.println(document1);
Document
代表内存中的dom树
获取Element对象
getElementById(string id)∶根据id属性值获取唯一的element对象
getElementsByTag(string tagNarle):根据标签名称获取元秦对象集合
getElementsByAttribute(String key)∶根据属性名称获取元秦对象集合
getElementsByAttributevalue(string key,string value):根据对应的属性名和属性值获取元秦对象集合
package xml.jsoup; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; import java.io.File; import java.io.IOException; public class JsoupDemo03 { public static void main(String[] args) throws IOException { //1。获取stuent.xml的path String path = JsoupDemo02.class.getClassLoader().getResource("student.xml").getPath(); //2.获取document对象 Document document = Jsoup.parse(new File(path), "utf-8"); //3.获取元素对象,获取所有student对象 Elements elements = document.getElementsByTag("student"); System.out.println(elements); System.out.println("------"); //4.获取属型名为id的元素对象们 Elements elementsByAttribute = document.getElementsByAttribute("007"); System.out.println(elementsByAttribute); System.out.println("------"); //5.获取number属型为s001的对象们 Elements elementsByAttributeValue = document.getElementsByAttributeValue("number", "s001"); System.out.println(elementsByAttributeValue); } }
elements
元素Element对象的集合。可以当做ArrayList来使用
element
1.获取子元素对象
getElementById(string id)∶根据id属性值获取唯一的element对象
getElementsByTag(string tagNarle):根据标签名称获取元秦对象集合
getElementsByAttribute(String key)∶根据属性名称获取元秦对象集合
getElementsByAttributevalue(string key,string value):根据对应的属性名和属性值获取元秦对象集合
2.获取属型值
string attr(string key):根据属性名称获取属性值
3.获取文本内容
string text():获取文本内容
string html():获取标签体的所有内容(包括字标签的字符串内容)
<?xml version="1.0" encoding="UTF-8" ?> <students> <student number="s001"> <name id="007">zs</name> <age>23</age> <sex>male</sex> </student> <student number="s002"> <name>zs</name> <age>23</age> <sex>male</sex> </student> </students>
package xml.jsoup; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.File; import java.io.IOException; public class JsoupDemo04 { public static void main(String[] args) throws IOException { //1。获取stuent.xml的path String path = JsoupDemo02.class.getClassLoader().getResource("student.xml").getPath(); //2.获取document对象 Document document = Jsoup.parse(new File(path), "utf-8"); //通过Document对象获取name标签,获取的是所有name标签,可以获取到两个 Elements name = document.getElementsByTag("name"); System.out.println(name.size()); System.out.println("-------------"); //通过Element对象获取子标签对象 Element student0 = document.getElementsByTag("student").get(0); Elements name1 = student0.getElementsByTag("name"); System.out.println(name1.size()); //获取student对象的属型值 String number = student0.attr("number"); System.out.println(number);//s001 //获取文本内容 String text = name1.text();//获取的是纯文本 String html = name1.html(); System.out.println(html);//所有标签体的内容 System.out.println(text); } }
node
是Document和Element的父类
快捷查询方式
selector选择器
使用的方法:Elements
select( string cssQuery)
语法:参考selector类中定义的语法
XPath
package xml.jsoup; import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException; import cn.wanghaomiao.xpath.model.JXDocument; import cn.wanghaomiao.xpath.model.JXNode; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import java.io.File; import java.io.IOException; import java.util.List; public class JsoupDemo05 { public static void main(String[] args) throws IOException, XpathSyntaxErrorException{ //1。获取stuent.xml的path String path = JsoupDemo02.class.getClassLoader().getResource("student1.xml").getPath(); //2.获取document对象 Document document = Jsoup.parse(new File(path), "utf-8"); //3.根据document对象,创建xpathdocument对象 JXDocument jxDocument = new JXDocument(document); //4.结合xpath语法查询 List<JXNode> jxNodes = jxDocument.selN("//student/name"); for (JXNode jxNode:jxNodes){ System.out.println(jxNode); } } }