Jdom解析XML文档

本文介绍如何利用Java库JDOM来创建XML文档并解析其内容。通过示例代码展示了创建XML文档的过程,包括添加元素、属性、文本内容及注释,并将文档输出到文件中;同时提供了读取XML文档的方法,演示了如何获取XML中的各种数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

##创建XML文档

/**
 *
 */
package com.xiaobian.dom.jdom;
import java.io.FileWriter;
import java.io.IOException;

import org.jdom.*;
import org.jdom.output.XMLOutputter;
/**
 * @author xiaobian
 *
 */
public class Jdom {

    public Jdom() {
        super();
    }
    public void CreateDocument(){
        //创建根元素
        Element eRoot = new Element("person");
        //为根元素创建属性
        eRoot.setAttribute(new Attribute("id", "13562986536525458954"));
        //为根元素添加注释内容
        eRoot.addContent(new Comment("A Person"));
        //创建根元素的子元素
        Element name = new Element("name");
        //为此元素创建内容
        name.addContent("xiaobian");
        //创建根元素的子元素
        Element sex = new Element("sex");
        sex.addContent("Male");
        //创建根元素的子元素
        Element year = new Element("year");
        year.addContent("1982");
        //创建根元素的子元素
        Element age = new Element("age");
        age.addContent("26");
        //创建根元素的子元素
        Element height = new Element("height");
        height.setAttribute("state","China");
        height.addContent("178cm");
        eRoot.addContent(name);
        eRoot.addContent(sex);
        eRoot.addContent(year);
        eRoot.addContent(age);
        eRoot.addContent(height);
       
        Document d = new Document(eRoot);
        //定义输出
        XMLOutputter outputter = new XMLOutputter();
        //输出流
        FileWriter writer = null;
         try {
             //输出到控制台
            outputter.output(d, System.out);
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
       
        try {
            //创建文件流
            writer = new FileWriter("c://test.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            //输出到Xml文件
            outputter.output(d, writer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            //关闭文件流
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        Jdom jdom = new Jdom();
        //测试
        jdom.CreateDocument();
    }

}

##读取XML文档

 public static void main(String[] args) throws Exception{ 
     SAXBuilder sb=new SAXBuilder();
     Document doc=sb.build("c:/test.xml"); //构造文档对象
     Element root=doc.getRootElement(); //获取根元素
     String rootName = root.getName();
     System.out.println("Type :"+rootName);
     Element name = root.getChild("name");
     System.out.println("Name: "+name.getText());
     Element sex = root.getChild("sex");
     System.out.println("Sex: "+sex.getText());
     Element year = root.getChild("year");
     System.out.println("Birth: "+year.getText());
     Element age = root.getChild("age");
     System.out.println("Age: "+age.getText());
     Element height = root.getChild("height");
     System.out.println("Height: "+height.getText());
     System.out.println("State: "+height.getAttributeValue("state"));
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值