2

我正在尝试使用 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 文件的问题吗?

4

2 回答 2

0

这是我用来用 SAX 解析文件的代码:

WifiHandler.java


公共类 WifiHandler 扩展 DefaultHandler {

public boolean isMain;

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;

        tmpVal = atts.getValue(WifiDescription.LATITUDE);
        this.currentWifi.mLatitude=tmpVal;

        tmpVal = atts.getValue(WifiDescription.LONGITUDE);
        this.currentWifi.mLongitude=tmpVal;

        tmpVal = atts.getValue(WifiDescription.SSID);
        this.currentWifi.mSSID=tmpVal;
        isWifi=true;
    }
}

@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");
}

}

WifiParser.java


public class WifiParser  {

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 ex.getMessage", "" + ex.getMessage());
        ex.printStackTrace();

        Log.d("XML", "WifiParser: parse() failed");
    }

    return wifiDescription;
}

}

MainActivity.java


    public void returnTableParseXml(){
    try {
        wifis = WifiParser.parse(getAssets().open("wifi.xml"));
    }
    catch (IOException e) 
    {
        Log.d("XML Main","onCreate(): parse() failed");
        return;
    }
}

WifiDescription.java


公共类 WifiDescription {

    // Description Wifi
    public String mLatitude;
    public String mLongitude;
    public String mBSSID;
    public String mSSID;
    public String mName;


    public static final String WIFI = "wifi";

    // vector usage
    public static final String LATITUDE = "latitude";
    public static final String LONGITUDE ="longitude";
    public static final String SSID = "ssid";
    public static final String MSSID ="bssid";
    public static final String NAME ="name";

}

Wifi.xml


<main>
    <wifi
        bssid="..:..:..:..:..:.."
        latitude="..."
        longitude="..."
        ssid="..." >
    </wifi>
       ...
</main>

于 2013-07-10T12:09:19.590 回答
0

您正在尝试转换 XML 文件中不存在的属性值:

else if(localName.equals("latitude") && isWifi ) {
    tmpVal = atts.getValue(WifiDescription.LATITUDE);
    this.currentWifi.mLatitude=Double.parseDouble(tmpVal);
}

在这里,tmpVal将为空,因此是异常。

之前,你有

<wifi
    bssid="..:..:..:..:..:.."
    latitude="..."
    longitude="..."
    ssid="..." >
</wifi>

现在,你有

<wifi bssid="..." >
   <latitude>
       ...
   </latitude>
</wifi>

所以,你想要的值不是属性,而是 <lattitude> 元素的字符内容。您必须使用该characters()方法阅读该内容。这会使代码更复杂,因为您必须收集字符内容,并将其读取到正确的变量中endElement

于 2013-07-10T12:17:46.277 回答