Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf
662 Project 6 CREATING AN ATL SERVER APPLICATION
//Create an instance of the XML parser
HRESULT hr=ptXMLDOM.CoCreateInstance(_uuidof(DOMDocument)); if(FAILED(hr))
return;
if(ptXMLDOM.p==NULL)
return;
VARIANT_BOOL bSuccess=false;
CComVariant strP(strPath.AllocSysString()); //Convert the path from CString to Variant type
hr=ptXMLDOM->load(strP,&bSuccess); //Load the XML file.
if(FAILED(hr)) |
//Check for error |
return; |
|
After the XML document is loaded, a reference to the root node is to be retrieved. To achieve this, the DOM object exposes a function, selectSingleNode, which enables you to search for a node by its name. This function returns a reference to the node if it exists. All the guest nodes that will be added for each visitor will be appended to the root node, which is guestbook node in GuestBook.xml.
//Remember COM requires strings in the BSTR type CComBSTR bstrRoot(L”guestbook”);
//Search for and initialize the root (<guestbook> node) CComPtr<IXMLDOMNode> ptRootNode; hr=ptXMLDOM->selectSingleNode(bstrRoot,&ptRootNode); if(FAILED(hr))
return;
if(ptRootNode.p==NULL)
return;
The next step is to create a new Guest node. For that you will use the DOM function createNode. This function accepts the type of the node to be created, the name for the node (“Guest” for all nodes in this application), and a variable to store the reference to the newly created node.
CREATING A GUEST BOOK APPLICATION |
Chapter 21 |
663 |
|
|
|
|
|
CComBSTR bstrText;
//Create a new Guest node CComPtr<IXMLDOMNode> ptGuestNode;
hr=ptXMLDOM-createNode(CComVariant(NODE_ELEMENT), CComBSTR(“Guest”),NULL, &ptGuestNode);
The following are the steps to create a Name node. You will follow the same steps to create the Email, Website, and Comments nodes.
CComPtr<IXMLDOMNode> ptNameNode;
//Create a pointer to store the reference to the Name node CComPtr<IXMLDOMNode> ptInsertedNode;
hr=ptXMLDOM->createNode(CComVariant(NODE_ELEMENT),
//The syntax requires a dummy node to store the reference for the new node
CComBSTR(“Name”),NULL, &ptNameNode); //Create a node with the name Name
After the Name node has been created, you will have to create a text node, which will contain the name of the visitor. The text node, once created, will be appended to the Name node as a child node. Consider that you need to create a node, under the Guest node, with the following structure:
<Name>John Doe</Name>
The following are the steps required to accomplish this task:
1.Create a node with the name Name.
2.Create a text node with the text John Doe.
3.Append the text node to the Name node.
4.Append the Name node to the Guest node.
To add a new node, you will use the createNode method of the DOM. You can create a new text node using the createTextNode method and append the text node to another node using the appendChild method:
CComPtr<IXMLDOMText> ptTxtNode;
//create a pointer to store reference to a text node
664 Project 6 CREATING AN ATL SERVER APPLICATION
hr=ptXMLDOM->createTextNode(CComBSTR(szName),&ptTxtNode); //Create a text node with the name of visitor as the text
hr=ptNameNode->appendChild(ptTxtNode,&ptInsertedNode); //Append the text node to the Name node
ptGuestNode->appendChild(ptNameNode, &ptInsertedNode); //Append the Name node to the Guest node
Repeat these steps to create the Email, Website, and Comments nodes and add them to the Guest node.
Finally, append the Guest node to the root node and save the contents of the XML DOM into the file using the following code:
ptRootNode->appendChild(ptGuestNode, &ptInsertedNode);
hr=ptXMLDOM->save(CComVariant(strP));
The Guest Book application is now ready for testing and deployment. Once the project has been published, a virtual directory named GuestBook will be created in the inetpub/wwwroot folder of your IIS installation.
Creating the Empty XML File
Create an XML file using any text editor and add the following text to it:
<?xml version=”1.0”?>
<guestbook/>
Save the file as GuestBook.xml.
After you deploy the project, copy the GuestBook.html (created earlier) and GuestBook.xml files to the inetpub/wwwroot/GuestBook folder. Set GuestBook.html file as the default document using the IIS Manager (alternatively, rename the GuestBook.html as Default.htm). Run Internet Explorer and connect to http://localhost/GuestBook and verify the functioning of the Guest Book application.
CREATING A GUEST BOOK APPLICATION |
Chapter 21 |
665 |
|
|
|
|
|
Summary
In this chapter, you created a Guest Book application using ATL Server. You learned about the various components of the ATL Server, such as the SRF, Web application DLLs, and the ISAPI extension DLL. You also learned to use these components to create high-performance Web sites and Web applications.
This page intentionally left blank
PARTVIIIProfessional
Project – 7
Project 7
Creating Web
Services
