获取XML文件的基本思路是,通过getResources().getXml()获的XML原始文件,得到XmlResourceParser对象,通过该对象来判断是文档的开头还是结尾,是某个标签的开始还是结尾,并通过一些获取属性的方法来遍历XML文件,从而访问XML文件的内容,下面是一个访问XML文件内容的例子,并将内容更显示在一个TextView上
数据写入xml:
ReadXMLTest.java
private String WriteToString() { XmlSerializer serializer = Xml.newSerializer(); StringWriter writer = new StringWriter(); try { serializer.setOutput(writer); serializer.startDocument("utf-8", true); serializer.startTag("", "users"); serializer.startTag("", "userName"); serializer.text(txtUser.getText().toString()); serializer.endTag("", "userName"); serializer.startTag("", "userEmail"); serializer.text(txtEmail.getText().toString()); serializer.endTag("", "userEmail"); serializer.startTag("", "passWord"); serializer.text(txtPass.getText().toString()); serializer.endTag("", "passWord"); serializer.endTag("", "users"); serializer.endDocument(); } catch (IllegalArgumentException e) { // TODO: handle exception e.printStackTrace(); } catch (IllegalStateException e) { // TODO: handle exception e.printStackTrace(); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } return writer.toString(); } //将字符串保存为apk的私有文件 private boolean writeToXml(String str) { try { OutputStream out = openFileOutput("users.xml",MODE_PRIVATE); OutputStreamWriter outWriter = new OutputStreamWriter(out); try { outWriter.write(str); outWriter.close(); out.close(); return true; } catch (IOException e) { // TODO: handle exception return false; } } catch (Exception e) { // TODO: handle exception return false; } }
调用时:
if(writeToXml(WriteToString()))
{
//成功
}
数据读取 xml:
private String ReadXmlUser(String tag) { String re=""; DocumentBuilderFactory documentBuilderFactory; DocumentBuilder documentBuilder; Document document; try { documentBuilderFactory=DocumentBuilderFactory.newInstance(); documentBuilder=documentBuilderFactory.newDocumentBuilder(); //xml文件放到assets目录下 document=documentBuilder.parse(this.openFileInput("users.xml")); org.w3c.dom.Element root= document.getDocumentElement(); NodeList nodeList=root.getElementsByTagName(tag); Node nd=nodeList.item(0); re= nd.getFirstChild().getNodeValue(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ document=null; documentBuilder=null; documentBuilderFactory=null; } return re; }
以上转载于 http://blog.csdn.net/hanjingjingpan/article/details/8812770
顺便介绍三种xml解析
public class MainActivity extends Activity implements OnClickListener { private static final String TAG = "MainActivity"; Button Sax_praseXml, Dom_praseXml, Pull_praseXml, Write_xml; TextView Sax_contentXml, Dom_contentXml, Pull_contentXml; String xmlContent; MyHandler myhandler; List<PersonDetail> list; InputStream inputStream; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); xmlContent = ""; Sax_praseXml = (Button) findViewById(R.id.btn_sax); Dom_praseXml = (Button) findViewById(R.id.btn_dom); Pull_praseXml = (Button) findViewById(R.id.btn_pull); Sax_contentXml = (TextView) findViewById(R.id.view_sax); Dom_contentXml = (TextView) findViewById(R.id.prase_dom); Pull_contentXml = (TextView) findViewById(R.id.prase_pull); Sax_praseXml.setOnClickListener(this); Dom_praseXml.setOnClickListener(this); Pull_praseXml.setOnClickListener(this); } public void onClick(View v) { switch (v.getId()) { case R.id.btn_sax: inputStream = MainActivity.class.getClassLoader() .getResourceAsStream("test.xml"); try { Log.v(TAG, "-------sax"); StringBuffer sb = new StringBuffer(); list = SAXpraseTransiaction.SaxPrase(inputStream); for (PersonDetail tail : list) { sb.append(tail.toString() + "\n"); } Sax_contentXml.setText("SaxResult:" + sb.toString()); Log.v(TAG, "" + "----------------------" + sb.toString()); } catch (Exception E) { Log.v(TAG, "" + "----------------------解析失败"); } break; case R.id.btn_dom: inputStream = MainActivity.class.getClassLoader() .getResourceAsStream("test.xml"); try { Log.v(TAG, "-------DOM"); StringBuffer sbDOM = new StringBuffer(); list = DomPraserTransaction.getPraserList(inputStream); for (PersonDetail tail : list) { sbDOM.append(tail.toString() + "\n"); } Dom_contentXml.setText("DOMResult:" + sbDOM.toString()); Log.v(TAG, "" + "----------------------" + sbDOM.toString()); } catch (Exception E) { Log.v(TAG, "" + "----------------------解析失败"); } break; case R.id.btn_pull: inputStream = MainActivity.class.getClassLoader() .getResourceAsStream("test.xml"); try { Log.v(TAG, "-------PULL"); StringBuffer sbPULL = new StringBuffer(); list = PullPraserTransaction.GetResultList(inputStream); for (PersonDetail tail : list) { sbPULL.append(tail.toString() + "\n"); } Pull_contentXml.setText("PULLResult:" + sbPULL.toString()); Log.v(TAG, "" + "----------------------" + sbPULL.toString()); } catch (Exception E) { Log.v(TAG, "" + "----------------------解析失败"); } break; } } /** * 实用于文件比较小的Xml解析,简单实用 2011/9/29 * * @author:1314hwl * */ public class DomPraserTransaction { public static List<PersonDetail> getPraserList(InputStream inputStream) { List<PersonDetail> Resultlist = null; PersonDetail Person = null; XmlPullParser pullpraser = Xml.newPullParser(); try { pullpraser.setInput(inputStream, "UTF-8"); int eventtype = pullpraser.getEventType(); while (eventtype != XmlPullParser.END_DOCUMENT) { switch (eventtype) { case XmlPullParser.START_DOCUMENT: Resultlist = new ArrayList<PersonDetail>(); break; case XmlPullParser.START_TAG: if ("person".equals(pullpraser.getName())) { Person = new PersonDetail(); Person.setId(new Integer(pullpraser .getAttributeValue(0))); } else if (Person != null) { if ("name".equals(pullpraser.getName())) { Person.setName(pullpraser.nextText()); } else if ("age".equals(pullpraser.getName())) { Person.setAge(new String(pullpraser.nextText())); } } break; case XmlPullParser.END_TAG: if ("person".equals(pullpraser.getName()) && Person != null) { Resultlist.add(Person); Person = null; } break; } eventtype = pullpraser.next(); } } catch (Exception e) { e.printStackTrace(); } try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } return Resultlist; } } /** * pull解析XML pull写入XML * * @author 1314hwl 2011/9/29 * */ public class PullPraserTransaction { public static List<PersonDetail> GetResultList(InputStream inputStream) throws Exception { List<PersonDetail> Resultlist = new ArrayList<PersonDetail>(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(inputStream); Element element = document.getDocumentElement(); NodeList nodes = element.getElementsByTagName("person"); int len = nodes.getLength(); for (int i = 0; i < len; i++) { Element personElement = (Element) nodes.item(i); PersonDetail person = new PersonDetail(); person.setId(new Integer(personElement.getAttribute("id"))); NodeList childNodes = personElement.getChildNodes(); for (int y = 0; y < childNodes.getLength(); y++) { Node childNode = (Node) childNodes.item(y); if (childNode.getNodeType() == Node.ELEMENT_NODE) { Element childElement = (Element) childNode; if ("name".equals(childElement.getNodeName())) { person.setName(childElement.getFirstChild() .getNodeValue()); } if ("age".equals(childElement.getNodeName())) { person.setAge(childElement.getFirstChild() .getNodeValue()); } } } Resultlist.add(person); } inputStream.close(); return Resultlist; } } /** * @author 1314hwl SAX解析 2011/9/29 */ public class SAXpraseTransiaction { public static List<PersonDetail> SaxPrase(InputStream inputStream) { SAXParserFactory factory = SAXParserFactory.newInstance();//解析工厂 // try { // SAXParser saxparser=factory.newSAXParser(); //解析器对象 // // MyHandler myhandler=new MyHandler(); // // saxparser.parse(inputStream, myhandler); // // inputStream.close(); // } catch (Exception e) { // // } 这里两种方式:SAXParser or XMLReader 都可以 MyHandler myhandler = new MyHandler(); try { XMLReader xmlreader = factory.newSAXParser().getXMLReader();//实现一个XMLReader xmlreader.setContentHandler(myhandler); //defualthandler xmlreader.parse(new InputSource(inputStream));//要解析的流加入解析中 } catch (Exception e) { e.printStackTrace(); } try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } return myhandler.resultList(); } } ** * SAX解析的defaultHandler * * @author 1314hwl 2011/9/29 */ public class MyHandler extends DefaultHandler { private static final String TAG = "MyHandler"; String name, age; String tagname; PersonDetail Person; List<PersonDetail> personList = null; public MyHandler() { super(); personList = new ArrayList<PersonDetail>(); } public void characters(char[] ch, int start, int length) throws SAXException { super.characters(ch, start, length); if (Person != null) { String content = new String(ch, start, length); if ("name".equals(tagname)) { Person.setName(content); } else if ("age".equals(tagname)) { Person.setAge(content); } } } public void endDocument() throws SAXException { super.endDocument(); Log.v(TAG, "解析结束"); } public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); if (localName.equals("person") && Person != null) { personList.add(Person); Person = null; } tagname=null; } public void startDocument() throws SAXException { super.startDocument(); Log.v(TAG, "解析开始"); } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); Log.v("startElement", localName); tagname = localName; if (localName.equals("person")) { Person = new PersonDetail(); Person.setId(new Integer(attributes.getValue(0))); } } public List<PersonDetail> resultList() { return personList; } } public class PersonDetail { String name, age; int id; public int getId() { return id; } public void setId(int id) { this.id = id; } public PersonDetail(int id, String name, String age) { super(); this.id = id; this.name = name; this.age = age; } public PersonDetail() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String toString() { return "person [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
以及 详解Java反射机制实例
import java.lang.reflect.*; public class ReflectTester { public Object copy(Object object) throws Exception { // 获得对象的类型 Class classType = object.getClass(); System.out.println("Class:" + classType.getName()); // 通过默认构造方法创建一个新的对象 Object objectCopy = classType.getConstructor(new Class[] {}) .newInstance(new Object[] {}); // 获得对象的所有属性 Field fields[] = classType.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; String fieldName = field.getName(); String firstLetter = fieldName.substring(0, 1).toUpperCase(); // 获得和属性对应的getXXX()方法的名字 String getMethodName = "get" + firstLetter + fieldName.substring(1); // 获得和属性对应的setXXX()方法的名字 String setMethodName = "set" + firstLetter + fieldName.substring(1); // 获得和属性对应的getXXX()方法 Method getMethod = classType.getMethod(getMethodName, new Class[] {}); // 获得和属性对应的setXXX()方法 Method setMethod = classType.getMethod(setMethodName, new Class[] { field.getType() }); // 调用原对象的getXXX()方法 Object value = getMethod.invoke(object, new Object[] {}); System.out.println(fieldName + ":" + value); // 调用复制对象的setXXX()方法 setMethod.invoke(objectCopy, new Object[] { value }); } return objectCopy; } public static void main(String[] args) throws Exception { Customer customer = new Customer("Tom", 21); customer.setId(new Long(1)); Customer customerCopy = (Customer) new ReflectTester().copy(customer); System.out.println("Copy information:" + customerCopy.getName() + " " + customerCopy.getAge()); } } class Customer { // Customer类是一个JavaBean private Long id; private String name; private int age; public Customer() { } public Customer(String name, int age) { this.name = name; this.age = age; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
阅读完以上的内容
我们的第一个方法 WriteToString() 就可以通过反射直接写出,不需要每次都写好多属性
举例说明:Product.Java
private String name; private String price; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; }
Test.java
public static void main(String[] args) throws Exception {
List<Product> plist = new ArrayList<Product>();
Product p1= new Product();
p1.setName("空气净化器1");
p1.setPrice("3799");
plist.add(p1);
Product p2= new Product();
p2.setName("空气净化器2");
p2.setPrice("3799");
plist.add(p2);
produceXml(Product.class , plist);
}
public static String produceXml(Class clazz,List plist) throws Exception{
System.out.println("<" +clazz.getName().toLowerCase()+"s" +">");
for(Object o:plist){
System.out.println( "<" +clazz.getName().toLowerCase()+">");
for(int i=0;i< clazz.getDeclaredFields().length;i++){
System.out.print("<" + clazz.getDeclaredFields()[i].getName() +">");
for(int j = 0; j < Product.class.getDeclaredMethods().length; j++) {
if ( ("get"+clazz.getDeclaredFields()[i].getName().substring(0,1).toUpperCase()+( clazz.getDeclaredFields()[i].getName().substring(1)) ).equals( clazz.getDeclaredMethods()[j].getName() )){
Method getMethod = clazz.getMethod(clazz.getDeclaredMethods()[j].getName(), new Class[]{});
Object result = getMethod.invoke( o, new Object[] {});
System.out.print( result.toString());
}
}
System.out.println("</"+ clazz.getDeclaredFields()[i].getName() +">");
}
System.out.println( "</"+clazz.getName().toLowerCase()+">");
}
System.out.println( "</"+clazz.getName().toLowerCase()+"s"+">");
return null;
}
运行结果:
<products>
<product>
<name>空气净化器1</name>
<price>3799</price>
</product>
<product>
<name>空气净化器2</name>
<price>3799</price>
</product>
</products>
System.out.println( "</"+ ……+">"); 替换成xmlSerializer.endTag(null, ……);就ok了哦!
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!