java JDOM生成和解析xml

JDOM

使用:需要下载jdom对应的jar引入

场景:需要知道整个文档结构是,比dom性能好

优点:解析的树形结构操作比较灵活,比较简便

JDOM常用类

Document:表示整个xml文档,是一个树形结构

Eelment:表示一个xml的元素,提供方法操作其子元素,它的文本,属性和名称空间

Attribute:表示元素的属性

Text:表示xml文本信息

JDOM生成xml

package com.xm.xml.jdom;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

/**
 * Java jdom生成xml
 * @author ouyangjun
 */
public class CreateJDOM {

	public static void main(String[] args) {
		
		// 调用jdom生成xml方法
		createJDOM(new File("E:\\jdom.xml"));
	}
	
	public static void createJDOM(File file){
		try {
			// 创建一个根节点
			Element rootElement = new Element("root");
			Document doc = new Document(rootElement);
			
			// 在根节点下创建第一个子节点
			Element rootOneElement = new Element("person");
			rootOneElement.setAttribute(new Attribute("attr","root one"));

			// 在第一个子节点下创建第一个子节点
			Element childOneElement = new Element("people");
			childOneElement.setAttribute(new Attribute("attr","child one"));
			childOneElement.setText("person child one");

			// 在第一个子节点下创建第二个子节点
			Element childTwoElement = new Element("people");
			childTwoElement.setAttribute(new Attribute("attr","child two"));
			childTwoElement.setText("person child two");
			
			// 在根节点下创建第二个子节点
			Element rootTwoElement = new Element("person");
			rootTwoElement.setAttribute(new Attribute("attr","root two"));
	
			// 在第一个子节点下创建第一个子节点
			Element oneChildOneElement = new Element("people");
			oneChildOneElement.setAttribute(new Attribute("attr","child one"));
			oneChildOneElement.setText("person child one");
	
			// 在第一个子节点下创建第二个子节点
			Element twoChildTwoElement = new Element("people");
			twoChildTwoElement.setAttribute(new Attribute("attr","child two"));
			twoChildTwoElement.setText("person child two");

			rootOneElement.addContent(childOneElement);
			rootOneElement.addContent(childTwoElement);
			
			rootTwoElement.addContent(oneChildOneElement);
			rootTwoElement.addContent(twoChildTwoElement);
			
			doc.getRootElement().addContent(rootOneElement);
			doc.getRootElement().addContent(rootTwoElement);
			
			// 创建xml输出流操作类
			XMLOutputter xmlOutput = new XMLOutputter();
	
	        // 格式化xml内容
	        xmlOutput.setFormat(Format.getPrettyFormat());
	        // 把xml输出到指定位置
			xmlOutput.output(doc, new FileOutputStream(file));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

xml效果图:

jdom解析xml

package com.xm.xml.jdom;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

/**
 * java jdom解析xml
 * @author ouyangjun
 */
public class ParseJDOM {

	public static void main(String[] args) {
		
		// 调用jdom解析xml方法
		parseJDOM(new File("E:\\jdom.xml"));
	}
	
	public static void parseJDOM(File file){
		try {
			// 创建sax解析器
			SAXBuilder saxBuilder = new SAXBuilder();

			// 获取Document
			Document document = saxBuilder.build(file);
			System.out.println("Root element :" + document.getRootElement().getName());

			Element classElement = document.getRootElement();
			// 递归读取xml节点信息
			parseElement(classElement);
			
		}catch(JDOMException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public static void parseElement(Element classElement){
		List<Element> elementList = classElement.getChildren();
		
		Element element;
		for (int temp = 0; temp < elementList.size(); temp++) {    
			element = elementList.get(temp);
			
			System.out.println("Current Element Name :" + element.getName());
			if (element.hasAttributes()) {
				Attribute attribute =  element.getAttribute("attr");
	            System.out.println("Current Element Attr Value : " + attribute.getValue());
			}
			System.out.println("Current Element Text : " + element.getText());
            
            if( element.getChildren() != null){
            	parseElement(element);
            }
            
         }
	}

}

控制台打印效果图:

本章完结,待续!

本文说明:该文章属于原创,如需转载,请标明文章转载来源

猜你喜欢

转载自blog.csdn.net/p812438109/article/details/81813411