XML Parser Example

460阅读 0评论2013-02-17 maunix
分类:Java

/**
 * @author mht created on Feb 17, 2013
 * This program is a simple version of XML parser by using DefaultHandler base class.
 * 
 */
import java.util.Stack;


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

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


public class MyHandlerTest extends DefaultHandler {
	
	static final int PRINTTAB = 5; 
	private String _fileName; // XML file name for parsed
	private Stack _stack; // help to create document tree
	private Node _root; // the root of returned document tree
	
	
	private class Node {
		private String _tag = null;
		private String _value = null; // for attributes
		int _numOfChildren = 0;
		Node[] _children = null;
		
		public void insertChild(Node child) {
			Node[] newChildren = new Node[_numOfChildren+1];
			if (_numOfChildren > 0) {
				System.arraycopy(_children, 0, newChildren, 0, _numOfChildren);
			}
			newChildren[_numOfChildren] = child; // insert child
			_children = newChildren;
			_numOfChildren++;
		}
		
		public void print() {
			print(0);
		}
		
		private void print(int height) {
			System.out.println("Node-tag : " + _tag + " Number of children " + _numOfChildren);
			if (_children != null) {
				for (int i = 0; i < _children.length; i++) {
					for (int j = 0; j < height + PRINTTAB; j++)
						System.out.print(" ");
					_children[i].print(height+PRINTTAB);
				}
			}
		}
		
		public void setTag(String tag) {_tag = tag;	}
		
		public String getTag() {return _tag;}
		
		public void setValue(String val) { _value = val; }
		public String getValue() { return _value; }
		
		
	}
	public MyHandlerTest(String fileName) {
		_stack = new Stack();
		_root = new Node();
		_fileName = fileName;
		_stack.push(_root);
	}
	
	public Node parse() {
		DefaultHandler handler = this;
		SAXParserFactory factory = SAXParserFactory.newInstance();
		try {
			SAXParser parser = factory.newSAXParser();
			parser.parse(_fileName,handler);
			
			
		} catch (Throwable t) {
			t.printStackTrace();
		}
		return _root;
	}
	
 	public void startDocument() throws SAXException {
 		
		System.out.println("start parsering document...");
	}
	
	public void endDocument() throws SAXException {
		
		System.out.println("end parsering document! ");
	}
	
	public void startElement(String uri, String localName,
			String qName, Attributes attrs) throws SAXException {
		Node currElement = new Node();
		if (_stack.empty()) {
			System.out.println("AN ERROR: Stack in parser is empty.");
			System.exit(1);
		}
		
		Node father =(Node) _stack.peek();
		String eName = localName;
		if ("".equals(eName))
			eName = qName;
		currElement.setTag(eName);
		father.insertChild(currElement);
		_stack.push(currElement);
		
		// add all attributes and their values as sons of currElement
		if (attrs != null) {
			for (int i = 0; i < attrs.getLength(); i++) {
				String aName = attrs.getLocalName(i);
				if ("".equals(aName))
					aName = attrs.getQName(i);
				Node currAttrName = new Node();
				currAttrName.setTag("@"+aName);
				Node currAttrVal = new Node();
				currAttrVal.setValue(attrs.getValue(i));
				currAttrName.insertChild(currAttrVal);
				currElement.insertChild(currAttrName);
			}
		}
		//System.out.println("start element : " + qName);
	}
	
	public void endElement(String uir, String localName, 
			String qName) throws SAXException {
		_stack.pop();
		//System.out.println("end element: " + qName);
	}
	
	public void characters(char buf[], int start, int length) 
			throws 	SAXException {
		String s = new String(buf, start, length);
		s.trim();
		if (s != null && !s.equalsIgnoreCase("")) {
			Node text = new Node();
			text.setValue(s);
			Node father =(Node) _stack.peek();
			father.insertChild(text);
			
		}
			
		//System.out.println("start characters : " + 	s);
		
	}
	
	// test client
	public static void main(String[] args) {
		MyHandlerTest my = new MyHandlerTest(args[0]);
		Node root = my.parse();
		root.print();
	}

}


/** simple.xml

    
        Tau
    
    
        Aj
    
    
        Ma
    

*/

上一篇:Tree Edit Distance - Tree Class
下一篇:没有了