使用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());
                }
            }
        }
    }
}

運行結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM