SAX
package saxblatt;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class GetCode extends DefaultHandler{
public StringBuffer result = new StringBuffer();
String lang;
String theClass;
public GetCode(String lang, String theClass) {
super();
this.lang = lang;
this.theClass = theClass;
}
boolean collect = false;
int innerCode = 0;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
innerCode++;
if (qName.equals("code")
&& theClass.equals(attributes.getValue("class"))
&& lang.equals(attributes.getValue("lang"))){
collect = true;
innerCode = 0;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("code") && innerCode==0) collect=false;
innerCode--;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (collect)result.append(new String(ch,start,length));
}
}