SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Building a Content Server with XML and ASP : Editing Files

Our story editor allows content to be added, edited, and deleted. Every time a story is manipulated, the index page needs to be updated. We perform the updates using the XML document object model. The XML document object model provides a complete representation of the XML document tree. To manipulate the XML document, you need to traverse or query the tree for a specific node. For example, looking back at our index.xml file:

<?xml version="1.0"?>
<storyindex>
 <article>
   <id>1</id>
   <title>My First Article</title>
   <author>Scott Isaacs</author>
 </article>
 <article>
   <id>2</id>
   <title>My Second Article</title>
   <author>Scott Isaacs</author>
 </article>
</storyindex>

The storyindex element is the root node of our tree. It currently contains a child node list (a collection of nodes) consisting of the two article elements. Each article has a child node list consisting of an id, title, and author. To manipulate the document, you need to traverse this hierarchy. You can either traverse this hierarchy manually analyzing each node's children or you can use the DOM to query for a node.

Stories are edited based on the ID. When a story is going to be edited, we need to locate the appropriate article in the index.xml file based on this ID. We can either do this by walking the entire document looking for the ID or we can use the selectSingleNode method to query the document. The latter method efficiently accomplishes in one statement the same result as manually writing many lines of complex looping (and many times recursive) code.

The selectSingleNode method takes an XSL pattern and returns the matching node. The pattern language enables queries to be executed against the XML document. When a story is edited, we execute a query that returns the article node containing the story with specified ID. For example, if the first story is edited (eg, id=1) we query the index as follows:

storyindex/article[id[.= "1"]]

This query looks inside of the storyindex element for an article element that contains an id element with the content of "1". The brackets "[]" around the ID element specifies the criteria that must be matched to return the article node. The "." is used to match the contents of the ID element to a value (in this case "1"). The pattern matching syntax is extremely powerful and will be the topic of a future article. In the meantime, you can find a pattern matching reference at Microsoft's XML Web-Site.

We use the pattern matching syntax from the XML DOM as follows:

' Open the index XML file

StoryID = request.queryString("storyID")

set indexFile = Server.CreateObject("Microsoft.XMLDOM")
indexFile.async = false
indexFile.load server.mappath("src/index.xml")

' story being edited
if (storyid<>"") then
  ' Find the article node
  set nodeStory = indexFile.selectSingleNode("storyindex/article[id[.=" _
    & chr(34) & storyid & chr(34) & "]]")
  ' Story found
  if not nodeStory is nothing then
    ' Locate title and author 
    sTitle = editNode.selectSingleNode("title").text
    sAuthor = editNode.selectSingleNode("author").text

    ' Read the XML story from disk
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    sPath = server.mappath("src/" & (storyID) & ".xml")
    if objFSO.FileExists(sPath) Then
      Set objFile = objFSO.OpenTextFile(sPath)
      ' Escape any ampersands
      sStory = replace(objFile.readAll,"&","&amp;")
      objFile.close
    end if
  end if
end if

This retrieves an existing story and stores the values in three variables: sTitle, sAuthor, and sStory. Inserting these values into the form is elementary ASP scripting:

<FORM METHOD=post>
  <TABLE><TR>
    <TD>Title:</TD>
    <TD><INPUT TYPE="text" NAME=title value="<%=sTitle%>">
  </TD></TR>
  <TR>
    <TD>Author:</TD>
    <TD><INPUT TYPE="text" NAME=author VALUE="<%=sAuthor%>">
  </TD></TR></TABLE>

  <TEXTAREA NAME=story ROWS=15 COLS=60>
    <%=sStory%>
  </TEXTAREA>
  <INPUT TYPE=hidden NAME=storyid VALUE="<%=storyid%>">
  <BR><INPUT TYPE=submit VALUE="Save Story">
  <% 
    ' If story being edited, display delete button
    if storyid<>"" then %>
    <INPUT TYPE=submit NAME='Delete' 
      VALUE='Delete Story' 
      ONCLICK='return confirm("Do you want to delete this story?")'>
  <% end if %>
</FORM>

This demonstrates how the articles are retrieved for editing. Next we discuss what happens when the user saves or deletes the article.