ASP
  Home arrow ASP arrow Page 3 - The Not So Ordinary Address Book
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP

The Not So Ordinary Address Book
By: Nick Gerakines
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 40
    2003-06-03

    Table of Contents:
  • The Not So Ordinary Address Book
  • Page 1
  • Page 2
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    The Not So Ordinary Address Book - Page 2


    (Page 3 of 4 )

    Wham, Bam, Thank you Ma'am. We've created a couple functions on adding new information to the address book entry. So what if we want to edit some data that is already there? There are two ways to go about this. You can either delete it and start off fresh, but who really wants to do that anyway? Or, and what i suggest, using those nice little id attributes we set to find the node we want to edit, and just edit the data directly. 

    The function starts off normally, load the object with the data from the XML file, using the error control of course. Then, we can either search for the entry node and then the contact node, or we can go to the contact node directly. Using XPath we can hunt down the node directly using the following string: 

    */contacts/contact[@id=XYZ123]

    where XYZ123 is the unique identifier that was set. After a bit of error control to make sure we have only 1 result we can then edit the data directly.

    An example would be:

    <coffeeprices>
    <small>1.39</small>
    </coffeeprices>
    <%
    smallcup_node.text = "0.99"
    %>
    <coffeeprices>
    <small>o.99</small>
    </coffeeprices>


    Its a bit crude in comparison to how it really works but it shows the simplicity of changing an elements text. Once the text is changed to how you want it, all you have to do is save the file and you are done.

    Note: Below is an example function of editing a contact element's data. Inside is the code to only change a phone (or misc) element type. The address bit is not included.

    <%
    function EditContact()
    Dim root, xmlFE
    dim Entry_node, EntryProfile_node, tmpnode
    ' Load the XML file that houses all this info, called abook.xml
    xmlFE = objxml.load(server.mappath("abook.xml"))
    ' If there was a problem loading the file, like it not being there either exit
    ' the function. Since this function relies on entries already present we just abort
    ' it altogether.
    if xmlFE = false then 
    response.write "Address book file not found. Aborting.<br>"
    exit function
    end if
    set root = objxml.documentelement
    ' This is where we actually find the node that we want to add the information to.
    ' It does a simple search for all nodes with an attribute matching the id
    ' sent from the form in a hiddden field.
    set Entry_node = root.selectnodes("entry[@id=""" & request.forms("id") & """]")
    set EntryContact_node = root.selectnodes("*/contacts/contact[@id=""" & request.forms("id") & """]")
    if EntryContact_node.length > 1 then
    response.write "Error: More than 1 entry found with that id. Aborting<br>"
    Exit Function
    end if
    if EntryContact_node.length < 1 then
    response.write "Error: No entries found with that id. Aborting.<br>"
    Exit Function
    end if
    ' Ok, everything so far is ok, set the entrycontact_node to the first node
    ' , and only node, found in the nodelist
    set EntryContact_node = EntryContact_node.item(0)
    ' So if the Contact type was phone then we simply set the text as requested field data.
    ' Then we set tmpnode as the name attribute and set its text to the requested field name.
    ' Its not that hard. 
    if request.form("ContactType") = "phone" then
    EntryContact_node.text = request.form("data")
    set tmpnode = EntryContact_node.getNamedItem("name")
    tmpnode.text = request.form("name")
    end if
    end function
    %>


    The same would go for editing a profile item element. Its just a matter of finding it using XPath and then changing the objects text. You can see how simple it would be to implement.

    The only thing left on the list is node deletion. Lets say you have an entry that you just want to get rid of completely. Instead of opening the XML and editing it by hand we can do that through XML DOM as well. Below is a function to give an example of how it can be done. First the file is loaded into the XML object and we use the error control to find it. If its not there then you don't have to worry about deleting the entry in the first place. 

    Then we find the entry node by the unique id for it using XPath. Once we have determined that only 1 instance results from the search its practically done. We put in <searchnode>.removeall and all found nodes, hopefully the only one you were looking for, are now deleted from their respective parents. Then we save the file and its all done.

    <%
    Function DeleteEntry
    Dim root, xmlFE
    dim Entry_node, EntryProfile_node, tmpnode
    ' Load the xml file that houses all this info, called abook.xml
    xmlFE = objxml.load(server.mappath("abook.xml"))
    ' If there was a problem loading the file, like it not being there either exit
    ' the function. Since this function relies on entries already present we just abort
    ' it altogether.
    if xmlFE = false then 
    response.write "Address book file not found. Aborting.<br>"
    exit function
    end if
    set root = objxml.documentelement
    ' This is where we actually find the node that we want to add the information to.
    ' It does a simple search for all nodes with an attribute matching the id
    ' sent from the form in a hiddden field.
    set Entry_node = root.selectnodes("entry[@id=""" & request.forms("id") & """]")
    ' This is basic error control
    ' If more than one nodes are found then there is a problem that should
    ' be taken care of before more nodes are added. 
    ' You can remove it and then all information will be put in the first node
    ' found in the node list
    if Entry_node.length > 1 then
    response.write "Error: More than 1 entry found with that id. Aborting<br>"
    Exit Function
    end if
    ' If less than 1 entries were found then the entry probly doesn't exist
    ' or something equally wrong. Make sure the form is sending the id to
    ' look for, and its not blank.
    if Entry_node.length < 1 then
    response.write "Error: No entries found with that id. Aborting.<br>"
    exit function
    end if
    ' Now all we do is remove all children from the select nodes node list and its gone.
    ' Simple huh?
    Entry_Node.removeall
    ' Final thing to do is to save the changes
    objxml.save(server.mappath("abook.xml"))
    end function
    %>


    Below is a utility function i use to simplify node creation. It takes the XML DOM object, the parent of the node you want to create, the node name and then its text as input and then creates the node.

    <%
    '
    ' This function simply creates a node under a node and adds
    ' text.
    '
    function create_Node(xmldoc, childroot, nodename, nodetext)
    if vartype(nodetext) = 1 then nodetext = ""
    dim newElem
    Set newElem = xmldoc.createElement(nodename)
    childroot.appendChild newElem
    childroot.lastChild.text = nodetext
    End function
    %>


    Below is an example address book entry.

    <data>
    <entry id="NGS12345678">
    <firstname>Nicholas</firstname>
    <lastname>Gerakines</lastname>
    <nickname>Sock</nickname>
    <company>Starbucks</company>
    <contact>
    <contact type="misc" name="Home Email Address" id="123456">Sock@Socklabs.com</contact>
    <contact type="address" name="Work Address" id="123457">
    <street></street>
    <city>Victorville</city>
    <state>Ca</state>
    <zip>92321</zip>
    </contact>
    <contact type="misc" name="Web Site Address" id="123458">http://www.Socklabs.com</contact>
    </contact>
    <profile>
    <item type="date" name="My Birthday" id="456123">10/22/1983</item>
    </profile>
    </entry>
    </data>

    More ASP Articles
    More By Nick Gerakines


     

    ASP ARTICLES

    - Central Scoreboard with Flash and ASP
    - Calorie Counter Using WAP and ASP
    - Creating PGP-Encrypted E-Mails Using ASP
    - Be My Guest in ASP
    - Session Replacement in ASP
    - Securing ASP Data Access Credentials Using t...
    - The Not So Ordinary Address Book
    - Adding and Displaying Data Easily via ASP an...
    - Sending Email From a Form in ASP
    - Adding Member Services in ASP
    - Removing Unconfirmed Members
    - Trapping HTTP 500.100 - Internal Server Error
    - So Many Rows, So Little Time! - Case Study
    - XDO: An XML Engine Class for Classic ASP
    - Credit Card Fraud Prevention Using ASP and C...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT