Listing B—cmdNew_Click and BuildNewBookNodes
Private Sub cmdNew_Click()
    Dim NewBookNode As MSXML2.IXMLDOMNode           'New root node for the new book item
    Dim DocFrag As MSXML2.IXMLDOMDocumentFragment   'Document fragment used to build new book item
    Dim valResult As MSXML2.IXMLDOMParseError
  
    EditCancelled = True
     
    'create the new book element
    Set NewBookNode = CatalogDoc.createElement("book")
    CatalogDoc.documentElement.appendChild NewBookNode
    'Build the child nodes of the book node and hand
    'it off to the edit form
    Set Form2.BookNode = BuildBookNodes(NewBookNode)
    Set Form2.CatalogDoc = CatalogDoc
    Form2.Show 1, Me
  
    If EditCancelled Then
        CatalogDoc.documentElement.removeChild NewBookNode
    Else
        'rebuild the UI
        BuildTree
    End If
End Sub
 
Private Function BuildBookNodes(RootNode As IXMLDOMNode) As IXMLDOMNode
    'Build the document structure needed to add a new book to the catalog
  
    Dim TitleNode As MSXML2.IXMLDOMNode     'Book title
    Dim AuthorNode As MSXML2.IXMLDOMNode    'Book Author
    Dim PriceNode As MSXML2.IXMLDOMNode     'Book Price
    Dim GenreNode As MSXML2.IXMLDOMNode     'Book Genre
    Dim DescNode As MSXML2.IXMLDOMNode      'Book Description
    Dim PubDateNode As MSXML2.IXMLDOMNode   'Book publish date
    Dim ID As MSXML2.IXMLDOMAttribute       'ID attribute for the book
    Dim CatalogNode As MSXML2.IXMLDOMNode   'The document's root catalog element
  
    'Create the ID attribute node for the new book
    Set ID = CatalogDoc.createAttribute("id")
    'Calculate the value of the new ID attribute
    Set CatalogNode = CatalogDoc.documentElement
    ID.Value = "bk" & CatalogNode.childNodes.length
  
    'Create all the new data nodes
    Set TitleNode = CatalogDoc.createElement("title")
    Set AuthorNode = CatalogDoc.createElement("author")
    Set PriceNode = CatalogDoc.createElement("price")
    Set GenreNode = CatalogDoc.createElement("genre")
    Set DescNode = CatalogDoc.createElement("description")
    Set PubDateNode = CatalogDoc.createElement("publish_date")
  
    'add space for text under all the new nodes
    TitleNode.appendChild CatalogDoc.createTextNode("")
    AuthorNode.appendChild CatalogDoc.createTextNode("")
    PriceNode.appendChild CatalogDoc.createTextNode("")
    GenreNode.appendChild CatalogDoc.createTextNode("")
    DescNode.appendChild CatalogDoc.createTextNode("")
    PubDateNode.appendChild CatalogDoc.createTextNode("")
  
    'Add the new nodes to the root "book" node
    'Note that the append order is important
    RootNode.Attributes.setNamedItem ID
    RootNode.appendChild AuthorNode
    RootNode.appendChild TitleNode
    RootNode.appendChild GenreNode
    RootNode.appendChild PriceNode
    RootNode.appendChild PubDateNode
    RootNode.appendChild DescNode
  
    'Return the new root "book" node
    Set BuildBookNodes = RootNode
End Function