要求:解析下面这样的XML文件,这个是非常规的XML文件
<adaptCountry name="Orange ES" mcc="214" mnc="03" date_format="dd-MM-yyyy" time="12" />由于担心会出现ANR,所以开启了一个子线程去做这个事情。
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; public class XMLParseTimeDateThread extends Thread { public void run() { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); String mXMLFilePath = "date_format_conf.xml";// xr.setContentHandler(new XMLParse()); FileInputStream fis = new FileInputStream(new File(mXMLFilePath)); if (fis != null) { xr.parse(new InputSource(fis)); } } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /* * <adaptCountry name="Orange ES" mcc="214" mnc="03" date_format="dd-MM-yyyy" time="12" /> */ class XMLParse extends DefaultHandler { public String mNumeric; public String mNum_match; String strMccMnc = null; @Override public void startDocument() throws SAXException { mNumeric = ""; mNum_match = ""; } @Override public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException { System.out.println(qName); System.out.println(localName); String Mcc; String mnc; String strMccMnc = "46002";// SystemProperties.get("gsm.sim.operator.numeric"); if (strMccMnc == null || strMccMnc.length() < 5) { return; } Mcc = strMccMnc.substring(0,3); mnc = strMccMnc.substring(3,strMccMnc.length()); System.out.println(attributes.getLength()); for (int i = 0; i < attributes.getLength(); i++) { String attr_name = attributes.getLocalName(i); String attr_value = attributes.getValue(i); System.out.print(attr_name+" "); System.out.println(attr_value); } } @Override public void endElement(String uri,String qName) throws SAXException { if ("virtualNet".equals(qName)) { mNumeric = ""; mNum_match = ""; } } } /* public static void main(String[] args) { // TODO Auto-generated method stub new XMLParseTimeDateThread().start(); }*/