0

我正在使用 SAX XML Parser 从运行良好的 XML 文件中获取值。这是我主要活动中的方法:

public void parseXML() {

    String parsedData = "";

    try {

        Log.w("AndroidParseXMLActivity", "Start");
        /** Handling XML */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        XMLContentHandler myXMLHandler = new XMLContentHandler();
        xr.setContentHandler(myXMLHandler);

        AssetManager assetManager = getAssets();
        InputStream inputStream = null;
        try {
            inputStream = assetManager.open("calimero.xml");
        } catch (IOException e) {
            Log.e("tag", e.getMessage());
        }
        xr.parse(new InputSource(inputStream)); 

        ArrayList<Datapoint> itemsList = myXMLHandler.getItemsList();
        int datapoint_size = itemsList.size();
        String xml_names[]  = new String [datapoint_size];
        String xml_states[]  = new String [datapoint_size];
        String xml_dptIDs[] = new String [datapoint_size];
        String xml_groupadresses[]  = new String [datapoint_size];
        String xml_mainNumbers[]  = new String [datapoint_size];

        for(int i=0;i<itemsList.size();i++){
            Datapoint item = itemsList.get(i);
            parsedData = parsedData + "----->\n";
            parsedData = parsedData + "Name: " + item.getName() + "\n";
            xml_names[i] = item.getName();
            parsedData = parsedData + "stateBased: " + item.getStateBased() + "\n";
            xml_states[i] = item.getStateBased();
            parsedData = parsedData + "mainNumber: " + item.getMainNumber() + "\n";
            xml_mainNumbers[i] = item.getMainNumber();
            parsedData = parsedData + "dptID: "+ item.getDptID() + "\n";
            xml_dptIDs[i] = item.getDptID();
            parsedData = parsedData + "Groupadress: "+ item.getGroupadress() + "\n";
            xml_groupadresses[i] = item.getGroupadress();
        }
        Log.w("AndroidParseXMLActivity", "Done");
    }
    catch (Exception e) {
        Log.w("AndroidParseXMLActivity",e );
    }
    Log.w("AndroidParseXMLActivity", names[0]);
    Log.w("AndroidParseXMLActivity", mainNumbers[2]);
    Log.w("AndroidParseXMLActivity", dptIDs[3]);
    //xmlOutput.setText(parsedData);
}

我想在我的方法之外使用名称、mainNumber 等值。但我不明白......我知道这是java基础......请帮助我,谢谢!

4

2 回答 2

1

如果你想在整个类中使用你的 String 数组,你必须在类中定义它。然后该变量可用于您类中的所有函数。

    public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        StringArray testString = new StringArray();
        testString.setArray();
        testString.printArray();
    }

    static private class StringArray{
        String test[];

        public void setArray(){
            test = new String[5];
            for (int i = 0 ; i < test.length; i++){
                test[i] = "test";
            }
        }

        public void printArray(){
            for (int i = 0 ; i < test.length; i++){
                System.out.println(test[i]);
            }
        }
    }
}
于 2012-09-14T12:01:45.647 回答
0

我解决了这个问题。我现在使用 Datapoint 作为返回值:

public Datapoint parseXML(){
    Datapoint item = null;

    for(){
        ...
    }

    return item;
}

然后在我的主要功能中我可以使用

item = parseXML();
String name = item.getName();
于 2012-09-14T13:45:16.980 回答