XML解析-jsoup,报空指针异常问题

问题
路径名中出现了空格,在编译的时候,编译器会讲空格编译成%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>

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值