java递归遍历Xml
2015-04-27 15:30
337 查看
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XMLDocParser {
private HashMap<String,XmlElement> map = new HashMap<String,XmlElement>();
public XMLDocParser()
{
}
/**
*
* @param xmlContent
* @param r
* @throws UnsupportedEncodingException
*/
public void parseXml(String xmlContent ) throws UnsupportedEncodingException
{
SAXReader reader = new SAXReader();
Document doc;
java.io.BufferedInputStream is =null;
try
{
is = new java.io.BufferedInputStream(new java.io.ByteArrayInputStream(xmlContent.getBytes("UTF-8")));
doc = reader.read(is);
Element root = doc.getRootElement();
Iterator<Element> allSons = root.elementIterator();
while(allSons.hasNext()){
getLeafNodes(allSons.next());
}
for(String key:map.keySet()){
System.out.println(map.get(key).getTagName());
}
}
catch (DocumentException e)
{
e.printStackTrace();
}finally{
try {
if(is!=null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// r.setElementMap(map);
}
/**
* 获取叶子节点
* @param currentNode
* @return
*/
private void getLeafNodes(Element currentNode)
{
Element e = currentNode;
String elementTag = e.getName();
String elementValue = e.getStringValue();
String elementParmentTag = e.getParent().getName();
XmlElement xmle = new XmlElement(elementTag, elementValue, elementParmentTag);
map.put(elementTag,xmle);
if ((e.elements()).size() >= 0)
{
List<Element> el = e.elements();
for (Element sonNode : el)
{
getLeafNodes(sonNode);
}
}
}
public static void main(String[] args) throws Exception {
String xml="<html><head><xx>ooxx</xx></head><body><mm>123</mm><oo>xxx</oo><nn>123456</nn></body></html>";
XMLDocParser a = new XMLDocParser();
a.parseXml(xml);
}
}
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XMLDocParser {
private HashMap<String,XmlElement> map = new HashMap<String,XmlElement>();
public XMLDocParser()
{
}
/**
*
* @param xmlContent
* @param r
* @throws UnsupportedEncodingException
*/
public void parseXml(String xmlContent ) throws UnsupportedEncodingException
{
SAXReader reader = new SAXReader();
Document doc;
java.io.BufferedInputStream is =null;
try
{
is = new java.io.BufferedInputStream(new java.io.ByteArrayInputStream(xmlContent.getBytes("UTF-8")));
doc = reader.read(is);
Element root = doc.getRootElement();
Iterator<Element> allSons = root.elementIterator();
while(allSons.hasNext()){
getLeafNodes(allSons.next());
}
for(String key:map.keySet()){
System.out.println(map.get(key).getTagName());
}
}
catch (DocumentException e)
{
e.printStackTrace();
}finally{
try {
if(is!=null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// r.setElementMap(map);
}
/**
* 获取叶子节点
* @param currentNode
* @return
*/
private void getLeafNodes(Element currentNode)
{
Element e = currentNode;
String elementTag = e.getName();
String elementValue = e.getStringValue();
String elementParmentTag = e.getParent().getName();
XmlElement xmle = new XmlElement(elementTag, elementValue, elementParmentTag);
map.put(elementTag,xmle);
if ((e.elements()).size() >= 0)
{
List<Element> el = e.elements();
for (Element sonNode : el)
{
getLeafNodes(sonNode);
}
}
}
public static void main(String[] args) throws Exception {
String xml="<html><head><xx>ooxx</xx></head><body><mm>123</mm><oo>xxx</oo><nn>123456</nn></body></html>";
XMLDocParser a = new XMLDocParser();
a.parseXml(xml);
}
}
相关文章推荐
- java非递归实现Xml的遍历(多叉树遍历)
- Java采用DOM方式递归遍历XML文件内容
- Java递归遍历XML所有元素
- Java递归遍历XML所有元素。
- Java使用递归遍历文件夹
- 二叉树的高度 java 利用递归和层次遍历两种方法
- Java 二叉树的前序、中序、后续遍历 递归和迭代实现
- 递归遍历E盘所有.java文件的绝对路径
- dom4j递归遍历XML的所有元素
- JAVA递归、非递归遍历二叉树
- JAVA 遍历文件夹下的所有文件(递归调用和非递归调用)
- Java递归遍历查询音乐,文件夹
- 基于Java Socket的文件UpLoad代码(完美版)-用递归解决java的目录树遍历
- Java实现二叉树的递归构建及先序、中序、后序、层序遍历(递归和循环)
- Java实现二叉树的前序、中序、后序、层序遍历(递归方法)
- Java二叉树的递归,非递归遍历,高度,节点数,叶子节点数
- java建立二叉树,递归/非递归先序遍历,递归/非递归中序遍历,层次遍历
- 非递归遍历二叉树--java实现
- 黑马程序员——JAVA基础---方法递归,遍历全路径
- 使用dom4j递归遍历xml