ASP
  Home arrow ASP arrow Page 2 - 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 1


    (Page 2 of 4 )

    The Framework

    Lets start with a framework and how to get it all started. Lets suppose that all the address book entries are
    stored in an XML file called abook.xml and it has a main node called 'data'. So it will look something like this:

    abook.xml

    <data>
    <!-- address book entries go here. -->
    </data>


    For simplicity each sub node, called entry, will be identified by a unique id. So this is what it will look like now:

    <data>
    <entry id="G742djge5">
    </entry>
    </data>


    Inside each entry we can have the basic information like first name, last name, nick name. So it starts to take shape
    like this:

    <data>
    <entry id="G742djge5">
    <firstname/>
    <lastname/>
    <nickname/>
    </entry>
    </data>


    The Contact Element

    So the basics are done. But what about adding a phone number or address. This is done by creating a sub node called 'contact' with the following attributes: type, name, and id. Type is just that, the type. For the contacts its either going to be phone, address, or misc. Phone types are phone numbers, Address types are addresses, and misc are other data types that don't have a name. 

    The id attribute is going to be another unique number/string combo used for finding the node once it is created. That way if we want to edit it we can easily find it again. The name attribute is just then displayed title of the data. This way we can store more than 1 or 2 phone numbers, email addresses, or addresses, which allows for very customizable content.

    <contacts>
    <contact type="phone" name="Home Phone Number" id="12345">(123) 456-7890</contact>
    <contact type="misc" name="Home Email Address" id="12345">Sock@Socklabs.com</contact>
    <contact type="address" name="Home Address" id="12346">
    <street>111 Socklabs drive</street>
    <city>New Orleans</city>
    <state>Louisiana</state>
    <zip></zip
    </contact>
    <contact type="misc" name="Employee Id" id="123457">1234567890</contact>
    </contacts>


    The Profile Element

    The profile Element is like the contact element in some ways. In it are nodes called 'item' with the attributes of type, name and id. The types in this example for these are date and misc. Date would be a birthday. Misc is anything else that isn't put in a category. In this example none of the item nodes have children but it wouldn't hard to give them some, just follow the pattern and give them a new type. Id is of course going to be unique like all the others.

    Below is an example of what it could look like:

    <profile>
    <item type="date" name="Birthday" id="23456">10/22/1983</item>
    <item type="misc" name="Notes" id="23457">
    Nick is a cool guy. He likes to read and hang out with friends.
    </item>
    <item type="misc" name="Signature" id="23458">
    Nick Gerakines
    http://www.socklabs.com
    Sock@Socklabs.com
    </item>
    </profile>


    Inserting the Data

    Now that the framework has been set we can now go about creating the elements. All these functions are assuming that an XML object has been made and it is called 'objxml'. Creating elements is as simple as using the XML DOM. This allows use to go across XML documents and edit them how we like it by manipulating objects. It all starts by having an XML object. I use the following html tag in the top of the file for mine:

    <OBJECT RUNAT="server" PROGID="Microsoft.XMLDOM" id="objxml"></OBJECT>

    Now that the object has been created we load the XML file we want to use, called abook.xml, using <objxml>.load(). For error control, it also returns a true or false based on if the sheet was loaded or not. I have a variable set to read the true or false called xmlfe. This way if it didn't load i can abort the entire operation or create a new sheet using objxml.createElement(ElementName). 

    So now, either by it loading or the need to create a new document altogether, the foundation is set and we can start manipulating the XML document. I would recommend checking out some more detailed articles on XML and all of the properties, methods and calls for it. It really would take a while to go through them all.

    In our little testing function we go about creating a new element using the objxml.CreateElement(ElementName) function. This creates a new node in the document. Now until it is attached it is just kind of hanging around. We attach it to a node, to make it a child node, using <node>.AppendChild(NewNode).

    The code is as follows:

    <states>
    </states>
    <%
    Set NewNode = xmldoc.createElement("California")
    StatesNode.appendChild newElem
    ' We could also do it like this: StatesNode.appendChild(xmldoc.CreateElement("California"))
    %> 
    <states>
    <california></california>
    </states>


    In essence this is what we are doing on a more complex scale. Using the Create_Node function located near the end of the article, we can create a new new, attach it and then set its text in one line. So we create new entity node then all of the sub nodes for it. Technically referred to as children. Last but not least we save the changed data to the file and its all done. Your new address book entry is now there.

    <%
    '
    ' This function creates the new entry element inside the XML file.
    ' The create_node function is at the end of the section
    '
    Function AddEntry
    Dim root, xmlFE
     dim NewEntry_node
      ' 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 or create it
      if xmlfe = false then
       response.write "Address book file not found. Aborting.<br>"
       'exit function
       ' OR create it with
       objxml.appendChild(objxml.createProcessingInstruction("xml","version=""1.0"""))
       objxml.appendChild(objxml.createElement("data"))
      end if
      Set root = objxml.documentElement
       ' Create_Node is a function i use to create a node.
       ' Below we create the initial entry node and then create its children
       create_Node objxml, root, "entry", ""
        response.write root.xml
        Set NewEntry_node = root.lastchild
         NewEntry_node.setAttribute "id", getseed(8) ' This is the unique identifier
         create_Node objxml, NewEntry_node, "firstname", request.form("firstname")
         create_Node objxml, NewEntry_node, "lastname", request.form("lastname")
         create_Node objxml, NewEntry_node, "nickname", request.form("nickname")
         create_Node objxml, NewEntry_node, "contacts", ""
         create_Node objxml, NewEntry_node, "profile", ""
     ' Last but not least we save it to the file.
     objxml.save(server.mappath("abook.xml"))
    End Function
    %>


    So what makes this different from the other address book? The ability to produce dynamic content on the fly. So far we've just put in the basic foundation for the entry lets go a step further and add new contact elements to it and give it its spin. In the function below it shows how to find the address book entry using the unique id that was set and then add new elements under the contacts element. 

    Lets break it down. First and foremost it after the object is created it loads the file. Unlike the function above it is dependant on entries already being there so if the file is not found it is just going to abort completely. If it loads successfully, it then uses <node>.selectnodes to find the address book entry based off of the unique id that it has. This is done using a search string format called XPath (http://www.w3schools.com/xpath/xpath_intro.asp for more info). 

    The search string used looks like this: 

    entry[@id="XYZ123"]

    where XYZ123 is the id in request.forms("id"). So we set Entry_node as an object of the results. With some error control to make sure that there is 1, no less and no more, result it we then set that object, Entry_node, as the first, and hopefully only, result in the search. Once the proper address book entry is found we set another object, EntryContact_node, as the contact element inside it.

    Then it is just a matter of creating the contact element in question. If it is a phone entry or misc entry we just create the child node and set the text. If it is a more complex entry like an address we create the contact node and then the correlating street, city, state and zip elements under it. 

    One important thing to notice is the fact that all of the contact nodes have attributes. They are type, name and id. The type attribute reflects what type, like phone misc or address. The name attribute is the displayed name when the data is drawn and displayed on a display page. The last and probably most important attribute is id. Id is a unique identifier that allows use to 'grab' a hold of the node, just like how we find and set the entry node. Once the node is found, sub nodes are created to fit the contact type and the data is filled all that is left to do is save. That's it.

    <%
    function AddContact()
    Dim root, xmlFE
    dim Entry_node, EntryContact_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 hidden 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 probably 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
    ' Ok, everything so far is ok, set the entry_node to the first node
    ' , and only node, found in the nodelist
    set Entry_node = Entry_node.item(0)
    ' now we set the contacts node for that entry node. 
    set EntryContact_node = Entry_node.getElementsbyTagName("contacts")
    ' If the contact type was phone then we create the node for what the phone
    ' data would look for. If it was for an address then we create the item node
    ' and the child nodes needed. If it was misc then its user field dependant
    ' and we just create the single node, expecting that the data will be put
    ' as the text of item.
    if request.form("ContactType") = "phone" then
    create_node objxml, EntryContact_node, "contact", request.form("data")
    set tmpnode = EntryContact_node.lastchild
    tmpnode.setAttribute "type", request.form("type")
    tmpnode.setAttribute "name", request.form("name")
    tmpnode.setAttribute "id", "156721" ' This is the unique identifier
    elseif request.form("ContactType") = "address" then
    create_node objxml, EntryContact_node, "contact", request.form("data")
    set tmpnode = EntryContact_node.lastchild
    tmpnode.setAttribute "type", request.form("type")
    tmpnode.setAttribute "name", request.form("name")
    tmpnode.setAttribute "id", "156721" ' This is the unique identifier
    create_node objxml, tmpnode, "street", request.form("street")
    create_node objxml, tmpnode, "city", request.form("city")
    create_node objxml, tmpnode, "state", request.form("state")
    create_node objxml, tmpnode, "zip", request.form("zip")
    elseif request.form("ContactType") = "misc" then
    create_node objxml, EntryContact_node, "contact", request.form("data")
    set tmpnode = EntryContact_node.lastchild
    tmpnode.setAttribute "type", request.form("type")
    tmpnode.setAttribute "name", request.form("name")
    tmpnode.setAttribute "id", "156721" ' This is the unique identifier
    end if
    ' Last but not least we save the information.
    objxml.save(server.mappath("abook.xml"))
    end function
    %>


    Below is a function used to add a profile item. For this address book a profile item isn't a contact information bit but more like user details like birthdays, signatures, etc. We can go about creating these in the same way as we do the contact elements. We find and set the entry node, using our error correction. Then create the child node as needed. Since the type is more user defined we don't have to worry about complex child node creation. All of them are going to be, roughly, the same type.

    <%
    function AddProfile()
    Dim root, xmlFE
    dim Entry_node, EntryContact_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
    ' Ok, everything so far is ok, set the entry_node to the first node
    ' , and only node, found in the node list
    set Entry_node = Entry_node.item(0)
    ' now we set the profile node for that entry node. 
    set EntryProfile_node = Entry_node.getElementsbyTagName("profile")
    ' Now we just create the item node and fill it with the type and data given.
    create_node objxml, EntryProfile_node, "item", request.form("data")
    set tmpnode = EntryProfile_node.lastchild
    tmpnode.setAttribute "type", request.form("type")
    tmpnode.setAttribute "name", request.form("name")
    tmpnode.setAttribute "id", "156721" ' This is the unique identifier
    ' Last but not least we save the information.
    objxml.save(server.mappath("abook.xml"))
    end function
    %>

    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 3 Hosted by Hostway
    Stay green...Green IT