更新时间:2019年01月10日13时56分 来源:银河网络网站是什么 浏览次数:
01 02 03 04 05 06 07 08 09 10 11 12 13 | //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); |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | //1.获取student.xml的path String path = JsoupDemo6. class .getClassLoader().getResource( "student.xml" ).getPath(); //2.获取Document对象 Document document = Jsoup.parse( new File(path), "utf-8" ); //3.根据document对象,创建JXDocument对象 JXDocument jxDocument = new JXDocument(document); //4.结合xpath语法查询 //4.1查询所有student标签 List<JXNode> jxNodes = jxDocument.selN( "//student" ); for (JXNode jxNode : jxNodes) { System.out.println(jxNode); } System.out.println( "--------------------" ); //4.2查询所有student标签下的name标签 List<JXNode> jxNodes2 = jxDocument.selN( "//student/name" ); for (JXNode jxNode : jxNodes2) { System.out.println(jxNode); } System.out.println( "--------------------" ); //4.3查询student标签下带有id属性的name标签 List<JXNode> jxNodes3 = jxDocument.selN( "//student/name[@id]" ); for (JXNode jxNode : jxNodes3) { System.out.println(jxNode); } System.out.println( "--------------------" ); //4.4查询student标签下带有id属性的name标签 并且id属性值为itcast List<JXNode> jxNodes4 = jxDocument.selN( "//student/name[@id='itcast']" ); for (JXNode jxNode : jxNodes4) { System.out.println(jxNode); } |