0

I am using a SAX XML Parser to get values out of a XML file that works fine. This is the method in my main activity:

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

I want to use the values like name, mainNumber and so on outside my method. But I don't get it... I know this are java basic.... Please help me, thanks!

2 Answers 2

1

If you want to use your String array in the whole class you have to define it in the class. Then the variable are avaliable for all functions in your class.

    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]);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks I know that but my string array should have the size of the Array list from the parsed XML file. So i would have to insert the code to implement and start the XML handler also in the variable declaration at the beginning of the class, or?
You can define it in the class like String name[] = null; and init it in your function like name = new String[datapoint_size];
Ok that sounds logical, I added the declarations to my code. I have also added the following line to see if it works Log.i("DataHandler", xml_names[1]); but now I am getting a null pointer exception. Do you know why?
Where have you add this line? How large is your field?
I have added this line after calling the parseXML method. At the moment there are 5 items created by the for-loop
|
0

I solved the problem. I now use the Datapoint as return value:

public Datapoint parseXML(){
    Datapoint item = null;

    for(){
        ...
    }

    return item;
}

And in my main function I can then use

item = parseXML();
String name = item.getName();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.