使用JDOM解析XML文档
一、导入依赖包
第一步:在https://mvnrepository.com/search?q=jdom网站上搜索jdom
第二步:选择相应的版本
第三步:复制dependency内容
第四步:新建工程,并在pom.xml中添加dependency
使用JDOM4J解析XML文档如法炮制,将jdom4j导入到pom.xml中
第五步:查看依赖包是否导入成功
二、示例:
1.配置pom.xml
<?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>com.uos</groupId> <artifactId>handlexml</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>7</source> <target>7</target> </configuration> </plugin> </plugins> </build> <packaging>jar</packaging> <dependencies> <!-- https://mvnrepository.com/artifact/org.jdom/jdom --> <dependency> <groupId>org.jdom</groupId> <artifactId>jdom</artifactId> <version>2.0.2</version> </dependency> <!-- https://mvnrepository.com/artifact/dom4j/dom4j --> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> </dependencies> </project>
2.新建studnet.xml
<?xml version="1.0" encoding="gb2312" ?> <?xml-stylesheet type='text/css' href='students.css'?> <student id="2013010111"> <name>小田</name> <age>22</age> <description><![CDATA[最喜爱的图书《红楼梦》]]></description> </student>
3.新建JDOMRead类
package com.uos.xml; import org.jdom2.*; import org.jdom2.input.SAXBuilder; import java.io.File; import java.io.IOException; import java.util.List; public class JDOMRead { public static void main(String[] args) { //构建解析器,使用SAX解析器 SAXBuilder saxBuilder = new SAXBuilder(); try{ //读取XML文件 Document doc = saxBuilder.build(new File("E:\\JavaProject\\HandleXml\\src\\main\\java\\com\\uos\\xml\\student.xml")); //该路径为需要解析的xml文档的路径 //获取doc的所有内容 List<Content> list = doc.getContent(); //调用readAndPrint方法进行解析 readAndPrint(list); }catch(JDOMException | IOException e){ e.printStackTrace(); } } //使用递归方式解析并显示所有的XML文档内容 private static void readAndPrint(List<Content> list) { for (Content temp:list){ //如果获取的内容是注释 if (temp instanceof Comment){ Comment com = (Comment)temp; System.out.println("<--"+com.getText()+"-->"); // 如果获取的内容是处理指令 }else if (temp instanceof ProcessingInstruction){ ProcessingInstruction pi = (ProcessingInstruction)temp; System.out.println("<?"+pi.getTarget()+""+pi.getData()+"?>"); //如果获取的内容是元素 }else if (temp instanceof Element){ Element elt = (Element)temp; List<Attribute> attrs = elt.getAttributes(); System.out.println("<"+elt.getName()+""); for (Attribute t:attrs){ System.out.println(t.getName()+"=\""+t.getValue()+"\""); } System.out.println(">"); readAndPrint(elt.getContent()); System.out.println("</"+elt.getName()+">"); //如果获取的内容是CDATA }else if (temp instanceof CDATA){ CDATA cdata = (CDATA)temp; System.out.println("<![CDATA["+cdata.getText()+"]]>"); //如果获取的内容是文本 }else if (temp instanceof Text){ Text text = (Text)temp; if (!text.getText().trim().equals("")){ System.out.println(text.getText()); } } } } }
运行结果: