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

本文介绍了如何使用SAX解析器处理自定义XML文件,包括处理XML属性、字符串操作和异常处理等内容。
摘要由CSDN通过智能技术生成


<?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






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值