问题
路径名中出现了空格,在编译的时候,编译器会讲空格编译成%20,从而无法得到正确的路径,或者空指针异常
解决办法JsoupDemo01.class.getClassLoader().getResource(“student.xml”).toURI().getPath();在getPath()前面加上toURI方法,讲空格重新转换回来。
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
public class JsoupDemo01 {
public static void main(String[] args) throws Exception {
//获取student.xml的path
String path = JsoupDemo01.class.getClassLoader().getResource("student.xml").toURI().getPath();
System.out.println(path);
//获取Document对象
Document document = Jsoup.parse(new File(path), "utf-8");
//通过Document对象获取name标签,获取所有的name标签
Elements elements = document.getElementsByTag("name");
System.out.println(elements.size());
//通过Elements对象获取子标签对象
Element student = document.getElementsByTag("student").get(0);
Elements name = student.getElementsByTag("name");
System.out.println(name.size());
String attr = student.attr("1");
System.out.println(attr);
}
}
xml
<?xml version="1.0" encoding="UTF-8" ?>
<students>
<student number="1">
<name>tom</name>
<age>18</age>
<sex>man</sex>
</student>
<student id="2">
<name>jack</name>
<age>18</age>
<sex>women</sex>
</student>
</students>
导包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Java Web</groupId>
<artifactId>Java Web</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Java Web Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- jsoup HTML parser library @ https://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
</dependencies>
<build>
<finalName>Java Web</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>