星空网 > 软件开发 > Java

Javaweb学习笔记——使用Jdom解析xml

 

一、前言

Jdom是什么?

Jdom是一个开源项目,基于树形结构,利用纯java的技术对

Jdom的优点:

1、Jdom专用于java技术,比Dom应用占用更少内存

2、Jdom提供更加简单和逻辑性访问

3、除

Jdom的构成:

Jdom由6个包构成

Element类表示

org.jdom:      解析

org.jdom.adapters:   包含DOM适配的Java

org.jdom.filter:      包含

org.jdom.input:     包含读取

org.jdom.output:      包含输出

org.jdom.trans form: 包含将Jdom

 

 

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

这里笔者代码做的是使用java创建一个

二、操作

下载jdom包,解压文件jdom-2.0.6.jar,jdom-2.0.6-javadoc.jar,将包导入到lib文件夹下。(注,如果有错误的话,将Jdom中的包全部导入)

Javaweb学习笔记——使用Jdom解析xml

 

例子1:使用jdom创建一个

新建类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.//生成public class CreateJdom {  public static void main(String[] args) {    //定义元素    Element people,student;    people = new Element("people");    student = new Element("student");    //设置属性    student.setAttribute("name", "张三");    student.setAttribute("salary","8000");    //设置文本    student.setText("呵呵");    //将其添加到根目录下    people.addContent(student);        //新建一个文档。    Document doc = new Document(people);    //读取格式,赋值给当前的Format    Format format = Format.getCompactFormat();    //对当前格式进行初始化    format.setEncoding("UTF-8");    //设置    format.setIndent("  ");    //建一个    new try {      //将其写好的文本给工厂,并且建一个文件输出流,将数据输出      new FileOutputStream("people.));      System.out.println("成功!");    } catch (FileNotFoundException e) {      // TODO Auto-generated catch block      e.printStackTrace();    } catch (IOException e) {      // TODO Auto-generated catch block      e.printStackTrace();    }  }}/*运行结果:<?*/

 

 例子2:使用Jdom解析people.

新建Read

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.public class Readpublic static void main(String[] args) {    //新建构造器解析    SAXBuilder sax = new SAXBuilder();    //建一个文档去接受数据    Document doc;    try {      //获取people.      doc = sax.build("people.);      //获得根节点      Element people = doc.getRootElement();      //获得根节点下的节点数据      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();    }  }}/* * 运行结果: * name:张三  salary:8000    呵呵 * */

 

解析Javaweb学习笔记——使用Jdom解析xmlJavaweb学习笔记——使用Jdom解析xml

用jdom获取多个相同标签名的不同属性值的方法<?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 Read/**   * @param args   */  public static void main(String[] args) throws JDOMException, IOException {    SAXBuilder sb = new SAXBuilder();    //构造文档对象    Document doc = sb.build(Test.class.getClassLoader().getResourceAsStream("nation.));    //获取根元素    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"> 标签里内容          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());            }          }        }      }    }  }}打印结果:China<--------->PhotoIDWidth,38PhotoIDWidth<--------->0.60ChildHeadPercentAustralia<--------->PhotoIDWidth,35PhotoIDWidth<--------->0.711ChildHeadPercentAustria<--------->PhotoIDWidth,35PhotoIDWidth<--------->0.689ChildHeadPercentJdom解析

Jdom解析

 




原标题:Javaweb学习笔记——使用Jdom解析xml

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

特朗普和拜登,谁上台对中国更有利?:https://www.ikjzd.com/articles/133031
旺季过后,卖家需要注意哪些问题?:https://www.ikjzd.com/articles/133032
“举报违规行为”正确率低或将被取消举报权限!:https://www.ikjzd.com/articles/133033
速看!美国大选接下来的一些重要时间节点:https://www.ikjzd.com/articles/133035
急!欧洲站全部沦陷,多地取消圣诞市场!物流或大面积延误!:https://www.ikjzd.com/articles/133036
跨境电商发展政策利好,进口消费线上化进程加速!:https://www.ikjzd.com/articles/133037
皇家游轮航线 皇家邮轮旅游攻略:https://www.vstour.cn/a/408245.html
2017春节旅游攻略有吗:https://www.vstour.cn/a/408246.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流