xml解析

package com.openv.spring;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.DOMBuilder;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.xml.sax.SAXException;

public class XmlTest {

	/**
	 * @param args
	 */

	public static void BuildXML() {
		Element root, student, number, name, age;
		root = new Element("student-info"); // 生成根元素:student-info
		student = new Element("student"); // 生成元素:student,该元素中将包含元素number,name,age
		number = new Element("number");
		name = new Element("name");
		age = new Element("age");

		Document doc = new Document(root); // 将根元素植入文档doc中
		number.setText("001");
		name.setText(" ma n ");
		age.setText("15");
		student.addContent(number);
		student.addContent(name);
		student.addContent(age);
		root.addContent(student);

		// Format format = Format.getCompactFormat();
		// format.setEncoding("gb2312"); //设置xml文件的字符为gb2312
		// format.setIndent(" "); //设置xml文件的缩进为4个空格
		// XMLOutputter XMLOut = new XMLOutputter(format);//在元素后换行,每一层元素缩排四格

		// XMLOutputter XMLOut = new XMLOutputter();
		// 第一个参数是行缩进的格式;第二个参数是你是否想另起一行。
		XMLOutputter XMLOut = new XMLOutputter(" ", true);
		// XMLOutputter XMLOut = new XMLOutputter(" ",true,"gb2312");
		XMLOut.setEncoding("GBK");// 设置字符集
		XMLOut.setExpandEmptyElements(true);
		XMLOut.setIndent("  ");
		XMLOut.setTextTrim(true);// 清除值中的空格
		XMLOut.setTrimAllWhite(true);// 清除所有节点之间的空白
		try {
			XMLOut.output(doc, new FileOutputStream("./studentinfo.xml"));
			XMLOut.output(doc, System.out);// 输出到控制台
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * jdom解析xml
	 * 
	 */
	public static void parseXML() {
		SAXBuilder saxBuilder = new SAXBuilder();
		// DOMBuilder domBuilder = new DOMBuilder();//速度慢,尽量不用
		try {
			Document doc = saxBuilder
					.build(new File("D:/eclipse/workspace/studys/applicationContext.xml"));
			Element root = doc.getRootElement();
			List allChildren = root.getChildren();
			Element firstChild = (Element) allChildren.get(0);
			System.out.println("First kid:" + firstChild);
			System.out.println("size:" + allChildren.size());
			Iterator it = allChildren.iterator();
			while (it.hasNext()) {
				Element child = (Element) it.next();
				if (child != null && child.getName().equals("bean")) {
					String value = child.getChildText("property");
					System.out.println("value:" + value);
				}
				if (child != null && child.getName().equals("ref")) {
					String value = child.getText();
					Attribute att = child.getAttribute("local");
					System.out.println("value:" + value);
					System.out.println("Attribute:" + att);
				}
			}

			List children = root.getChild("bean").getChildren("property");
			for (int i = 0; i < children.size(); i++) {
				Element child = (Element) children.get(i);
				Element value = child.getChild("value");
				String text1 = "";
				String text2 = "";
				if (value != null) {
					text1 = value.getText();
					text2 = child.getChildText("value");
				}
				Attribute att = child.getAttribute("name");
				System.out.println("Attribute:" + att);
				System.out.println("value:" + text1 + "-----" + text2);
			}

		} catch (JDOMException e) {
			e.printStackTrace();
		}
	}

	/**
	 * dom解析xml
	 * 
	 * @param args
	 */
	public static void domParseXML() {
		DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder docBuilder = df.newDocumentBuilder();
			org.w3c.dom.Document doc = docBuilder.parse(new File(
					"./applicationContext.xml"));
			org.w3c.dom.Element root = doc.getDocumentElement();
			System.out.println("org.w3c.dom.Document:" + doc
					+ doc.getNodeName());
			System.out.println("org.w3c.dom.Element:" + root + "\n"
					+ "root.getLocalName():" + root.getLocalName() + "\n"
					+ "root.getNodeName():" + root.getNodeName() + "\n"
					+ "root.getTagName():" + root.getTagName());

			
			
			Hashtable a = new Hashtable();
			for (int i = 0; i < 10; i++) {
				Hashtable temp = new Hashtable();
				a.put(1, temp);
			}
			
//			try {
//				Transformer tr = TransformerFactory.newInstance().newTransformer();
//			
//			} catch (TransformerConfigurationException e) {
//				
//				e.printStackTrace();
//			} catch (TransformerFactoryConfigurationError e) {
//				e.printStackTrace();
//			}
		

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// System.out.println("Now we build an XML document .....");
		// BuildXML();
		// System.out.println("finished!");

		parseXML();
		domParseXML();

	}
}

你可能感兴趣的:(java,eclipse,spring,xml,bean)