1. This is the XML file we want, with all the information of user Member. . Of course, the format of MEMBER is also available through Members.dtd.
XML Version = "1.0" encoding = "UTF-8"?>
2. Recommended a corresponding Member JavaBean:
/ * * Created on 2004-7-10 * * Todo to change the template for this generated file go to * window - preferences - java - code style - code templates * /
/ ** * @author Huang Jinxiang * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates * / public class Member {private String username; private String password; private String Private string birth; private string email; private string pender; private string zipcode;
public String getAddress () {return address;} public void setAddress (String address) {this.address = address;} public String getBirth () {return birth;} public void setBirth (String birth) {this.birth = birth;} public String getContact () {return contact;} public void setContact (String contact) {this.contact = contact;} public String getEmail () {return email;} public void setEmail (String email) {this.email = email;} public String getGender () {return gender;} public void setGender (String gender) {this.gender = gender;} public String getPassword () {return password;} public void setPassword (String password) {this.password = password;} public String getRealname () {return realname;} public void setRealname (String realname) {this.realname = realname;} public String getUsername () {return username;} public void setUsername (String username) {this.username = username;} Public string getzipcode () {return zipcode;} public void setzipcode e) {this.zipcode = zipcode;}} Because the XML is in the String format, I use String to reply to String, and the user can pass the integer.parseint (). Helper to change its type. . 3. Eliance with DOM:
/ * * Created on 2004-7-10 * * Todo to change the template for this generated file go to * window - preferences - java - code style - code templates * /
Import java.io.file;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml. Imform.dom.domsource; import javax.xml.transform.Stream.streamResult;
Import org.w3c.dom.document; import org.w3c.dom.element; import org.w3c.dom.node; / ** * @Author Huang jinxiang * * Todo to change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates * / Public Class MemberImp {
// This method is to edit the Member's information, then save / first to find the corresponding Member information, extract it to put the corresponding member bean //, then remove this user's information from the XML file , // then change the extraction of the attribute value of MemberBean // last written to Members.xml //// where string string is the path to Members.xml (both MEMBERS.XML position) public void UpdateMember (MEMBER) member, String str) {try {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder builder = factory.newDocumentBuilder (); Document document = builder.parse (str); Element root = document.getDocumentElement (); int i; for ( i = 0; i DOMSource source = new DOMSource (document); StreamResult result = new StreamResult (str);. String systemValue = (. New File (document.getDoctype () getSystemId ())) getName (); transformer.setOutputProperty (OutputKeys.DOCTYPE_SYSTEM, systemValue ); Transformer.Transform (Source, Result); Writemember (MEMBER, STR);}}} catch (exception e) {E.PrintStackTrace ();}} // This method is used to obtain all the member relative to the username information public Member getMember (String username, String str) {Member member = new Member (); Document document; try {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder Builder = factory.newdocumentbuilder (); document = builder.parse (STR); element root = document.getdocumentElement (); int i; for (i = 0; i // This is a new generation of Member Bean wrote in members.xml // Do not forget to also pay attention to the XML DTD in SystemId public void writeMember (Member member, String str) {try {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance () ; factory.setValidating (true); DocumentBuilder builder = factory.newDocumentBuilder (); Document document = builder.parse (str); Element mem = document.createElement ( "member"); mem.setAttribute ( "gender", member.getGender ()); Element username = document.createElement ("username"); username.Appendchild (Member.getuseExTNode)); Mem.Appendchild (username); Element password = document.createElement ( "password"); password.appendChild (document.createTextNode (member.getPassword ())); mem.appendChild (password); Element realname = document.createElement ( "realname"); realname.appendChild (document.createTextNode (member.getRealname ())); mem.appendChild (realname); Element birth = document.createElement ( "birth"); birth.appendChild (document.createTextNode (member.getBirth ())); mem. Appendchild (Birth); Element con = document.createElement ( "contact"); con.appendChild (document.createTextNode (member.getContact ())); mem.appendChild (con); Element email = document.createElement ( "email"); email.appendChild (document.createTextNode (member.getEmail ())); mem.appendChild (email); Element add = document.createElement ( "address"); add.appendChild (document.createTextNode (member.getAddress ())); mem. Appendchild (add); element zipcode = document.createElement ("zipcode"); zipcode.createtextNode (Member.Getzipcode ()); mem.appendchild (zipcode); Element Root = Document.getDocumentelement (); root.appendchild (mem); TRANSFORMERFACTORY TFACTORY = TransformerFactory.newInstance (); Transformer Transformer = tfactory.NewTransformer (); DOMSource source = new DOMSource (document); StreamResult result = new StreamResult (str);. String systemValue = (. New File (document.getDoctype () getSystemId ())) getName (); transformer.setOutputProperty (OutputKeys.DOCTYPE_SYSTEM, systemValue ); Transformer.Transform (Source, Result);} catch (exception e) {E.PrintStackTrace ();}}}