国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Javaweb中使用Jdom解析xml的方法

2019-11-26 13:47:11
字體:
供稿:網(wǎng)友

一、前言

Jdom是什么?

Jdom是一個開源項目,基于樹形結(jié)構(gòu),利用純java的技術(shù)對XML文檔實現(xiàn)解析,生成,序列化以及多種操作。它是直接為java編程服務(wù),利用java語言的特性(方法重載,集合),把SAX和DOM的功能結(jié)合起來,盡可能的把原來解析xml變得簡單,我們使用Jdom解析xml會是一件輕松的事情。

Jdom的優(yōu)點:

1、Jdom專用于java技術(shù),比Dom應(yīng)用占用更少內(nèi)存。

2、Jdom提供更加簡單和邏輯性訪問xml信息的基礎(chǔ)方法

3、除xml文件外,Jdom還可以訪問其他的數(shù)據(jù)源,例如可以創(chuàng)建類從SQL查詢結(jié)果中訪問數(shù)據(jù)

Jdom的構(gòu)成:

Jdom由6個包構(gòu)成

Element類表示XML文檔的元素

org.jdom:      解析xml文件所要用到的基礎(chǔ)類

org.jdom.adapters: 包含DOM適配的Java類

org.jdom.filter:    包含xml文檔的過濾類

org.jdom.input:   包含讀取XML文檔的Java類

org.jdom.output: 包含輸出XML文檔的類

org.jdom.trans form: 包含將Jdom xml文檔接口轉(zhuǎn)換為其他XML文檔接口的Java類

xml是什么?

xml是一種廣為使用的可擴展標(biāo)記語言,java中解析xml的方式有很多,最常用的像jdom、dom4j、sax等等。

Jdom包下載:http://www.jdom.org/downloads/index.html

這里筆者代碼做的是使用java創(chuàng)建一個xml和讀取一個xml,僅作為筆記介紹。

二、操作

下載jdom包,解壓文件jdom-2.0.6.jar,jdom-2.0.6-javadoc.jar,將包導(dǎo)入到lib文件夾下。(注,如果有錯誤的話,將Jdom中的包全部導(dǎo)入)

例子1:使用jdom創(chuàng)建一個xml文件,名字為people.xml

新建類CareateJdom

package com.book.jdom;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import org.jdom2.Document;import org.jdom2.Element;import org.jdom2.output.Format;import org.jdom2.output.XMLOutputter;//生成xml文件public class CreateJdom {public static void main(String[] args) {//定義元素Element people,student;people = new Element("people");student = new Element("student");//設(shè)置屬性student.setAttribute("name", "張三");student.setAttribute("salary","8000");//設(shè)置文本student.setText("呵呵");//將其添加到根目錄下people.addContent(student);//新建一個文檔。Document doc = new Document(people);//讀取格式,賦值給當(dāng)前的FormatFormat format = Format.getCompactFormat();//對當(dāng)前格式進行初始化format.setEncoding("UTF-8");//設(shè)置xml文件縮進4個空格format.setIndent(" ");//建一個xml輸出工廠,將格式給工廠XMLOutputter xmlout = new XMLOutputter(format);try {//將其寫好的文本給工廠,并且建一個文件輸出流,將數(shù)據(jù)輸出xmlout.output(doc, new FileOutputStream("people.xml"));System.out.println("成功!");} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}/*運行結(jié)果:<?xml version="1.0" encoding="UTF-8"?><people><student name="張三" salary="8000" /></people>* */

例子2:使用Jdom解析people.xml文件

新建Readxml類

package com.book.jdom;import java.io.IOException;import java.util.List;import org.jdom2.Document;import org.jdom2.Element;import org.jdom2.JDOMException;import org.jdom2.input.SAXBuilder;//讀取people.xml文檔public class Readxml {public static void main(String[] args) {//新建構(gòu)造器解析xmlSAXBuilder sax = new SAXBuilder();//建一個文檔去接受數(shù)據(jù)Document doc;try {//獲取people.xml文檔doc = sax.build("people.xml");//獲得根節(jié)點Element people = doc.getRootElement();//獲得根節(jié)點下的節(jié)點數(shù)據(jù)List<Element> list = people.getChildren();for(int i = 0;i<list.size();i++){Element e = list.get(i);//獲得屬性值System.out.println("name:"+e.getAttributeValue("name")+" salary:"+e.getAttributeValue("salary"));//獲得文本值System.out.println(e.getText());}} catch (JDOMException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}/** 運行結(jié)果:* name:張三 salary:8000呵呵* */

解析xml

用jdom獲取多個相同標(biāo)簽名的不同屬性值的方法<?xml version="1.0" encoding="UTF-8"?><Configuration>    <Key Name="China">      <Value Name="TextKey">China</Value>      <Value Name="Enabled">true</Value>      <Value Name="PhotoIDWidth">38PhotoIDWidth</Value>      <Value Name="PhotoIDHeight">38</Value>      <Key Name="Adult">        <Value Name="CrownPercent">0.10</Value>        <Value Name="HeadPercent">0.60AdultHeadPercent</Value>      </Key>      <Key Name="Child">        <Value Name="CrownPercent">0.10</Value>        <Value Name="HeadPercent">0.60ChildHeadPercent</Value>      </Key>    </Key>    <Key Name="Australia">      <Value Name="TextKey">Australia</Value>      <Value Name="Enabled">true</Value>      <Value Name="PhotoIDWidth">35PhotoIDWidth</Value>      <Value Name="PhotoIDHeight">45</Value>      <Key Name="Adult">        <Value Name="CrownPercent">0.061</Value>        <Value Name="HeadPercent">0.756"Adult"HeadPercent</Value>      </Key>      <Key Name="Child">        <Value Name="CrownPercent">0.072</Value>        <Value Name="HeadPercent">0.711ChildHeadPercent</Value>      </Key>    </Key>    <Key Name="Austria">      <Value Name="TextKey">Austria</Value>      <Value Name="Enabled">true</Value>      <Value Name="PhotoIDWidth">35PhotoIDWidth</Value>      <Value Name="PhotoIDHeight">45</Value>      <Key Name="Adult">        <Value Name="CrownPercent">0.064</Value>        <Value Name="HeadPercent">0.744AdultHeadPercent</Value>      </Key>      <Key Name="Child">        <Value Name="CrownPercent">0.078</Value>        <Value Name="HeadPercent">0.689ChildHeadPercent</Value>      </Key>    </Key></Configuration>package input;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.jdom.Document;import org.jdom.Element;import org.jdom.JDOMException;import org.jdom.input.SAXBuilder;public class ReadXML {  /**   * @param args   */  public static void main(String[] args) throws JDOMException, IOException {    SAXBuilder sb = new SAXBuilder();    //構(gòu)造文檔對象    Document doc = sb.build(Test.class.getClassLoader().getResourceAsStream("nation.xml"));    //獲取根元素    Element root = doc.getRootElement();    //定位到<Configuration> -> <Key>    List<Element> list = root.getChildren("Key");    List<Element> children = new ArrayList<Element>();    List<Element> childrens = new ArrayList<Element>();    for (int i = 0; i < list.size(); i++) {      Element element = (Element) list.get(i);      System.out.print(element.getAttributeValue("Name"));      //定位到<Configuration> -> <Key> -> <Value>      children = element.getChildren("Value");       for(int j=0; j<children.size(); j++){        Element elementChildren = (Element) children.get(j);        //定位到<Configuration> -> <Key> -> <Value Name="PhotoIDWidth">        if(elementChildren.getAttributeValue("Name").equals("PhotoIDWidth")){          //獲取<Configuration> -> <Key> -> <Value Name="PhotoIDWidth"> 屬性值          System.out.print("<--------->"+elementChildren.getAttributeValue("Name"));          //獲取<Configuration> -> <Key> -> <Value Name="PhotoIDWidth"> 標(biāo)簽里內(nèi)容          System.out.print(","+elementChildren.getText());        }      }      children.clear();      //定位到<Configuration> -> <Key> -> <Key>      children = element.getChildren("Key");      for(int j=0; j<children.size(); j++){        Element elementChildren = (Element)children.get(j);        //定位到<Configuration> -> <Key> -> <Key Name="Child">        if(elementChildren.getAttributeValue("Name").equals("Child")){          //定位到<Configuration> -> <Key> -> <Key Name="Child"> -> <Value>          childrens = elementChildren.getChildren("Value");          for(int k=0; k<childrens.size(); k++){            Element elementChildrens = (Element)childrens.get(k);            //定位到<Configuration> -> <Key> -> <Key Name="Child"> -> <Value Name="HeadPercent">            if(elementChildrens.getAttributeValue("Name").equals("HeadPercent")){              System.out.println("<--------->"+elementChildrens.getText());            }          }        }      }    }  }}打印結(jié)果:China<--------->PhotoIDWidth,38PhotoIDWidth<--------->0.60ChildHeadPercentAustralia<--------->PhotoIDWidth,35PhotoIDWidth<--------->0.711ChildHeadPercentAustria<--------->PhotoIDWidth,35PhotoIDWidth<--------->0.689ChildHeadPercent

以上所述是小編給大家介紹的Javaweb中使用Jdom解析xml的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 荃湾区| 博罗县| 白银市| 内乡县| 比如县| 莱芜市| 屏山县| 蒙阴县| 德钦县| 神池县| 高安市| 桦甸市| 营口市| 承德县| 余江县| 镇沅| 大庆市| 砀山县| 澄城县| 房产| 仁布县| 闸北区| 甘德县| 南城县| 唐河县| 博野县| 柘荣县| 苗栗市| 南通市| 绥江县| 读书| 临泽县| 泾源县| 平山县| 伊春市| 自贡市| 吕梁市| 安顺市| 尼勒克县| 南漳县| 纳雍县|