Basically i want to call a method from another class. I want the data()
method from DownloadXML.java
to be called in MainActivity.java
It is an android application
so here is the MainActivity.java
public class MainActivity extends ListActivity {
String item;
DownloadXML a = new DownloadXML();
a.data();
// SYNTAX ERROR ON TOKEN "DATA" IDENTIFIER EXPECTED AFTER THIS TOKEN
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
item = getItemFromXML(this);
}catch (XmlPullParserException e){
}catch (IOException e){
}
String[] items = item.split("\n");
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
}
public String getItemFromXML(Activity activity) throws XmlPullParserException, IOException{
StringBuffer stringBuffer = new StringBuffer();
Resources res = activity.getResources();
XmlResourceParser xpp = res.getXml(R.xml.items);
xpp.next();
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT){
if (eventType == XmlPullParser.START_TAG){
if (xpp.getName().equals("Item")){
stringBuffer.append(xpp.getAttributeValue(null, "Event") + "\n");
}
if (xpp.getName().equals("ab")){
stringBuffer.append(xpp.getAttributeValue(null, "when") + "\n");
}
if (xpp.getName().equals("cd")){
stringBuffer.append(xpp.getAttributeValue(null, "where") + "\n" + "----------------------------------------------" + "\n");
}
}
eventType = xpp.next();
}
return stringBuffer.toString();
}
}
Here is the DownloadXML.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;
public class DownloadXML {
DownloadXML(){
}
public void data() throws Exception{
URL url = new URL("http://localhost:8080/lab/lab.xml");
BufferedReader reader = new BufferedReader
(new InputStreamReader(url.openStream()));
BufferedWriter writer = new BufferedWriter
(new FileWriter("C:\\Users\\eyas\\workspace\\CebuHQ\\res\\xml\\items.xml"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
writer.write(line);
writer.newLine();
}
reader.close();
writer.close();
}
}
When i tried to instantiate it and call the method it has an error of:
DownloadXML a = new DownloadXML();
a.data(); SYNTAX ERROR ON TOKEN "DATA" IDENTIFIER EXPECTED AFTER THIS TOKEN
Can you guys help me with this. Thanks