小弟最近在写一个地址本
初用XPATH,JDOM
在添加删除上出了点问题
望指教 谢谢 public void addContact(Contact c) throws JDOMException, IOException {
System.out.println ("#########begin addcontact ########");
//Get root element of address book
Element root = doc.getRootElement ();
Element contact = new Element("contact");
//Get ID of address book
//System.out.println ("id: " + c.getID());
if(c.getID() != null && c.getID() != "") {
int id = Integer.parseInt (c.getID ());
if(id > 0) { Element file = (Element)XPath.selectSingleNode (doc.getRootElement (),"//file[attribute::id = '"+ id +"']"); root.removeContent (existing);
contact.setAttribute ("id",String.valueOf (id)); } else { String nextid = root.getAttribute ("nextid").getValue ();
id = Integer.parseInt (nextid); contact.setAttribute ("id",String.valueOf (id));
nextid = String.valueOf (++id); root.setAttribute ("nextid",nextid);
}
}
contact.addContent (new Element("Address")).addContent (c.getAddress ());
contact.addContent (new Element("EMail")).addContent (c.getEMail ());
contact.addContent (new Element("Forename")).addContent (c.getForename ());
contact.addContent (new Element("ID")).addContent (c.getID ());
contact.addContent (new Element("Landline")).addContent (c.getLandline ());
contact.addContent (new Element("Mobile")).addContent (c.getMobile ());
contact.addContent (new Element("PhotoURL")).addContent (c.getPhotoURL ());
contact.addContent (new Element("Surname")).addContent (c.getSurname ());
root.addContent (contact);
XMLOutputter xmlo = new XMLOutputter();
xmlo.output (doc, new FileOutputStream(file));
System.out.println ("#####################end addcontact ######################");
}
public void deleteContact(Contact c) {
int id = Integer.parseInt (c.getID ());
try {
Element file = (Element)XPath.selectSingleNode (doc.getRootElement (),
"//file[attribute::id = '"+ id +"']");
doc.getRootElement ().removeContent (file);
} catch (JDOMException jde) {
jde.printStackTrace ();
}
}
初用XPATH,JDOM
在添加删除上出了点问题
望指教 谢谢 public void addContact(Contact c) throws JDOMException, IOException {
System.out.println ("#########begin addcontact ########");
//Get root element of address book
Element root = doc.getRootElement ();
Element contact = new Element("contact");
//Get ID of address book
//System.out.println ("id: " + c.getID());
if(c.getID() != null && c.getID() != "") {
int id = Integer.parseInt (c.getID ());
if(id > 0) { Element file = (Element)XPath.selectSingleNode (doc.getRootElement (),"//file[attribute::id = '"+ id +"']"); root.removeContent (existing);
contact.setAttribute ("id",String.valueOf (id)); } else { String nextid = root.getAttribute ("nextid").getValue ();
id = Integer.parseInt (nextid); contact.setAttribute ("id",String.valueOf (id));
nextid = String.valueOf (++id); root.setAttribute ("nextid",nextid);
}
}
contact.addContent (new Element("Address")).addContent (c.getAddress ());
contact.addContent (new Element("EMail")).addContent (c.getEMail ());
contact.addContent (new Element("Forename")).addContent (c.getForename ());
contact.addContent (new Element("ID")).addContent (c.getID ());
contact.addContent (new Element("Landline")).addContent (c.getLandline ());
contact.addContent (new Element("Mobile")).addContent (c.getMobile ());
contact.addContent (new Element("PhotoURL")).addContent (c.getPhotoURL ());
contact.addContent (new Element("Surname")).addContent (c.getSurname ());
root.addContent (contact);
XMLOutputter xmlo = new XMLOutputter();
xmlo.output (doc, new FileOutputStream(file));
System.out.println ("#####################end addcontact ######################");
}
public void deleteContact(Contact c) {
int id = Integer.parseInt (c.getID ());
try {
Element file = (Element)XPath.selectSingleNode (doc.getRootElement (),
"//file[attribute::id = '"+ id +"']");
doc.getRootElement ().removeContent (file);
} catch (JDOMException jde) {
jde.printStackTrace ();
}
}
*
* @author Woo
*/
public class Contact
{
private String Address;
private String EMail;
private String Forename;
private String ID;
private String Landline;
private String Mobile;
private String PhotoURL;
private String Surname;
/** Creates a new instance of Contact */
public Contact ()
{
}
public Contact(String address,String email,String forename,String landline,String mobile,String PhotoURL,String surname) {
Address = Address;
EMail = EMail;
Forename = Forename;
ID = ID;
Landline = Landline;
Mobile = Mobile;
PhotoURL = PhotoURL;
Surname = Surname;
}
public void setAddress(String Address) {
this.Address = Address;
}
public void setEMail(String EMail) {
this.EMail = EMail;
}
public void setForename(String Forename) {
this.Forename = Forename;
}
public void setID(String ID) {
this.ID = ID;
}
public void setLandline(String Landline) {
this.Landline = Landline;
}
public void setMobile(String Mobile) {
this.Mobile = Mobile;
}
public void setPhotoURL(String PhotoURL) {
this.PhotoURL = PhotoURL;
}
public void setSurname(String Surname) {
this.Surname = Surname;
} public String getAddress() {
return Address;
}
public String getEMail() {
return EMail;
}
public String getForename() {
return Forename;
}
public String getID() {
return ID;
}
public String getLandline() {
return Landline;
}
public String getMobile() {
return Mobile;
}
public String getPhotoURL() {
return PhotoURL;
}
public String getSurname() {
return Surname;
}
public String toString() {
return Forename + " " + Surname;
}
}