现在的位置: 首页 > 综合 > 正文

简单SAX封闭(解析自定义XML”)

2018年02月13日 ⁄ 综合 ⁄ 共 3317字 ⁄ 字号 评论关闭

<?xml version="1.0" encoding="utf-8"?>
<classify>
	<image>123</image>
	<image>456</image>
</classify>

package com.yzsd.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			InputStream in = new FileInputStream(new File("1.xml"));
			/*
			 * root节点是第一个节点不包含任何数据 root节点的第一个子节点是xml的第一个标签
			 */
			TreeNode root = ParseXml.parseXml(in);
			root = root.getChildren().get(0);
			for (int i = 0; i < root.getChildren().size(); i++) {
				System.out.println(root.getChildren().get(i).getValue());
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static void readTree(TreeNode treeNode) {
		System.out.println(treeNode.getAttributes());

		for (int i = 0; i < treeNode.getChildren().size(); i++) {
			readTree(treeNode.getChildren().get(i));
		}
	}
}

package com.yzsd.demo;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

public class ParseXml {

	private static SAXParser saxParser;
	
	static {
		SAXParserFactory sParserFactory = SAXParserFactory.newInstance();

		try {
			saxParser = sParserFactory.newSAXParser();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
	}

	public static TreeNode parseXml(InputStream inputStream) {
		UserHandler userHandler = new UserHandler();
		try {
			saxParser.parse(inputStream, userHandler);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return userHandler.getRootNode();
	}
}

package com.yzsd.demo;
import java.util.Stack;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class UserHandler extends DefaultHandler {

	private Stack<TreeNode> mStack;

	private TreeNode rootNode;

	@Override
	public void startDocument() throws SAXException {
		mStack = new Stack<TreeNode>();
		rootNode = new TreeNode();
		mStack.push(rootNode);
		super.startDocument();
	}

	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		TreeNode treeNode = new TreeNode();
		treeNode.setTagName(qName);
		treeNode.setAttributes(attributes);

		if (!mStack.isEmpty()) {
			mStack.peek().addChild(treeNode);
		}

		mStack.push(treeNode);
		super.startElement(uri, localName, qName, attributes);
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		mStack.pop();
		super.endElement(uri, localName, qName);
	}

	@Override
	public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
		mStack.peek().setValue(new String(arg0, arg1, arg2));
		super.characters(arg0, arg1, arg2);
	}
	
	public TreeNode getRootNode() {
		return rootNode;
	}
}

package com.yzsd.demo;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;

public class TreeNode {
	private String tagName;

	private String Value;

	private List<TreeNode> children;

	private Attributes attributes;

	public TreeNode() {
		children = new ArrayList<TreeNode>();
	}

	public String getTagName() {
		return tagName;
	}

	public void setTagName(String tagName) {
		this.tagName = tagName;
	}

	public String getValue() {
		return Value;
	}

	public void setValue(String value) {
		Value = value;
	}

	public List<TreeNode> getChildren() {
		return children;
	}

	public void addChild(TreeNode treeNode) {
		this.children.add(treeNode);
	}

	public Attributes getAttributes() {
		return attributes;
	}

	public void setAttributes(Attributes attributes) {
		this.attributes = attributes;
	}
}

结果:123
    456

抱歉!评论已关闭.