مرحبا بكم اخواني الكرام وكما عودناكم كثيرا من الوقت المفيد..
معانا اليوم شرح جميل ونتمنى أن ينال اعجابكم..
موضوعنا اليوم عن SAX Parser Xml
اذآ...
يوضح هذا المثال أندرويد كيفية تحليل شمل بسيط يحتوي على تفاصيل الموظف باستخدام محلل SAX وعرض النتيجة في سبينر. هذا المثال يخزن ملف شمل في مجلد أصول بروجيكتوس ويفتح الملف كما إنبوتستريم باستخدام أسيتماناجر. على زر انقر فوق الحدث، نتحدد شمل وعرض معرف الموظف والاسم في سبينر، وعندما يتم تحديد عنصر، يتم عرض تفاصيل الموظف الكامل في رسالة نخب.
لزيادة شمل في الروبوت، يمكنك استخدام واجهة برمجة التطبيقات و دوس شورس التي تقدمها منصة جافا. بالإضافة إلى هذين اثنين من الاريطال، يوفر أندرويد شملبولبارسر التي تشبه محلل ستاكس، كما أيضا محلل أوسد لاستخدامها في الروبوت.
ثم انشاء XML file
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
قم بانشاء.
.
إنشاء 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() {
public void setName(String name) {
public int getId() {
public void setId(int id) {
public String getDepartment() {
public void setDepartment(String department) {
public String getType() {
public void setType(String type) {
public String getEmail() {
public void setEmail(String email) {
@Override
public String toString() {
public String getDetails() {
}
}
انشاء ملف.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() {
public List getEmployees() {
// Event Handlers
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
public void characters(char[] ch, int start, int length)
throws SAXException {
public void endElement(String uri, String localName, String qName)
throws SAXException {
}
private List
private String tempVal;
private Employee tempEmp;
public SAXXMLHandler() {
employees = new ArrayList();
}public List
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();
}
}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);
}
}// 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 {
Log.d("XML", "SAXXMLParser: parse() failed");
}
// return Employee list
return employees;
}
}
قم بانشاءpublic static List
List
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) {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();
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) {
private void findViewsById() {
public void onClick(View v) {
try {
e.printStackTrace();
}
}
@Override
public void onItemSelected(AdapterView parent, View view, int pos, long id) {
@Override
public void onNothingSelected(AdapterViewarg0) {
}
}
OnClickListener, OnItemSelectedListener {
Button button;
Spinner spinner;
List
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewsById();
button.setOnClickListener(this);
}setContentView(R.layout.activity_main);
findViewsById();
button.setOnClickListener(this);
private void findViewsById() {
button = (Button) findViewById(R.id.button);
spinner = (Spinner) findViewById(R.id.spinner);
}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) {ArrayAdapter
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
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();
}Toast.makeText(parent.getContext(), employee.getDetails(), Toast.LENGTH_LONG).show();
@Override
public void onNothingSelected(AdapterViewarg0) {
}
}
الى هناء قد انتهينا من الشرح موضوع SAX XML Parser نتمنى لكم الاستفاده من منه
مدونه...
عالم البرمجه
تعليقات
إرسال تعليق