My iPhone 4 is a lemon, oh my! (updated)
我是要把一个JAVABEAN写入一个XML中,然后再度出,我是利用了一个vector,先把所有要读入的同一个StudentBean都添加到一个vector中我的StudentBean如下: import java.io.Serializable; public class StudentBean implements Serializable { String name; String sex; String phone; public StudentBean(String name, String sex, String phone) { super(); this.name = name; this.sex = sex; this.phone = phone; } public StudentBean() {}; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } } 写操作是如下: import org.w3c.dom.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.*; import java.util.Vector; public class writexml { private Document document; private String filename; public writexml(String name) throws ParserConfigurationException{ filename=name; DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); document=builder.newDocument(); } public void toWrite(Vector stu_vector) { Element root = document.createElement("seller_record"); document.appendChild(root); for(int i = 0;i stu_vector = new Vector(); StudentBean stu = new StudentBean("姚明","男","123456"); stu_vector.add(stu); stu = new StudentBean("刘翔","女","0988556"); stu_vector.add(stu); try{ writexml myxml=new writexml("E:\\软件\\eclipse\\workstation\\IOxml\\src\\seller.xml"); myxml.toWrite(stu_vector); myxml.toSave(); System.out.print("Your writing is successful."); } catch(ParserConfigurationException exp){ exp.printStackTrace(); System.out.print("Your writing is failed."); } } } 下面是读操作: import java.io.*; import java.util.Vector; import javax.xml.parsers.*; import org.w3c.dom.*; public class readxml { static Document document; private boolean validating; public readxml() { } public Vector toRead(String filename) { Vector stu_vector=new Vector(); StudentBean stu = new StudentBean(); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringElementContentWhitespace(true); factory.setValidating(validating); DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(new File(filename)); // document.getDocumentElement().normalize();//注释(0) Element root = document.getDocumentElement(); NamedNodeMap attr; NodeList children = root.getElementsByTagName("student");//注释(1) attr = root.getAttributes(); if(attr!=null) { for(int i = 0;i A = null; readxml my = new readxml(); A = my.toRead("E:\\软件\\eclipse\\workstation\\IOxml\\src\\seller.xml"); System.out.println(A.size()); System.out.println("asd"); } } 在下面是我读入的XML格式如下: − 男 123456 − 女 0988556 我写入的XML文档根有两个孩子,两个孩子又有3个node; 下面简要谈一下我对读操作的理解(主要是我标的注释的地方): 注释(1):以tag为student为标志建立NodeList,其长度为2,这个是符合逻辑的,但是下面的children的长度老提示是5,开始时令我很郁闷,后来查书和上网看了一下,才知道原来 男他们两个之间是有空格的,他们之间的空格也算一个Node,所以才形成了注释(2)(3)的item标号为1,3。书上说有办法吧他们之间的空格去掉,其给的一个办法是用以下方法: for(int i=0;i