欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用JDOM生成和解析XML

程序员文章站 2022-04-11 09:51:47
...

到官方网站下载JDOM包http://www.jdom.org/ 

注意的是,版本1和版本2的类路径已经变更,如果你是更新使用版本2,则需要重新编译你的代码

 

package com.test;

import java.io.FileOutputStream;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

/**
 * @说明 JDom生成解析XML
 * @author cuisuqiang
 * @version 1.0
 * @since
 */
@SuppressWarnings("unchecked")
public class JDomDemo {
	public static void main(String[] args) {
		String file = "C:\\p.xml"; // 文件存放位置
		JDomDemo dj = new JDomDemo();
		dj.createXml(file);
		dj.parserXml(file);
	}
	/**  
     * 生成XML  
     * @param filePath 文件路径  
     */
	public void createXml(String fileName) {
		Element root = new Element("persons");
		Document document = new Document(root);
		Element person = new Element("person");
		root.addContent(person);
		Element name = new Element("name");
		name.setText("java小强");
		person.addContent(name);
		Element sex = new Element("sex");
		sex.setText("man");
		person.addContent(sex);
		Element age = new Element("age");
		age.setText("23");
		person.addContent(age);
		XMLOutputter XMLOut = new XMLOutputter();
		try {
			Format f = Format.getPrettyFormat();
			f.setEncoding("UTF-8");//default=UTF-8
			XMLOut.setFormat(f);
			XMLOut.output(document, new FileOutputStream(fileName));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	 /**  
     * 解析XML  
     * @param filePath 文件路径  
     */ 
	public void parserXml(String fileName) {
		try {
			SAXBuilder builder = new SAXBuilder();
			Document document = builder.build(fileName);
			Element root = document.getRootElement();
			List persons = root.getChildren("person");
			for (int i = 0; i < persons.size(); i++) {
				Element person = (Element) persons.get(i);
				List pros = person.getChildren();
				for (int j = 0; j < pros.size(); j++) {
					Element element = (Element) pros.get(j);
					System.out.println(element.getName() + ":" + element.getValue());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

请您到ITEYE网站看原创,谢谢!

http://cuisuqiang.iteye.com/ ! 

自建博客地址:http://www.javacui.com/ ,内容与ITEYE同步!