问题描述
importjava.io.*;importjava.util.*;importjava.util.Vector;importjavax.xml.parsers.*;importorg.w3c.dom.*;importorg.apache.crimson.tree.XmlDocument;classStudentBean{Stringsex;Stringname;intage;Stringphone;Stringnumber;publicvoidSetsex(Strings){sex=s;}publicvoidSetname(Stringn){name=n;}publicvoidSetphone(Stringp){phone=p;}publicvoidSetage(inta){age=a;}publicvoidSetnumber(Stringn){number=n;}publicStringGetsex(){returnsex;}publicStringGetphone(){returnphone;}publicintGetage(){returnage;}publicStringGetname(){returnname;}publicStringGetnumber(){returnnumber;}}classXMLTest{privatevoidreadXMLFile(StringinFile)throwsException{DocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();DocumentBuilderdb=null;try{db=dbf.newDocumentBuilder();}catch(ParserConfigurationExceptionpce){System.err.println(pce);System.exit(1);}Documentdoc;try{doc=db.parse(inFile);}catch(DOMExceptiondom){System.err.println(dom.getMessage());System.exit(1);}catch(IOExceptionioe){System.err.println(ioe);System.exit(1);}Elementroot=doc.getDocumentElement();//取学生列表元素NodeListstudents=root.getElementsByTagName("学生");for(inti=0;i<students.getLength();i++){Elementstudent=(Element)students.item(i);StudentBeanstudentbean=newStudentBean();studentbean.Setsex(student.getAttribute("性别"));NodeListnames=student.getElementsByTagName("姓名");if(names.getLength()==1){Elemente=(Element)names.item(0);Textt=(Text)e.getFirstChild();studentbean.Setname(t.getNodeValue());}NodeListages=student.getElementsByTagName("年龄");if(ages.getLength()==1){Elemente=(Element)ages.item(0);Textt=(Text)e.getFirstChild();studentbean.Setage(Integer.parseInt(t.getNodeValue()));}NodeListphones=student.getElementsByTagName("电话");if(phones.getLength()==1){Elemente=(Element)phones.item(0);Textt=(Text)e.getFristChild();(cannotfindsymbolsymbol:methodgetFristChild()location:interfaceorg.w3c.dom.ElementTextt=(Text)e.getFristChild();)studentbean.Setphone(t.getNodeValue());}NodeListnumbers=student.getElementsByTagName("学号");if(numbers.getLength()==1){Elemente=(Element)numbers.item(0);Textt=(Text)e.getFristChild();(cannotfindsymbolsymbol:methodgetFristChild()location:interfaceorg.w3c.dom.ElementTextt=(Text)e.getFristChild();)studentbean.Setnumber(t.getNodeValue());}student_Vector.add(studentBean);(cannotfindsymbolsymbol:variablestudentBeanlocation:classXMLTeststudent_Vector.add(studentBean);)}}privatevoidwriteXMLFile(StringoutFile)throwsException{DocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();DocumentBuilderdb=null;try{db=dbf.newDocumentBuilder();}catch(ParserConfigurationExceptionpce){System.err.println(pce);System.exit(1);}Documentdoc=null;doc=db.newDocument();Elementroot=doc.createElement("学生花名册");doc.appendChild(root);for(inti=0;student_Vector.size();i++)(cannotfindsymbolsymbol:variablestudent_Vectorlocation:classXMLTestfor(inti=0;student_Vector.size();i++)){StudentBeanstudentBean=(StudentBean)student_Vector.get(i);[/b](Project.java:138:cannotfindsymbolsymbol:variablestudent_Vectorlocation:classXMLTestStudentBeanstudentBean=(StudentBean)student_Vector.get(i);)[b]Elementstudent=doc.createElement("学生");student.setAttribute("性别",studentBean.Getsex());root.appendChild(student);Elementname=doc.createElement("姓名");student.appendChild(name);Texttname=doc.createTextNode(studentBean.Getname());name.appendChild(tname);Elementage=doc.createElement("年龄");student.appendChild(age);Texttage=doc.createTextNode(studentBean.Getage());(createTextNode(java.lang.String)inorg.w3c.dom.Documentcannotbeappliedto(int)Texttage=doc.createTextNode(studentBean.Getage());^)age.appendChild(tage);Elementphone=doc.createElement("电话");student.appendChild(phone);Texttphone=doc.createTextNode(studentBean.Getphone());phone.appendChild(tphone);Elementnumber=doc.createElement("学号");student.appendChild(number);Texttnumber=doc.createTextNode(studentBean.Getnumber());phone.appendChild(tnumber);}FileOutputStreamoutStream=newFileOutputStream(outFile);OutputStreamWriteroutWriter=newOutputStreamWriter(outStream);((XmlDocument)doc).write(outWriter,"GB2312");outWriter.close();outStream.close();}publicstaticint[]random_serial(intlimit,intneed){int[]temp=newint[limit];int[]result=newint[need];for(inti=0;i<limit;i++)temp[i]=i;//将1到limit传到temp数组。intw;Randomrand=newRandom();for(inti=0;i<need;i++){w=rand.nextInt(limit-i)+i;intt=temp[i];temp[i]=temp[w];result[i]=temp[i];temp[i]=t;}returnresult;}publicstaticvoidmain(String[]args)throwsException{XMLTestxmltest=newXMLTest();xmltest.student_Vector=newVector();(cannotfindsymbolsymbol:variablestudent_Vectorlocation:classXMLTestxmltest.student_Vector=newVector();)inta[]=newint[2];System.out.println("开始读入数据");xmltest.readXMLFile("新建文本文档.xml");System.out.println("读入数据完毕,现在开始写入数据");xmltest.writeXMLFile("output.xml");System.out.println("写入完成");}}