Pastebin
Paste #166:
< previous paste - next paste>
Pasted by tdn
import org.xml.sax.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Collections;
import java.util.Random;
/** an example sink for content events.
It simply prints what it sees. */
final class Config
extends org.xml.sax.helpers.DefaultHandler
implements org.xml.sax.ContentHandler
{
/* collect data here */
public ElementNode root=null;
private ElementNode current=null;
final public class ElementNode extends Object{
private String type;
private String value;
private ArrayList nodes;
private ElementNode parent;
ElementNode(){
type=null;
value=null;
parent=null;
nodes=new ArrayList();
}
ElementNode(final String s_type,final ElementNode s_parent){
type=s_type;
value=null;
parent=s_parent;
nodes=new ArrayList();
}
public String type(){
return type;
}
public String value(){
return value;
}
public ElementNode node(final String type){
return node(type,0);
}
public ElementNode node(final String type,final int num){
int cnt=num;
Iterator i;
for (i=nodes.listIterator();i.hasNext();){
ElementNode n=(ElementNode)i.next();
if (type.equals(n.type())) {
if (cnt==0) return n;
cnt--;
}
}
return null;
}
public ElementNode node(final int num) {
if (num>=0&&num<nodes.size()) {return (ElementNode)nodes.get(num);}
return null;
}
int size(final String type) {
Iterator i;
int cnt=0;
for (i=nodes.listIterator();i.hasNext();){
ElementNode n=(ElementNode)i.next();
if (type.equals(n.type)) cnt++;
}
return cnt;
}
int size() { return nodes.size();}
public void print(){
System.out.println("<"+type+">");
if (nodes.size()!=0){
Iterator i;
for (i=nodes.listIterator();i.hasNext();){
ElementNode n=(ElementNode)i.next();
n.print();
}
}else{
System.out.println(value);
}
System.out.println("</"+type+">");
}
}
/* This is the parser part */
final public void startElement
( final String namespace, final String localname, final String type, final org.xml.sax.Attributes attributes )
throws org.xml.sax.SAXException
{
String s=new String( type );
/* Add a node, set as current node*/
if (current==null) {
root=new ElementNode("XML",null);
current=root;
}
ElementNode n=new ElementNode(s,current);
current.nodes.add(n);
current=n;
}
final public void endElement
( final String namespace, final String localname, final String type )
throws org.xml.sax.SAXException
{
/* End a node, use parent as current node */
if (current!=null) {
current=current.parent;
}
}
final public void characters
( final char[] ch, final int start, final int len )
{
final String text = new String( ch, start, len );
final String text1 = text.trim();
if( text1.length() > 0 ) {
if (current!=null) {
current.value=text1;
}
}
}
}
New Paste
Go to most recent paste.