package org.hjw.service; import java.util.ArrayList; import java.util.List; import org.hjw.model.Product; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class XML2Product extends DefaultHandler { private Product product; private List<Product> products; StringBuffer buffer = new StringBuffer(); public List<Product> getProducts() { return products; } @Override public void characters(char[] ch, int start, int length) throws SAXException { buffer.append(ch, start, length); super.characters(ch, start, length); } @Override public void startDocument() throws SAXException { products = new ArrayList<Product>(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (localName.equals("product")) { product = new Product(); } super.startElement(uri, localName, qName, attributes); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (localName.equals("product")) { products.add(product); } else if (localName.equals("id")) { product.setId(Integer.parseInt(buffer.toString().trim())); buffer.setLength(0); } else if (localName.equals("name")) { product.setName(buffer.toString().trim()); buffer.setLength(0); } else if (localName.equals("price")) { product.setPrice(Float.parseFloat(buffer.toString().trim())); buffer.setLength(0); } super.endElement(uri, localName, qName); } }
InputStream is = getResources().openRawResource(R.raw.products); XML2Product xml2Product = new XML2Product(); android.util.Xml.parse(is, Xml.Encoding.UTF_8, xml2Product); List<Product> products = xml2Product.getProducts();
writer.beginObject(); writer.name("id").value(product.id);//key-value对应 writer.name("name").value(product.name); writer.endObject();
一般来说,我们要尽可能的分层次的解析,让代码实现更好的分离,流操作-数组操作-对象操作。
JsonReader--------具体的读操作:
reader.beginObject(); while (reader.hasNext()) { String field = reader.nextName();//游标往后移 if (field.equals("id")) { id = reader.nextString();//获取数据 } else if (field.equals("name")) { name = reader.nextString(); } else { reader.skipValue(); } } reader.endObject();
一般来说,只要Reader初始化输入流,通过ArrayList接收,分层次的读取便可实现功能。
public void onClick_SaveImageToSDCard(View view) { try { FileOutputStream fos = new FileOutputStream( android.os.Environment.getExternalStorageDirectory() + "/image.jpg");//获取sd卡根目录 InputStream is = getResources().getAssets().open("image.jpg"); byte[] buffer = new byte[8192]; int count = 0; while ((count = is.read(buffer)) >= 0) { fos.write(buffer, 0, count);//写文件 } fos.close(); is.close(); Toast.makeText(this, "已成功将图像文件写到SD卡上.", Toast.LENGTH_LONG).show(); } catch (Exception e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); } }
读文件:
public void onClick_ReadImageFromSDCard(View view) { String filename = android.os.Environment.getExternalStorageDirectory() + "/image.jpg"; if (!new File(filename).exists()) { Toast.makeText(this, "还没有往SD卡写入图像文件", Toast.LENGTH_LONG).show(); return; } ImageView imageView = (ImageView) findViewById(R.id.imageview); try { FileInputStream fis = new FileInputStream(filename);//初始化文件输入流 Bitmap bitmap = BitmapFactory.decodeStream(fis); imageView.setImageBitmap(bitmap); fis.close(); } catch (Exception e) { } }
如果不用数据库的话,以上操作都能实现数据操作,不过更推荐xml文件读写,当然如果在网络应用中,用JSON是最好的轻量级选择。