标签:配置 选择器 exception btn 网络 java 读取 获取元素 选择
快速入门:
<?xml version='1.0' ?>
<users>
<user id='1'>
<name>zhangsan</name>
<age>23</age>
<gender>male</gender>
<br/>
</user>
<user id='2'>
<name>lisi</name>
<age>24</age>
<gender>female</gender>
</user>
</users>
组成部分:
<?xml 属性列表 ?>
指令(了解):结合css的,展示数据
<?xml-stylesheet type="text/css" href="a.css" ?>
<![CDATA[ 数据内容 ]]>
约束:规定xml文档的书写规则
DTD:一种简单的约束技术,有缺陷,属性限定不全面
<!DOCTYPE 根标签名 SYSTEM "dtd文件的位置">
<!DOCTYPE 根标签名 PUBLIC "dtd文件名字" "dtd文件的位置URL">
dtd文件内容
<!ELEMENT students (student*) >
<!ELEMENT student (name,age,sex)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT sex (#PCDATA)>
<!ATTLIST student number ID #REQUIRED>
XML引入内部dtd
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE students SYSTEM "student.dtd">
<students>
<student number="0001">
<name>tom</name>
<age>18</age>
<sex>male</sex>
</student>
</students>
引入:
1.填写xml文档的根元素
2.引入xsi前缀. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3.引入xsd文件命名空间. xsi:schemaLocation="xxx/student.xsd"
4.为每一个xsd约束声明一个前缀(用来区分多个xsd文档),作为标识 xmlns="xxx/xml"(xmlns:a="xxx/xml")
<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="xxx/xml"
xsi:schemaLocation="xxx/tudent.xsd">
//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对象,elements继承ArrayList
Element element = elements.get(0);
//3.2获取数据
String name = element.text();
System.out.println(name);
import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.net.URL;
public class JsoupDemo01 {
public static void main(String[] args) throws IOException, XpathSyntaxErrorException {
String path= JsoupDemo01.class.getClassLoader().getResource("").getPath();
URL url= new URL("https://www.baidu.com/");
Document document = Jsoup.parse(url,10000);
Elements tag = document.getElementsByTag("map");
Elements btn = document.getElementsByAttributeValue("type","submit");
String text = btn.attr("value");
System.out.println(btn);
System.out.println("-------------");
System.out.println(text);
System.out.println("-------------");
}
}
代码:
//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属性值为pp
List<JXNode> jxNodes4 = jxDocument.selN("//student/name[@id='sex']");
for (JXNode jxNode : jxNodes4) {
System.out.println(jxNode);
}
标签:配置 选择器 exception btn 网络 java 读取 获取元素 选择
原文地址:https://www.cnblogs.com/huxiaobai/p/12129447.html