Code example for SAXParser: Parsing XML documents

Methods: parse, setProperty

0
	 * Build an iDOM tree from a input source. 
	 */ 
	public final Document build(InputSource src)
	throws SAXException, IOException { 
		SAXHandler handler = newHandler();
		_parser.parse(src, handler);
		return handler.getDocument();
	} 
	/** 
	 * Build an iDOM tree from a URI string. 
	 */ 
	public final Document build(String uri)
	throws SAXException, IOException { 
		SAXHandler handler = newHandler();
		_parser.parse(uri, handler);
		return handler.getDocument();
	} 
	/** 
	 * Build an iDOM tree from a URL. 
	 */ 
	public final Document build(URL url)
	throws SAXException, IOException { 
		SAXHandler handler = newHandler();
		_parser.parse(url.toExternalForm(), handler);
		return handler.getDocument();
	} 
	/** 
	 * Build an iDOM tree from a Reader. 
	 */ 
	public final Document build(Reader src)
	throws SAXException, IOException { 
		SAXHandler handler = newHandler();
		_parser.parse(new InputSource(src), handler);
		return handler.getDocument();
	} 
	/** 
	 * Creates a SAX Handler. 
	 * Deriving class might override to provide a subclass of SAXHandler. 
	 */ 
	protected SAXHandler newHandler() throws SAXException { 
		SAXHandler handler = new SAXHandler(_factory);
 
		//configure handler 
		handler.setIgnoringElementContentWhitespace(_ignoreWhitespaces);
		handler.setExpandEntityReferences(_expandEntities);
		handler.setCoalescing(_coalescing);
		handler.setIgnoringComments(_ignoreComments);
		handler.setErrorHandler(_errHandler);
		handler.setEntityResolver(_resolver);
 
		//configure parser 
		setSafeProperty( 
			"http://xml.org/sax/properties/lexical-handler", 
			"http://xml.org/sax/handlers/LexicalHandler", 
			handler);
 
		if (!isExpandEntityReferences()) { //not expanding? 
			//then, we need declaration-handler 
			setSafeProperty( 
				"http://xml.org/sax/properties/declaration-handler", null, 
				handler);
		} 
		return handler;
	} 
	private void setSafeProperty(String name, String auxnm, Object value) {
		Throwable ex;
		try { 
			_parser.setProperty(name, value);
			return; 
		} catch (Throwable t) {
			ex = t;
		} 
		if (auxnm != null) {
			try { 
				_parser.setProperty(auxnm, value);
				return; 
			} catch (Throwable t) {
			} 
		}