xml学习-基础(根据马士兵视频)

前端之家收集整理的这篇文章主要介绍了xml学习-基础(根据马士兵视频)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.bjsxt;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class Dom4jTest {

	public static void main(String[] args) throws Exception {

		readXml();
		createDocument();
	}

	public static void readXml() throws DocumentException,Exception {
		SAXReader saxReader = new SAXReader();

		Document doc = saxReader.read(new File("User.hbm.xml"));
		//获取根节点
		Element rootElement = getRootNodes(doc);
		//遍历所有节点
		treeWalk(rootElement);
		
		listAllChildren(rootElement);
		//xpath用法
		xpathUse(doc);
		// 枚举名称为foo的节点
		listNodesByName(rootElement);
		
	}

	public static void listNodesByName(Element rootElement) {
		for (Iterator<Element> k = rootElement.elementIterator("aasd"); k.hasNext();) {
			Element element1 = (Element) k.next();
			System.out.println(element1.getName());
		}
	}

	public static void xpathUse(Document doc) {
		// XPath
		List<Node> list = doc.selectNodes("//hibernate-mapping/class/property");
		for (Node node : list) {
			System.out.println(node.getName());
			System.out.println(node.valueOf("@name"));
		}
		System.out.println("-----------------------------Xpath结束----------------------------");
	}

	public static void listAllChildren(Element rootElement) {
		// 枚举所有子节点
		for (Iterator<Element> i = rootElement.elementIterator(); i.hasNext();) {
			Element element = (Element) i.next();
			System.out.println(element.getName());

			// 枚举属性
			for (Iterator j = element.attributeIterator(); j.hasNext();) {
				Attribute attribute = (Attribute) j.next();
				System.out.println(attribute.getName() + "---->" + attribute.getValue());
			}
			System.out.println("-----------------------------------------");

		}
	}

	public static Element getRootNodes(Document doc) {
		//获取根节点
		Element rootElement = doc.getRootElement();

		System.out.println(rootElement.getName());
		return rootElement;
	}

	// 递归
	public static void treeWalk(Element element) {

		for (int i = 0,size = element.nodeCount(); i < size; i++) {

			Node node = element.node(i);

			if (node instanceof Element) {
				
				System.out.println(node.getName() + "-->它是节点");
				treeWalk((Element) node);

			} else {
				System.out.println(node.getName() + "-->它不是节点");
			}

		}

	}

	public static Document createDocument() throws Exception {

		Document document = DocumentHelper.createDocument();

		Element root = document.addElement("二少之家");

		Element author1 =root
			.addElement("父亲")
			.addAttribute("姓名","阿牛")
			.addAttribute("性格","温和")
			.addText("我的父亲");

		Element author2 =root
			.addElement("母亲")
			.addAttribute("姓名","阿珍")
			.addAttribute("性格","急躁")
			.addText("我的母亲");
		
		Element author3 =root
			.addElement("老婆")
			.addAttribute("姓名","阿享")
			.addAttribute("性格","温柔")
			.addText("我的老婆");
		// 写为xml文件
		OutputStream out = new FileOutputStream("二少之家.xml");
		// 防止乱码
		Writer w = new OutputStreamWriter(out,"UTF-8");
		document.write(w);
		// 关掉流,保证内容全部写到了文件里面
		w.flush();
		w.close();

		//格式美化
		FileOutputStream outStream = new FileOutputStream("二少之家2.xml");
		// Writer fileWriter = new FileWriter(fileName);
		OutputFormat format = OutputFormat.createPrettyPrint();
		// 防止中文乱码问题
		format.setEncoding("UTF-8");
		XMLWriter xmlWriter = new XMLWriter(outStream,format);
		xmlWriter.write(document);
		xmlWriter.flush();
		xmlWriter.close();
		return document;

	}
}
原文链接:https://www.f2er.com/xml/297963.html

猜你在找的XML相关文章