I have a class called ReadXMLFile.java
that get a parsing on a XML file called infofermata.xml
. Using the DOMParser
, it works.
My app have a Button
in the MainActivity
that link in another activity (carry on ReadXMFile
using <ReadXMLFile.readXMLFile(this);>
) called "Page1". Now I would to switch data withdrawn from my DOMParser
(in ReadXMLFile.java
) in Page1, for visualize that data (stored into infofermata.xml
).
For this reason I suppose to use an Intent
, I write this rows in ReadXMLFile.java
after the parsing:
String testo1 = eElement.getElementsByTagName("idfermata").item(0).getTextContent();
Intent nuovaPagina = new Intent(this, Page1.class);
nuovaPagina.putExtra("NomeDati1", testo1);
["testo1" contains the string that I would switch in "Page1"]
But there are an error: non-static variable this cannot be referenced from a static context
.
This is ReadXMLFile
code:
/**
* Created by Giacomo B on 30/07/2015.
*/
package com.example.giacomob.myapplication;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class ReadXMLFile {
public static void readXMLFile(Context context) {
try {
// Log.i("MyActivity", "casa");
AssetManager assetManager = context.getAssets();
InputStream is = assetManager.open("infofermata.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(is);
// String filePath = "assets/infofermata.xml";
//File fXmlFile = new File(filePath);
//DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
//DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
//Document doc = dBuilder.parse(fXmlFile);
//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("fermata");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
// Log.i("MyActivity", "casa");
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
// System.out.println("Staff id : " + eElement.getAttribute("id"));
// String stringidfermata = "Id Fermata : " + eElement.getElementsByTagName("idfermata").item(0).getTextContent()"";
// Log.i("MyActivity", "\"Id Fermata : \" + eElement.getElementsByTagName(\"idfermata\").item(0).getTextContent()");
System.out.println("Id Fermata : " + eElement.getElementsByTagName("idfermata").item(0).getTextContent());
String testo1 = eElement.getElementsByTagName("idfermata").item(0).getTextContent();
Intent nuovaPagina = new Intent(this, Page1.class);
nuovaPagina.putExtra("NomeDati1", testo1);
System.out.println(testo1); //provo per vedere se stampa quello che ho messo nella variabile "testo1"
System.out.println("Naziome : " + eElement.getElementsByTagName("nazione").item(0).getTextContent());
System.out.println("Paese : " + eElement.getElementsByTagName("paese").item(0).getTextContent());
System.out.println("Via : " + eElement.getElementsByTagName("via").item(0).getTextContent());
}
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
This is infofermata.xml
code:
<?xml version="1.0" encoding="utf-8"?>
<fermata>
<idfermata>1</idfermata>
<nazione>Italia</nazione>
<paese>Lecce</paese>
<via>Viale Grassi</via>
</fermata>
This is MainActivity.java
code:
package com.example.giacomob.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ReadXMLFile.readXMLFile(this);
Button b_load=(Button)findViewById(R.id.button_send);
b_load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent openPage1 = new Intent(MainActivity.this, Page1.class);
startActivity(openPage1);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
What I can do now? Please, help me. Thanks :)