1. 程式人生 > >DOM4J jar包 獲取xml檔案中的標籤內容

DOM4J jar包 獲取xml檔案中的標籤內容

    // 1.匯入DOM4J jar包
        // 建立解析器物件
        SAXReader reader = new SAXReader();
        Document doc = reader.read(new FileInputStream("students.xml"));
        // 獲取根標籤物件
        Element rootElement = doc.getRootElement();
        // 獲取根標籤下的子標籤 預設獲取的是第一個子標籤
        Element stuElement = rootElement.element("student");
        System.out.println(stuElement.getName());
        System.out.println("-------------以下是獲取所有子標籤---------------------");
        // 獲取所有的子標籤
        List<Element> eles = rootElement.elements();
        for (Element ele : eles) {
            System.out.println(ele.getName());

        }
        // 方式三 獲取所有子標籤
        System.out.println("-------------通過迭代器獲取所有子標籤---------------------");
        Iterator<Element> elementIterator = rootElement.elementIterator();
        while (elementIterator.hasNext()) {
            Element element = elementIterator.next();
            System.out.println(element.getName());

        }

        System.out.println("-------------以下是獲取屬性物件--------------------------");
        Element element = rootElement.element("student");
        Attribute attribute = element.attribute("id");
        String value = attribute.getValue();
        String name = attribute.getName();
        System.out.println(name);
        System.out.println(value);
        // 方式2:直接獲取屬性值
        System.out.println("------------------------------------------");
        String value2 = rootElement.element("student").attributeValue("id");
        System.out.println(value2);
        // 方式三:獲取所有的屬性物件
        List<Attribute> attributes = rootElement.element("student").attributes();
        for (Attribute atr : attributes) {
            String name2 = atr.getName();
            String value3 = atr.getValue();
            System.out.println(name2 + "======" + value3);

        }
        System.out.println("---------------通過迭代器獲取所有的屬性物件--------------------------");
        Iterator<Attribute> attributeIterator = rootElement.element("student").attributeIterator();
        while(attributeIterator.hasNext()){
            Attribute attribute2 = attributeIterator.next();
            System.out.println(attribute2.getName()+"=="+attribute2.getValue());
            
            
        }
        
        System.out.println("--------------獲取標籤之間的文字-----------------------");
        //層層往下拿
        String text = doc.getRootElement().element("student").element("name").getText();
        System.out.println(text);
        //方式2:
        String text2 = doc.getRootElement().element("student").elementText("name");
        System.out.println(text2);
        
        

        // 抽取方法獲取所有的節點
        // getNodes(rootElement);
        // 獲取節點物件
        // Node node = doc.node(0);//只能獲取一個子節點

        // System.out.println(node.getName());
        // 獲取所有的節點
        // Iterator<Node> iterator = doc.nodeIterator();
        // while (iterator.hasNext()) {
        // Node nodes = iterator.next();
        // System.out.println(nodes.getName());
        //
        // }

    }

    private static void getNodes(Element ele) {
        System.out.println(ele.getName());
        Iterator<Node> iterator = ele.nodeIterator();
        while (iterator.hasNext()) {
            Node nodes = iterator.next();
            if (nodes instanceof Element) {//
                Element ele2 = (Element) nodes;
                getNodes(ele2);// 遞迴呼叫 方法內部呼叫方法本身 注意遞迴比較耗費資源,因為他要不斷的載入方法進棧記憶體

            }

        }

    }
    
    /*獲取節點物件:1.node() 獲取單個節點物件  2.nodeIterator(); 獲取多個節點物件 只能獲取子節點
     *             2.獲取根標籤物件 getRootElement()
     *             3.Element();獲取第一個子標籤  Elements() 獲取所有的子標籤
     *                 elememtesIterator() 獲取所有子標籤物件
     *             4.attribute() 獲取單個屬性物件  getName()獲取屬性的鍵  getValue()獲取屬性的值
     *                 attributes()獲取所有的屬性物件
     *                 attributeIterator() 獲取所有的屬性物件
     *                 直接獲取屬性物件的值 attributeValue()
     *             5.先獲取到文字所在的標籤物件 通過getText()拿出這個標籤直接的文字
     *                 elementText("name");
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
                */
   

相關推薦

DOM4J jar 獲取xml檔案標籤內容

    // 1.匯入DOM4J jar包         // 建立解析器物件         SAXReader reader = new SAXReader();         Document doc = reader.read(new FileInputStre

快速全方位解決:maven倉庫裡有jar但是pom檔案依賴不到 報錯~

最近新接手了一個專案,引入後調環境,到了maven倉庫這裡,pom檔案一直報錯,但是點進倉庫裡確是有的,著實讓我上火,閒話少敘 進入正題:(是在eclipse中調整的) 報錯: 正常有點經驗的都知道: 點選上面的window->preferences: 選擇使用你自己的maven;

boost::filesystem獲取目錄下的所有.xml檔名,並批量更改.xml檔案內容

第一步,遍歷資料夾並獲取所有.xml檔名,並存入files列表。 #include <boost/filesystem.hpp> namespace fs = boost::filesystem; int get_filenames(const std::s

mapper.xml檔案標籤沒有提示的解決

1、首先我們來看看mapper.xml的標頭檔案 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.

ViewPager動態新增XML佈局檔案,並獲取XML檔案控制元件ID的操作(inflate)

Step 1: 佈局檔案中新增viewpager和切換頁面button <spanstyle="font-size:18px;"><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/a

javaEE Springmvc,properties檔案解決硬編碼問題,@Value註解獲取properties檔案內容

springmvc.xml(Springmvc的核心配置檔案,讀取properties檔案): <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframewor

一種獲取xml檔案某個節點內容的shell方法

配置檔案 config.xml <xml> <server> <name>srv-01</name> </server> <server> <name>srv-02</name> </se

Python3提取xml檔案內容

import xml.dom.minidom def find_child(Par_nodes, mystr): for child_node in Par_nodes: if(len(child_node.childNodes) > 0):

按照一定的規格將XML檔案內容批量匯入至資料庫

一、前臺點選按鈕 <li> asp:Button ID="ImportMember" runat="server" CssClass="btn btn-primary" Text="匯入會員" /> </li> 二、後臺按鈕宣告、編

解析xml檔案標籤內容

package com.huawei.gts.impl.Dao.document; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; impo

shell指令碼獲取配置檔案內容

1、獲取配置檔案中的內容。 步驟是1,2,3. 2.刪除一個檔案中的內容,然後追加自己需要的內容,這裡的先將檔案中的內容刪除,然後新增1234進去。  3.在某行的前一行或後一行新增內容 具休操作如下: #匹配行前加 sed -i '/allow 361

Java:使用DOM4j來實現讀寫XML檔案的屬性和元素

DOM4可以讀取和新增XML檔案的屬性或者元素 讀取屬性: public static void ReadAttributes() throws DocumentException { File file = new File("D:\\cmz\\java\\XMLTest\\Custom

mapper.xml檔案獲取最新插入資料的主鍵

在開發過程中,我們常常用到在插入資料時,需要得到剛插入的資料的主鍵,MySQL中有以下做法: 1、推薦使用 <insert id="addVehicleParam" parameterType="com.corp.dto.VehicleParamAddDt

spring xml配置檔案標籤的含義

例如在spring-config.xml檔案頭看到如下的配置: <beans xmlns="http://www.springframework.org/schema/beans"          xmlns:xsi="http://www.w3.o

application.properties多環境配置檔案jar外部配置檔案、配置項加密、程式配置使用

一、簡介 spring boot專案application.properties檔案存放及使用介紹 二、方法一多環境配置檔案 我們一般都會有多個應用環境,開發環境、測試環境、生產環境,各個環境的配置會略有不同,我可以根據這個建立多份配置檔案,由主配置檔案來控制讀取那個子配置 建立spring boot專案

Android-通過網路獲取xml檔案使用pull解析得到伺服器的資訊(新聞客戶端)

通過網路獲取xml檔案,使用pull解析該檔案得到伺服器中的資訊; demo中使用了一個開源的圖片載入包,故上傳原始碼方便檢視; 效果圖: 步驟: 1.連線伺服器獲取xml檔案; 2.使用pull解析xml檔案存入實體物件中; 3.解析後將實體物件存入List集合中;

從AndroidManifest.xml檔案獲取 android:value 值的Integer與String轉換異常

最近接入Android的渠道商SDK時,在AndroidManifest.xml中添加了一些渠道引數,發現當填入的值恰好為一個int值,使用getString()方法取值就會報一個型別轉換異常。 配置如下: <meta-data android:

xml檔案載入Animator屬性動畫的三種xml標籤

<set android:ordering="sequentially" > <set> <objectAnimator android:duration="500" android:propertyN

Spring xml檔案dubbo相關標籤不能被識別的問題

1、解決Eclipse的XML驗證錯誤:手動指定XSD檔案 http://blog.csdn.net/tech4j/article/details/46754751 回到Eclipse中右鍵點選配置檔案,選擇Validate,然後紅叉就都消失了嘿!

MyBatisxml檔案部分標籤使用

1:parameterType、resultType 無非就是物件,map,其他型別 <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.