القائمة الرئيسية

الصفحات

مرحبا بكم اخواني الكرام وكما عودناكم كثيرا من الوقت المفيد..

معانا اليوم شرح جميل ونتمنى أن ينال اعجابكم..

موضوعنا اليوم عن SAX Parser Xml
اذآ...
يوضح هذا المثال أندرويد كيفية تحليل شمل بسيط يحتوي
على تفاصيل الموظف باستخدام محلل SAX وعرض النتيجة في سبينر. هذا المثال يخزن ملف شمل في مجلد أصول بروجيكتوس ويفتح الملف كما إنبوتستريم باستخدام أسيتماناجر. على زر انقر فوق الحدث، نتحدد شمل وعرض معرف الموظف والاسم في سبينر، وعندما يتم تحديد عنصر، يتم عرض تفاصيل الموظف الكامل في رسالة نخب.


لزيادة شمل في الروبوت، يمكنك استخدام واجهة برمجة التطبيقات و دوس شورس التي تقدمها منصة جافا. بالإضافة إلى هذين اثنين من الاريطال، يوفر أندرويد شملبولبارسر التي تشبه محلل ستاكس، كما أيضا محلل أوسد لاستخدامها في الروبوت.

ثم انشاء XML file





2163
Kumar
Development
Permanent
kumar@tot.com


6752
Siva
DB
Contract
siva@tot.com


6763
Timmy
Testing
Permanent
timmy@tot.com


Main layout file


android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button" />

android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

قم بانشاء.

.

Employee.java
إنشاء AFLADEJ.COAVA جديد في حزمة �com.theoputorutials.android.beans and ونسخ التعليمات البرمجية التالية.

public class Employee {
private String name;
private int id;
private String department;
private String type;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return id + ": " + name;
}
public String getDetails() {
String result = id + ": " + name + " " + department + "-" + type + " " + email;
return result;
}
}

انشاء ملف.

SAXXMLHandler class
إنشاء فئة جافا جديدة �saxxmlhandler.java in في حزمة �com.theoputorutials.android.xml and ونسخ التعليمات البرمجية التالية.

public class SAXXMLHandler extends DefaultHandler {
private List employees;
private String tempVal;
private Employee tempEmp;

public SAXXMLHandler() {
employees = new ArrayList();
}
public List getEmployees() {
return employees;
}
// Event Handlers
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
// reset
tempVal = "";
if (qName.equalsIgnoreCase("employee")) {
// create a new instance of employee
tempEmp = new Employee();
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
tempVal = new String(ch, start, length);
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase("employee")) {
// add it to the list
employees.add(tempEmp);
} else if (qName.equalsIgnoreCase("id")) {
tempEmp.setId(Integer.parseInt(tempVal));
} else if (qName.equalsIgnoreCase("name")) {
tempEmp.setName(tempVal);
} else if (qName.equalsIgnoreCase("department")) {
tempEmp.setDepartment(tempVal);
} else if (qName.equalsIgnoreCase("type")) {
tempEmp.setType(tempVal);
} else if (qName.equalsIgnoreCase("email")) {
tempEmp.setEmail(tempVal);
}
}
}

SAX (أبي بسيطة لشمل) هو أبي واجهة محلل متساعدة في الحدث مع عدد من طرق الاتصالات التي سيتم استدعاءها عندما تكون الأحداث مثل عنصر البدء، والانصير النهائي، سمات الخ، خلال الخزانة. هذه الفئة حمل ملف شمل يحتوي على تفاصيل الموظفين والمخازن في قائمة كائن موظف.

انشاء..


Parser XML SAX class
إنشاء فئة جافا جديدة saxxmlparser.java in في حزمة com.theoputorutials.android.xml and ونسخ التعليمات البرمجية التالية.

public class SAXXMLParser {
public static List parse(InputStream is) {
List employees = null;
try {
// create a XMLReader from SAXParser
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser() .getXMLReader();
// create a SAXXMLHandler
SAXXMLHandler saxHandler = new SAXXMLHandler();
// store handler in XMLReader
xmlReader.setContentHandler(saxHandler);
// the process starts
xmlReader.parse(new InputSource(is));
// get the `Employee list`
employees = saxHandler.getEmployees();
} catch (Exception ex) {
Log.d("XML", "SAXXMLParser: parse() failed");
}
// return Employee list
return employees;
}
}

قم بانشاء
SAXParserActivity كلاس

public class MainActivity extends Activity implements
OnClickListener, OnItemSelectedListener {
Button button;
Spinner spinner;
List employees = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewsById();
button.setOnClickListener(this);
}
private void findViewsById() {
button = (Button) findViewById(R.id.button);
spinner = (Spinner) findViewById(R.id.spinner);
}
public void onClick(View v) {
try {
employees = SAXXMLParser.parse(getAssets().open("employee.xml"));
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, employees);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onItemSelected(AdapterView parent, View view, int pos, long id) {
Employee employee = (Employee) parent.getItemAtPosition(pos);
Toast.makeText(parent.getContext(), employee.getDetails(), Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterViewarg0) {
}
}

الى هناء قد انتهينا من الشرح موضوع SAX XML Parser نتمنى لكم الاستفاده من منه

مدونه...
عالم البرمجه

تعليقات