mardi 8 avril 2014

Java - android l'analyse à l'aide de l'analyseur SAX - Stack Overflow


for some reason im having a hard time displaying whats on my xml file.


here is my xml file : where its local (tomcat) link is "http://*ocal*ost:8080/OFPB%20v2/contacts.xml"


<contacts>
<contact>
<name contact="contact">open</name>
<address address="address">will</address>
<phone phone="phone">9</phone>
<age age="age">8</age>
</contact>
<contact>
<name contact="contact">dean kurt</name>
<address address="address">mandaue</address>
<phone phone="phone">925738132</phone>
<age age="age">18</age>
</contact>
</contacts>

im trying to parse the xml with SAX parser in android and here are my codes : XMLHelper.java


package pcsalt.example.xmlparsingsaxdemo;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class XMLHelper extends DefaultHandler {
private String URL_Main = "http://*ocal*ost:8080/OFPB%20v2/contacts.xml";
String TAG = "XMLHelper";

Boolean currTag = false;
String currTagVal = "";
public PostValue post = null;
public ArrayList<PostValue> posts = new ArrayList<PostValue>();

public void get() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setContentHandler(this);
InputStream inputStream = new URL(URL_Main).openStream();
reader.parse(new InputSource(inputStream));
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}

// Receives notification of the start of an element
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {

Log.i(TAG, "TAG: " + localName);

currTag = true;
currTagVal = "";
if (localName.equals("contact")) {
post = new PostValue();
}

}

// Receives notification of end of element
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {

currTag = false;

if (localName.equalsIgnoreCase("name"))
post.setName(currTagVal);

else if(localName.equalsIgnoreCase("address"))
post.setAddress(currTagVal);

else if(localName.equalsIgnoreCase("phone"))
post.setPhone(Integer.parseInt(currTagVal));

else if(localName.equalsIgnoreCase("age"))
post.setAge(Integer.parseInt(currTagVal));


}

// Receives notification of character data inside an element
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {

if (currTag) {
currTagVal = currTagVal + new String(ch, start, length);
currTag = false;
}

}
}

PostValue.java


package pcsalt.example.xmlparsingsaxdemo;

public class PostValue {
String name, address;
int phone, age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}

MainActivity.java


package pcsalt.example.xmlparsingsaxdemo;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView tvResponse;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResponse = (TextView) findViewById(R.id.tvResponse);
new LoginAsync().execute();
}

class LoginAsync extends AsyncTask<Void, Void, Void> {
XMLHelper helper;

@Override
protected Void doInBackground(Void... params) {
helper = new XMLHelper();
helper.get();
return null;
}

@Override
protected void onPostExecute(Void result) {

StringBuilder builder = new StringBuilder();
for (PostValue post : helper.posts) {
builder.append("\nPost: " + post.getName());
builder.append("\nPublish Date: " + post.getAddress());
builder.append("\nGuid: " + post.getPhone() + "\n");
builder.append("\nGuid: " + post.getAge() + "\n");
}
tvResponse.setText(builder.toString());
}
}

}

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pcsalt.example.xmlparsingsaxdemo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="pcsalt.example.xmlparsingsaxdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

activity_main.xml


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/tvResponse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

</ScrollView>

it all runs great and all but it displays nothing. i have no idea why. can any one clear this one out for me. thanks :)


Note: i sensored the "l" in local and "h" in host because of post problems



for some reason im having a hard time displaying whats on my xml file.


here is my xml file : where its local (tomcat) link is "http://*ocal*ost:8080/OFPB%20v2/contacts.xml"


<contacts>
<contact>
<name contact="contact">open</name>
<address address="address">will</address>
<phone phone="phone">9</phone>
<age age="age">8</age>
</contact>
<contact>
<name contact="contact">dean kurt</name>
<address address="address">mandaue</address>
<phone phone="phone">925738132</phone>
<age age="age">18</age>
</contact>
</contacts>

im trying to parse the xml with SAX parser in android and here are my codes : XMLHelper.java


package pcsalt.example.xmlparsingsaxdemo;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class XMLHelper extends DefaultHandler {
private String URL_Main = "http://*ocal*ost:8080/OFPB%20v2/contacts.xml";
String TAG = "XMLHelper";

Boolean currTag = false;
String currTagVal = "";
public PostValue post = null;
public ArrayList<PostValue> posts = new ArrayList<PostValue>();

public void get() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setContentHandler(this);
InputStream inputStream = new URL(URL_Main).openStream();
reader.parse(new InputSource(inputStream));
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}

// Receives notification of the start of an element
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {

Log.i(TAG, "TAG: " + localName);

currTag = true;
currTagVal = "";
if (localName.equals("contact")) {
post = new PostValue();
}

}

// Receives notification of end of element
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {

currTag = false;

if (localName.equalsIgnoreCase("name"))
post.setName(currTagVal);

else if(localName.equalsIgnoreCase("address"))
post.setAddress(currTagVal);

else if(localName.equalsIgnoreCase("phone"))
post.setPhone(Integer.parseInt(currTagVal));

else if(localName.equalsIgnoreCase("age"))
post.setAge(Integer.parseInt(currTagVal));


}

// Receives notification of character data inside an element
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {

if (currTag) {
currTagVal = currTagVal + new String(ch, start, length);
currTag = false;
}

}
}

PostValue.java


package pcsalt.example.xmlparsingsaxdemo;

public class PostValue {
String name, address;
int phone, age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}

MainActivity.java


package pcsalt.example.xmlparsingsaxdemo;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView tvResponse;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResponse = (TextView) findViewById(R.id.tvResponse);
new LoginAsync().execute();
}

class LoginAsync extends AsyncTask<Void, Void, Void> {
XMLHelper helper;

@Override
protected Void doInBackground(Void... params) {
helper = new XMLHelper();
helper.get();
return null;
}

@Override
protected void onPostExecute(Void result) {

StringBuilder builder = new StringBuilder();
for (PostValue post : helper.posts) {
builder.append("\nPost: " + post.getName());
builder.append("\nPublish Date: " + post.getAddress());
builder.append("\nGuid: " + post.getPhone() + "\n");
builder.append("\nGuid: " + post.getAge() + "\n");
}
tvResponse.setText(builder.toString());
}
}

}

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pcsalt.example.xmlparsingsaxdemo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="pcsalt.example.xmlparsingsaxdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

activity_main.xml


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/tvResponse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

</ScrollView>

it all runs great and all but it displays nothing. i have no idea why. can any one clear this one out for me. thanks :)


Note: i sensored the "l" in local and "h" in host because of post problems


0 commentaires:

Enregistrer un commentaire