/*** XML read/write example * @ author HX **/public class xmlreaderwriter {/*** read an XML file, returns an employee linked list * @ Param filename * @ return */public list <employee> readxml (string filename) {list <employee> employees = new arraylist <employee> (); saxreader reader = new saxreader (); try {document = reader. read (new file (filename); element root = document. getrootelement (); List nodes = root. elements ("employee"); For (iterator it = nodes. iterator (); it. hasnext ();) {employee Employee = new employee (); // The employee subnode element employeeelm = (element) it under the root node. next (); // obtain the ID attribute of the employee subnode. Attribute attribute = employeeelm. attribute ("ID"); employee. setid (Long. parselong (attribute. gettext (); // restores the long string in the text form to the basic type. // obtains the name of the employee subnode. The subnode element nameelm = employeeelm. element ("name"); employee. setname (nameelm. gettext (); // get the name of the subnode text // get the employee's subnode Gender of the subnode text employee. setmale (Boolean. parseboolean (employeeelm. element ("ismale "). gettext (); // Add the employee information to the linked list employees. add (employee) ;}} catch (employee entexception e) {// todo auto-generated Catch Block E. printstacktrace ();} return employees;}/*** write an employee linked list to the XML document * @ Param employees * @ Param filename */Public void writertofile (list <employee> employees, string filename) {// create the Document Object document = incluenthelper. createdocument (); // create the root node element root = document. addelement ("employees"); For (employee EMP: Employees) {// create employee node element empelm = root. addelement ("employee"); // create the ID attribute empelm of the employee node. addattribute ("ID", String. valueof (EMP. GETID (); // create the sub-node name element nameelm = empelm of the employee node. addelement ("name"); nameelm. settext (EMP. getname (); // set the text of the subnode // create the subnode ismale element ismaleelm = empelm. addelement ("ismale"); ismaleelm. settext (string. valueof (EMP. ismale (); // set the sub-node text} // write the Document Object to the file xmlwriter writer; try {writer = new xmlwriter (New filewriter (filename )); writer. write (document); writer. close ();} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace () ;}} public static void main (string [] ARGs) {list <employee> employees = new arraylist <employee> ();/* employees. add (new employee (1, "Andy", true); employees. add (new employee (2, "Bill", false); employees. add (new employee (3, "Cindy", true); employees. add (new employee (4, "doutglas", false); xmlreaderwriter = new xmlreaderwriter (); xmlreaderwriter. writertofile (employees, "employees20140724.xml"); */xmlreaderwriter = new xmlreaderwriter (); List <employee> Employee = xmlreaderwriter. readxml ("employees20140724.xml"); Employee = NULL ;}}
public class Employee{ private long id; private String name; private boolean isMale; public Employee(){ } public Employee(long id,String name,boolean isMale){ this.id=id; this.name=name; this.isMale=isMale; } public long getId() { return id; } public void setId(long id) { this.id = id; } public boolean isMale() { return isMale; } public void setMale(boolean isMale) { this.isMale = isMale; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
This aims to give students an idea of how to import third-party packages.