sina4everafter
شنبه 11 مرداد 1393, 13:29 عصر
سلام دوستان.
شایدباورتون نشه ولی الان تقریبا یک ماهی هست که می خوام این فایل xml و پارس کنم ولی نمیشه. تو روخدا اگر اطلاعی دارین راهنماییم کنید. ممنون.
این فایل xml من که داخل فولدر assets تعریفش کردم.
<?xml version="1.0" encoding="utf-8"?>
<categories>
<category>
<id>length</id>
<name>طول</name>
</category>
<category>
<id>Power</id>
<name>توان</name>
</category>
<category>
<id>Energy</id>
<name>انرژی</name>
</category>
<category>
<id>Time</id>
<name>زمان</name>
</category>
<category>
<id>Temperature</id>
<name>درجه حرارت</name>
</category>
<category>
<id>Mass</id>
<name>جرم</name>
</category>
<category>
<id>Area</id>
<name>سطح</name>
</category>
<category>
<id>Angle</id>
<name>زاویه</name>
</category>
<category>
<id>Speed</id>
<name>سرعت</name>
</category>
<category>
<id>Volume</id>
<name>حجم</name>
</category>
<category>
<id>Pressure</id>
<name>فشار</name>
</category>
</categories>
یه کلاس دارم به نام Category که از متد set و get استفاده کردم.
public class Category {
String name, id;
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
این هم کلاسی هست که می خوام اون xml و پارس کنم. این کلاس فقط باید کارش پارس کردن اون فایل باشه.
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.content.Context;
import com.asr24.cando.Category.Category;
public class CategoryParser {
static Context context;
static public class CategoryPullParser{
static ArrayList<Category> parseCategory(InputStream inputStream) throws XmlPullParserException, IOException{
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser() ;
inputStream = context.getApplicationContext().getAssets().open("categories.xml");
parser.setInput(inputStream, "utf-8");
Category category = null;
ArrayList<Category> categoryList = new ArrayList<Category>();
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_TAG:
if (parser.getName().equals("id")) {
category = new Category();
category.setId(parser.getAttributeValue(null, "id"));
}else if (parser.getName().equals("name")) {
category.setName(parser.getAttributeValue(null, "name"));
}
break;
case XmlPullParser.END_TAG:
if (parser.getName().equals("category")) {
categoryList.add(category);
category = null;
}
break;
default:
break;
}
eventType = parser.next();
}
return categoryList;
}
}
}
این هم کلاس اصلی که قراره وقتی که اون فایله xml و که پارس کرد به تعداد اون فایل دکمه درست کنه و بخش name در فایل xml و به اسم دکمه و بخش id در فایل xml و برای آی دی دکمه در نظر بگیره.
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
import com.asr24.cando.Category.Category;
public class XmlPullParserActivity extends Activity {
Button btn;
ArrayList<Category> categories= new ArrayList<Category>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
LinearLayout li = (LinearLayout) findViewById(R.id.li);
for(int i = 0; i < categories.size(); i++){
btn = new Button(this);
li.addView(btn);
btn.setText(categories.get(i).getName());
}
}
}
تو رو خدا اگر می تونید کمک کنید که خیلی گیرم. ممنون.
شایدباورتون نشه ولی الان تقریبا یک ماهی هست که می خوام این فایل xml و پارس کنم ولی نمیشه. تو روخدا اگر اطلاعی دارین راهنماییم کنید. ممنون.
این فایل xml من که داخل فولدر assets تعریفش کردم.
<?xml version="1.0" encoding="utf-8"?>
<categories>
<category>
<id>length</id>
<name>طول</name>
</category>
<category>
<id>Power</id>
<name>توان</name>
</category>
<category>
<id>Energy</id>
<name>انرژی</name>
</category>
<category>
<id>Time</id>
<name>زمان</name>
</category>
<category>
<id>Temperature</id>
<name>درجه حرارت</name>
</category>
<category>
<id>Mass</id>
<name>جرم</name>
</category>
<category>
<id>Area</id>
<name>سطح</name>
</category>
<category>
<id>Angle</id>
<name>زاویه</name>
</category>
<category>
<id>Speed</id>
<name>سرعت</name>
</category>
<category>
<id>Volume</id>
<name>حجم</name>
</category>
<category>
<id>Pressure</id>
<name>فشار</name>
</category>
</categories>
یه کلاس دارم به نام Category که از متد set و get استفاده کردم.
public class Category {
String name, id;
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
این هم کلاسی هست که می خوام اون xml و پارس کنم. این کلاس فقط باید کارش پارس کردن اون فایل باشه.
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.content.Context;
import com.asr24.cando.Category.Category;
public class CategoryParser {
static Context context;
static public class CategoryPullParser{
static ArrayList<Category> parseCategory(InputStream inputStream) throws XmlPullParserException, IOException{
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser() ;
inputStream = context.getApplicationContext().getAssets().open("categories.xml");
parser.setInput(inputStream, "utf-8");
Category category = null;
ArrayList<Category> categoryList = new ArrayList<Category>();
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_TAG:
if (parser.getName().equals("id")) {
category = new Category();
category.setId(parser.getAttributeValue(null, "id"));
}else if (parser.getName().equals("name")) {
category.setName(parser.getAttributeValue(null, "name"));
}
break;
case XmlPullParser.END_TAG:
if (parser.getName().equals("category")) {
categoryList.add(category);
category = null;
}
break;
default:
break;
}
eventType = parser.next();
}
return categoryList;
}
}
}
این هم کلاس اصلی که قراره وقتی که اون فایله xml و که پارس کرد به تعداد اون فایل دکمه درست کنه و بخش name در فایل xml و به اسم دکمه و بخش id در فایل xml و برای آی دی دکمه در نظر بگیره.
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
import com.asr24.cando.Category.Category;
public class XmlPullParserActivity extends Activity {
Button btn;
ArrayList<Category> categories= new ArrayList<Category>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
LinearLayout li = (LinearLayout) findViewById(R.id.li);
for(int i = 0; i < categories.size(); i++){
btn = new Button(this);
li.addView(btn);
btn.setText(categories.get(i).getName());
}
}
}
تو رو خدا اگر می تونید کمک کنید که خیلی گیرم. ممنون.