Created
April 8, 2015 09:22
-
-
Save anonymous/0757f4f1c6c11fe0f026 to your computer and use it in GitHub Desktop.
This file contains 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 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