加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml解析3:使用jdom框架进行对xml文档生成和读写解析

发布时间:2020-12-15 23:36:46 所属栏目:百科 来源:网络整理
导读:使用jdom框架首先下载需要的jar包 :jdom.jar 然后将jdom.jar纳入到java项目的管理之中 一个例子: package ytu.botao.xml.dom;import java.io.FileWriter;import java.io.IOException;import org.jdom.Attribute;import org.jdom.Comment;import org.jdom.D

使用jdom框架首先下载需要的jar包 :jdom.jar

然后将jdom.jar纳入到java项目的管理之中

一个例子:

package ytu.botao.xml.dom;
import java.io.FileWriter;
import java.io.IOException;
import org.jdom.Attribute;
import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
 * 使用jdom框架进行 对xml构造,在本地硬盘上生成xml文档
 *
 * @author botao
 *
 */
public class JdomTest1 {
    /**
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        Document document = new Document();
        Element root = new Element("root");
        document.addContent(root);
        Comment comment = new Comment("This is my comments");
        root.addContent(comment);
        Element e = new Element("hello");
        e.setAttribute("sohu","www.sohu.com");
        root.addContent(e);
        Element e2 = new Element("world");
        // 方法一:设置属性
        Attribute attr = new Attribute("test","hehe");
        e2.setAttribute(attr);
        e.addContent(e2);
        // 方法二:利用方法链的风格进行设置属性
        e2.addContent(new Element("aaa").setAttribute("a","b")
                .setAttribute("x","y").setAttribute("gg","hh")
                .setText("text content"));
        Format format = Format.getPrettyFormat();
        format.setIndent("    ");
        // format.setEncoding("gbk");
        XMLOutputter out = new XMLOutputter(format);
        out.output(document,new FileWriter("jdom.xml"));
    }
}



解析:


package ytu.botao.xml.dom;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
 * 利用jdom框架进行解析和修改属性
 * @author botao
 *
 */
public class JdomTest2 {
    /**
     *
     * @param args
     * @throws IOException
     * @throws FileNotFoundException
     * @throws JDOMException
     */
    public static void main(String[] args) throws FileNotFoundException,IOException,JDOMException {
SAXBuilder builder = new SAXBuilder();
          
        Document doc = builder.build(new File("jdom.xml"));
          
        Element element = doc.getRootElement();
          
        System.out.println(element.getName());
          
        Element hello = element.getChild("hello");
          
        System.out.println(hello.getText());
          
        List list = hello.getAttributes();
          
        for(int i = 0 ;i < list.size(); i++)
        {
            Attribute attr = (Attribute)list.get(i);
              
            String attrName = attr.getName();
            String attrValue = attr.getValue();
              
            System.out.println(attrName + "=" + attrValue);
        }
          
        hello.removeChild("world");
          
        XMLOutputter out = new XMLOutputter(Format.getPrettyFormat().setIndent("    "));
          
          
        out.output(doc,new FileOutputStream("jdom2.xml"));    
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读