Sample code sharing for simply parsing XML with SAX

黄舟
Release: 2017-03-31 14:33:50
Original
1312 people have browsed it

SAX simple parsingXMLSample code sharing

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);
		}
	}
	
}
Copy after login

The above is the detailed content of Sample code sharing for simply parsing XML with SAX. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!