CppDom.cpp
上传用户:sz0451
上传日期:2022-07-29
资源大小:256k
文件大小:3k
源码类别:

.net编程

开发平台:

Visual C++

  1. // This is the main project file for VC++ application project 
  2. // generated using an Application Wizard.
  3. #include "stdafx.h"
  4. #using <mscorlib.dll>
  5. #using <System.xml.dll>
  6. using namespace System;
  7. using namespace System::Xml;
  8. using namespace System::Collections;
  9. __gc class XmlBuilder
  10. {
  11. private:
  12.     XmlNode* root;
  13.     XmlDocument* doc;
  14.     XmlNodeList* xnl;
  15. public:
  16.     XmlBuilder(String* path)
  17.     {
  18.         // Create the XmlDocument
  19.         doc = new XmlDocument();
  20.         // Load the data
  21.         doc->Load(path);
  22.         Console::WriteLine("Document loaded");
  23.         // Get the root of the tree
  24.         root = doc->DocumentElement;
  25.         // get the child node list
  26.         xnl = doc->ChildNodes;
  27.         IEnumerator* ie = xnl->GetEnumerator();
  28.         while (ie->MoveNext() == true)
  29.             Console::WriteLine("Child: {0}",
  30.                                (dynamic_cast<XmlNode*>(ie->Current))->Name);
  31.     }
  32.     void processChildNodes()
  33.     {
  34.         // Declare an enumerator
  35.         IEnumerator* ie = xnl->GetEnumerator();
  36.         while (ie->MoveNext() == true)
  37.         {
  38.             // Get a pointer to the node
  39.             XmlNode* pNode = dynamic_cast<XmlNode*>(ie->Current);
  40.             // See if it is the root
  41.             if (pNode->NodeType == XmlNodeType::Element && 
  42.                                 pNode->Name->Equals(S"geology"))
  43.             {
  44.                 Console::WriteLine("  Found the root");
  45.                 processRoot(pNode);
  46.             }
  47.         }
  48.     }
  49.     void processRoot(XmlNode* rootNode)
  50.     {
  51.         XmlNode* pVolc = dynamic_cast<XmlNode*>(rootNode->ChildNodes->Item(1));
  52.         // Create a new volcano element
  53.         XmlElement* newVolcano = createNewVolcano();
  54.         // Link it in
  55.         root->InsertBefore(newVolcano, pVolc);
  56.     }
  57.     XmlElement* createNewVolcano()
  58.     {
  59.         // Create a new element
  60.         XmlElement* newElement = doc->CreateElement("volcano");
  61.         // Set the name attribute
  62.         XmlAttribute* pAtt = doc->CreateAttribute("name");
  63.         pAtt->Value = S"Mount St.Helens";
  64.         newElement->Attributes->Append(pAtt);
  65.         // Create the location element
  66.         XmlElement* locElement = doc->CreateElement("location");
  67.         XmlText* xt = doc->CreateTextNode(S"Washington State, USA");
  68.         locElement->AppendChild(xt);
  69.         newElement->AppendChild(locElement);
  70.         return newElement;
  71.     }
  72.     void printTree()
  73.     {
  74.         XmlTextWriter* xtw = new XmlTextWriter(Console::Out);
  75.         xtw->Formatting = Formatting::Indented;
  76.         doc->WriteTo(xtw);
  77.         xtw->Flush();
  78.         Console::WriteLine();
  79.     }
  80. };
  81. // This is the entry point for this application
  82. #ifdef _UNICODE
  83. int wmain(void)
  84. #else
  85. int main(int argc, char* argv[])
  86. #endif
  87. {
  88.     // Check for required arguments
  89.     if (argc < 2)
  90.     {
  91.         Console::WriteLine(S"Usage: CppWriter path");
  92.         return -1;
  93.     }
  94.     String* path = new String(argv[1]);
  95.     try
  96.     {
  97.         XmlBuilder* pf = new XmlBuilder(path);
  98.         pf->processChildNodes();
  99.         pf->printTree();
  100.     }
  101.     catch(Exception* pe)
  102.     {
  103.         Console::WriteLine(pe->Message);
  104.     }
  105.     return 0;
  106. }