我正在尝试使用 SAX 解析 xml 文件。我不明白为什么它会出现异常。此代码适用于我几周前编写的另一个应用程序。这次我创建了一个 XML 文件:
wifi.xml
<?xml version="1.0" encoding="UTF-8"?>
<main>
<wifi bssid="..." >
<latitude>
...
</latitude>
<longitude>
...
</longitude>
<ssid>
...
</ssid>
</wifi>
<wifi bssid="..." >
<latitude>
...
</latitude>
<longitude>
...
</longitude>
<ssid>
...
</ssid>
</wifi>
</main>
要解析此文件,我使用以下代码:
WifiParser.java
...
public static Vector<WifiDescription> parse(InputStream file) {
Vector<WifiDescription> wifiDescription = new Vector<WifiDescription>();
Log.d("XML", "test Parsage");
try {
// create a XMLReader from SAXParser
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
Log.e("XML", "xmlReader");
WifiHandler wifiHandler = new WifiHandler();
Log.e("XML", "WifiHandler");
xmlReader.setContentHandler(wifiHandler);
Log.e("XML", "setContentHandler");
xmlReader.parse(new InputSource(file));
Log.e("XML", "parse");
wifiDescription = wifiHandler.getWifiDescription();
Log.e("XML", "wifi description");
}
catch(Exception ex) {
Log.d("XML", "WifiParser: parse() failed");
}
return wifiDescription;
}
...
Wifi处理程序.java
public class WifiHandler extends DefaultHandler {
public boolean isMain,isWifi,isLatitude,isLongitude,isSSID;
public int i;
public String tmpVal;
private Vector<WifiDescription> mWifiDescription;
private WifiDescription currentWifi;
/**
* @return
*/
public Vector<WifiDescription> getWifiDescription() {
Log.v("test parsage getWifiDescription","");
return mWifiDescription;
}
@Override
public void startDocument() throws SAXException {
// create new object
i=0;
Log.v("test parsage Start","");
this.mWifiDescription = new Vector<WifiDescription>();
}
@Override
public void endDocument() throws SAXException {
// nothing we need to do here
Log.v("test parsage End","");
}
@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
throws SAXException {
Log.v("test parsage StartElement","");
if(localName.equals("main")) {
isMain=true;
Log.v("test parsage main","");
}
if(localName.equals("wifi") && isMain ) {
this.currentWifi = new WifiDescription();
tmpVal = atts.getValue(WifiDescription.MSSID);
this.currentWifi.mBSSID=tmpVal;
isWifi=true;
}
else if(localName.equals("latitude") && isWifi ) {
tmpVal = atts.getValue(WifiDescription.LATITUDE);
this.currentWifi.mLatitude=Double.parseDouble(tmpVal);
}
else if(localName.equals("longitude") && isWifi ) {
tmpVal = atts.getValue(WifiDescription.LONGITUDE);
this.currentWifi.mLongitude=Double.parseDouble(tmpVal);
}
else if(localName.equals("ssid") && isWifi ) {
tmpVal = atts.getValue(WifiDescription.SSID);
this.currentWifi.mSSID=tmpVal;
}
}
@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
Log.v("test parsage EndElement","");
if(localName.equals("wifi")) {
//System.out.println("passe dans End landmark");
isWifi=false;
i++;
this.mWifiDescription.add(currentWifi);
}
Log.v("XML", "DONE \n");
}
}
我在 mainActivity 中调用它:
MainActivity.java
...
public void returnTableParseXml(){
try {
wifis = WifiParser.parse(getAssets().open("wifi.xml"));
}
catch (IOException e)
{
Log.d("XML Main","onCreate(): parse() failed");
return;
}
}
...
日志猫:
07-10 11:00:07.460: E/XML(16482): xmlReader
07-10 11:00:07.460: E/XML(16482): WifiHandler
07-10 11:00:07.460: E/XML(16482): setContentHandler
07-10 11:00:07.465: D/XML ex.getMessage(16482): null
07-10 11:00:07.465: D/XML(16482): WifiParser: parse() failed
ex.printStackTrace();
07-10 13:33:35.480: W/System.err(29590): java.lang.NullPointerException
07-10 13:33:35.480: W/System.err(29590): at java.lang.StringToReal.parseDouble(StringToReal.java:244)
07-10 13:33:35.480: W/System.err(29590): at java.lang.Double.parseDouble(Double.java:295)
07-10 13:33:35.480: W/System.err(29590): at com.stage.ecolenavale.parser.WifiHandler.startElement(WifiHandler.java:66)
07-10 13:33:35.480: W/System.err(29590): at org.apache.harmony.xml.ExpatParser.startElement(ExpatParser.java:143)
07-10 13:33:35.480: W/System.err(29590): at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
07-10 13:33:35.480: W/System.err(29590): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:513)
07-10 13:33:35.480: W/System.err(29590): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:474)
07-10 13:33:35.480: W/System.err(29590): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:321)
07-10 13:33:35.480: W/System.err(29590): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:279)
07-10 13:33:35.480: W/System.err(29590): at com.stage.ecolenavale.parser.WifiParser.parse(WifiParser.java:28)
07-10 13:33:35.480: W/System.err(29590): at com.stage.ecolenavale.MainActivity.returnTableParseXml(MainActivity.java:420)
07-10 13:33:35.480: W/System.err(29590): at com.stage.ecolenavale.MainActivity.onCreate(MainActivity.java:161)
我不明白为什么。这是我的 XML 文件的问题吗?