首页 > 代码库 > jdom的运用

jdom的运用

1.到官网下载jdom文件。(www。jdom.org)

2.减压缩:找到build下的jdom包 。

web中使用:直接拷贝到Tomcat目录放在lib文件夹下即可。

程序中使用:找到jdom.jar的路径,复制到CLASSPATH环境中去。

import java.io.*;import org.jdom.*;import org.jdom.output.*;public class WriteXML {    public static void main(String[] args) throws Exception {        Element addresslist = new Element ("addresslist");        Element linkman = new Element ("linkman");        Element name = new Element ("name");        Element email = new Element ("email");        Attribute id = new Attribute("id","lop");        Document doc = new Document (addresslist);        name.setText("lcp");        name.setAttribute(id);//讲属性设置到元素之中        email.setText("mldn@l63.com");        linkman.addContent(name);//设置关系        linkman.addContent(email);        addresslist.addContent(linkman);        XMLOutPutter out = new XMLOutPutter();        out.setFormat(out getFormat(),setEncoding("GBK"));        out.output(doc,new FileOutputStream(new File("D:" + File.separator + "address.xml")));    }}

sax 解析文件

package org.lxh.xml.jdom;import java.io.*;import java.util.*;import org.jdom;import org.jdom.input.*;public class WriteXML {    public static void main(String args[]) throws Exception{        SAXBuilder builder = new SAXBuilder();        Document read_doc = builder.build(new File("D:" + File.separator + "address.xml"));        Element root = read_doc.getRootElement();//取得根        List list = root.getChildree("linkman");//取得所有的linkman        for(int x = 0; x < list.size(); x++){            Element e = (Element)list.get();            String name = e.getChildText("name");//得到name子节点内容            String id = e.getChild("name").getAttribute("id").getValue();             String email = e.getChildText("email");            System.out.println("姓名" + name + ",编号" + id );            System.out.println("Eamil" + name + " );        }    }}

 

jdom的运用