// Create a handler for SAX events
DefaultHandler handler = new MyHandler();
// Parse an XML file using SAX;
// The Quintessential Program to Parse an XML File Using SAX
parseXmlFile("infilename.xml", handler, true);
// This class listens for startElement SAX events
static class MyHandler extends DefaultHandler {
// This method is called when an element is encountered
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) {
// Get the number of attribute
int length = atts.getLength();
// Process each attribute
for (int i=0; i<length; i++) {
// Get names and values for each attribute
String name = atts.getQName(i);
String value = atts.getValue(i);
// The following methods are valid only if the parser is namespace-aware
// The uri of the attribute's namespace
String nsUri = atts.getURI(i);
// This is the name without the prefix
String lName = atts.getLocalName(i);
}
}
}
Thanks for posting this SAX coding example. I have been trying to find such an example of how to do this in several places and books, and they all ignored this feature of SAX (assumed all data would simply be between the start and end tags of an element).
By the way, the Captcha is very difficult to read. I only succeeded after several attempts.
Hi, interesting blog. I also get knowledge from your blog. That was a great help to me.
If you want to buy real estate, you would have to receive the business loans. Furthermore, my mother all the time utilizes a auto loan, which seems to be really firm.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.git.flip.web.bankgateway;
import java.util.ArrayList;
import java.util.List;
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;
/**
*
* @author 06083
*/
public class XMLParser {
public static void main(String argv[]) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
XmlProcesser handler=new XmlProcesser();
saxParser.parse("book.xml", handler);
for(XMLResp xr:handler.resplist){
System.out.println("tag :"+xr.getTag());
System.out.println("Atribute :"+xr.getAttribute());
System.out.println("value :"+xr.getValue());
}
} catch (Exception e) {
}
}
}
class XmlProcesser extends DefaultHandler{
XMLResp xmlResponse=null;
Listresplist=new ArrayList();
Listtemp=new ArrayList();
boolean endtag=false;
@Override
public void startElement(String uri, String localName,String qName,
Attributes attributes) throws SAXException {
xmlResponse=new XMLResp();
xmlResponse.setTag(qName);
xmlResponse.setAttribute(attributes.getValue("name"));
temp.add(xmlResponse);
}
@Override
public void characters(char ch[], int start, int length) throws SAXException {
if(!endtag){
xmlResponse=temp.get(0);
xmlResponse.setValue(new String(ch, start, length));
resplist.add(xmlResponse);
endtag=false;
temp.remove(xmlResponse);
}
endtag=false;
}
@Override
public void endElement(String uri, String localName,
String qName) throws SAXException {
endtag=true;
}
}
class XMLResp {
String tag;
String attribute;
String value;
public String getAttribute() {
return attribute;
}
public void setAttribute(String attribute) {
this.attribute = attribute;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Such marathons are rather challenging, but they give you an unsurpassed feeling of freedom and control over your body, so I envy you a bit.
anyone can give the code this code using qt please :(