Skip to content

Instantly share code, notes, and snippets.

Created April 8, 2015 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0757f4f1c6c11fe0f026 to your computer and use it in GitHub Desktop.
Save anonymous/0757f4f1c6c11fe0f026 to your computer and use it in GitHub Desktop.
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class SaxTest {
private static class DefaultContentHandler extends DefaultHandler {
private Map<String, String> ctMap = new HashMap<>();
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(!"CS".equals(localName)) {
return;
}
String id = attributes.getValue("id");
String ct = attributes.getValue("ct");
ctMap.put(id, ct);
}
public Map<String, String> getCtMap() {
return ctMap;
}
}
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SAXParser saxParser = spf.newSAXParser();
DefaultContentHandler contentHandler = new DefaultContentHandler();
saxParser.parse(new File("data.xml"), contentHandler);
Map<String, String> results = contentHandler.getCtMap();
System.out.println(results);
/*{3=503459, 2=225331, 5=0}*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment