Created
April 7, 2012 05:37
-
-
Save yamanyar/2325574 to your computer and use it in GitHub Desktop.
Sax Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import org.xml.sax.*; | |
import org.xml.sax.helpers.*; | |
import org.apache.xerces.parsers.SAXParser; | |
public class Flour extends DefaultHandler { | |
float amount = 0; | |
public void startElement(String namespaceURI, String localName, | |
String qName, Attributes atts) { | |
if (namespaceURI.equals("http://recipes.org") | |
&& localName.equals("ingredient")) { | |
String n = atts.getValue("", "name"); | |
if (n.equals("flour")) { | |
String a = atts.getValue("", "amount"); // assume 'amount' exists | |
amount = amount + Float.valueOf(a).floatValue(); | |
} | |
} | |
} | |
public static void main(String[] args) { | |
Flour f = new Flour(); | |
SAXParser p = new SAXParser(); | |
p.setContentHandler(f); | |
try { | |
p.parse(args[0]); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
System.out.println(f.amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment