旗下导航:搜·么
当前位置:网站首页 > XML教程 > 正文

SAX简朴剖析XML的示例代码分享【XML教程】,SAX,XML

作者:搜教程发布时间:2019-11-30分类:XML教程浏览:37评论:0


导读:SAX简朴剖析XML的示例代码分享packagecom.zkn.xmlparse.text;importjava.io.File;importjava.u...
SAX简朴剖析XML的示例代码分享

package com.zkn.xmlparse.text;

import java.io.File;
import java.util.Iterator;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * @since:2015-6-3 下昼12:40:10
 * 
 */
public class XMLParseTest02 {

	public static void main(String[] args) throws Exception {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/mapping.xml"));
		Element element = document.getRootElement();
		getElement(element);
	}
	
	public static void getElement(Element element) {
		/**
		 * 获得tag名字
		 */
		String elements = element.getName();
		Iterator<?> ite = element.attributes().iterator();
		String attrName = "";
		while(ite.hasNext()) {
			Attribute attr =  (Attribute)ite.next();
			attrName += attr.getName() + "="+attr.getValue()+"   ";
		}
		System.out.println("元素名字:"+elements+"  属性:"+attrName);
		Iterator<?> it = element.elements().iterator();
		while(it.hasNext()){
			Element ele = (Element)it.next();
			//递归挪用
			getElement(ele);
		}
	}
	
}

以上就是SAX简朴剖析XML的示例代码分享的细致内容,更多请关注ki4网别的相干文章!

标签:SAXXML


欢迎 发表评论: