⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xml_parser.cpp

📁 功能:首先
💻 CPP
📖 第 1 页 / 共 3 页
字号:
bool XML_PARSER::Go_Forward()
{       // Sibling node
        //
        MSXML::IXMLDOMNodePtr pSibling = NULL;

        if(Is_MSXML_Node(this->CurrentNode) != MSXML::NODE_ELEMENT && Is_MSXML_Node(this->CurrentNode) != MSXML::NODE_CDATA_SECTION)
                return false;

        pSibling = CurrentNode->nextSibling;
        if(pSibling == NULL) return false;
        CurrentNode = pSibling;

        // Grab Information from Sibling node
        //
        attrib_index = -1;
        this->Grab_Node_Informations(CurrentNode);
        return true;
}

bool XML_PARSER::Go_Backward()
{       // Sibling node
        //
        MSXML::IXMLDOMNodePtr pSibling;

        if(Is_MSXML_Node(this->CurrentNode) != MSXML::NODE_ELEMENT && Is_MSXML_Node(this->CurrentNode) != MSXML::NODE_CDATA_SECTION)
                return false;

        pSibling = CurrentNode->previousSibling;
        if(pSibling == NULL) return false;
        CurrentNode = pSibling;

        // Grab Information from Sibling node
        //
        attrib_index = -1;
        this->Grab_Node_Informations(pSibling);
        return true;
}

bool XML_PARSER::Go_to_Child(LPCTSTR NodeName)
{
        // Child node
        //
        MSXML::IXMLDOMNodePtr pChild;

        if(Is_MSXML_Node(this->CurrentNode) != MSXML::NODE_ELEMENT)
                return false;

        ChainTag_Add(this->Get_CurrentTag());

        for (pChild = CurrentNode->firstChild;  NULL != pChild;  pChild = pChild->nextSibling)
        {
                // Grab Information from Child node
                //
                this->Grab_Node_Informations(pChild);

                if(this->Get_CurrentName() == NodeName)
                {       // Update new Position
                        //
                        attrib_index = -1;
                        this->CurrentNode = pChild;
                        return true;
                }
        }

        // Node Not Found (Restore Node informations)
        //
        this->ChainTag_Remove(1);
        this->Grab_Node_Informations(this->CurrentNode);
        return false;
}

bool XML_PARSER::Go_to_Descendant(LPCTSTR NodeName)
{
        // Child node
        //
        MSXML::IXMLDOMNodePtr pChild;

        // Current Node before the call method
        //
        MSXML::IXMLDOMElementPtr pCurrent = this->CurrentNode;

        if(Is_MSXML_Node(this->CurrentNode) != MSXML::NODE_ELEMENT)
                return false;

        ChainTag_Add(this->Get_CurrentTag());

        for (pChild = CurrentNode->firstChild;  NULL != pChild;  pChild = pChild->nextSibling)
        {
                // Grab Information from Child node
                //
                this->Grab_Node_Informations(pChild);

                if(this->Get_CurrentName() == NodeName)
                {       // Update new Position
                        //
                        attrib_index = -1;
                        this->CurrentNode = pChild;
                        return true;
                }

                // Try to go into Childs of this Child
                //
                this->CurrentNode = pChild;
                if( this->Go_to_Descendant(NodeName) )
                { // We find the approriate node
                  // All is set, we can return
                  //
                  return true;
                }

                // Restore Current node
                //
                this->CurrentNode = pCurrent;
        }

        // Node Not Found (Restore Node informations)
        //
        this->ChainTag_Remove(1);
        this->Grab_Node_Informations(this->CurrentNode);
        return false;
}

bool XML_PARSER::Go_to_Parent(LPCTSTR NodeName)
{
        // Parent node
        //
        MSXML::IXMLDOMNodePtr pParent = NULL;
        MSXML::IXMLDOMNodePtr oldCurrent = this->CurrentNode;

        if(this->CurrentNode == this->m_pDocRoot)
                return false;

        CString oldChainTag = this->ChainTag;

        this->CurrentNode->get_parentNode(&pParent);

        while (true)
        {
                this->CurrentNode = pParent;
                this->ChainTag_Remove(1);
                this->Grab_Node_Informations(this->CurrentNode);
                if(this->Get_CurrentName() == NodeName)
                {       attrib_index = -1;
                        return true;
                }

                if(this->CurrentNode == this->m_pDocRoot)
                        break;

                this->CurrentNode->get_parentNode(&pParent);
        }

        // Parent not found
        //
        this->CurrentNode = oldCurrent;
        this->ChainTag = oldChainTag;
        this->Grab_Node_Informations(this->CurrentNode);
        return false;
}

// Go to a Node attached at the same Node than the Current Node (Forward sens)
//
bool XML_PARSER::Go_Forward(LPCTSTR NodeName)
{
        // Sibling node
        //
        MSXML::IXMLDOMNodePtr pSibling = NULL;

        if(Is_MSXML_Node(this->CurrentNode) != MSXML::NODE_ELEMENT && Is_MSXML_Node(this->CurrentNode) != MSXML::NODE_CDATA_SECTION)
                return false;

        for (pSibling= CurrentNode;  true ;  pSibling = pSibling->nextSibling)
        {
                if(pSibling == NULL)
                        break;

                // Grab Information from Sibling node
                //
                this->Grab_Node_Informations(pSibling);

                if(this->Get_CurrentName() == NodeName)
                {       // Update new Position
                        //
                        attrib_index = -1;
                        this->CurrentNode = pSibling;
                        return true;
                }
        }

        // Node Not Found (Restore Node informations)
        //
        this->Grab_Node_Informations(this->CurrentNode);
        return false;
}

bool XML_PARSER::Go_Backward(LPCTSTR NodeName)
{
        // Sibling node
        //
        MSXML::IXMLDOMNodePtr pSibling;

        if(Is_MSXML_Node(this->CurrentNode) != MSXML::NODE_ELEMENT && Is_MSXML_Node(this->CurrentNode) != MSXML::NODE_CDATA_SECTION)
                return false;

        for (pSibling = CurrentNode;  NULL != pSibling;  pSibling = pSibling->previousSibling)
        {
                // Grab Information from Sibling node
                //
                this->Grab_Node_Informations(pSibling);

                if(this->Get_CurrentName() == NodeName)
                {       // Update new Position
                        //
                        attrib_index = -1;
                        this->CurrentNode = pSibling;
                        return true;
                }
        }

        // Node Not Found (Restore Node informations)
        //
        this->Grab_Node_Informations(this->CurrentNode);
        return false;
}

bool XML_PARSER::Remove()
{
        // Parent node
        //
        MSXML::IXMLDOMNodePtr pParent= NULL;

        if(CurrentNode == NULL)
        {       if(this->m_pDocRoot != NULL)
                        this->CurrentNode = this->m_pDocRoot;
                else
                        return false;
        }

        if(this->CurrentNode != this->m_pDocRoot)
        {       this->CurrentNode->get_parentNode(&pParent);
                pParent->removeChild(this->CurrentNode);
                this->CurrentNode = pParent;
                this->Grab_Node_Informations(this->CurrentNode);
        }
        else
                this->Reset_XML_Document();

        attrib_index = -1;
        return true;
}

bool XML_PARSER::RemoveChild(LPCTSTR NodeName)
{
        // Find the Child
        //
        if(Is_MSXML_Node(this->CurrentNode) != MSXML::NODE_ELEMENT) return false;

        if( this->Go_to_Child(NodeName) )
        {       int cur_attrib_index = attrib_index;
                bool result = this->Remove();

                attrib_index = cur_attrib_index;
                return result;
        }
        else
                return false;
}

// ***************************
// ** Header XML Management **
// ***************************
bool XML_PARSER::Set_Header(LPCTSTR header,LPCTSTR name,LPCTSTR value)
{
      lasterror = ok;
      bool empty_xml = false;

      CString strHeader = header;
      CString strName = name;
      CString strValue = value;
      
      BSTR bstr;
      CString cstr,cstr2;

      MSXML::IXMLDOMNodePtr pChild = NULL;           // Working node
      pChild = m_plDomDocument->firstChild;   // Start at first document child

      
      
      if(pChild == NULL)
        empty_xml = true;
      
      while(pChild != NULL)
      { 
         if(pChild == m_pDocRoot) 
         {  // Root document reach, it's done, the processing node wasn't found
            //
            break;
         }
           
         if(pChild->nodeType != MSXML::NODE_PROCESSING_INSTRUCTION) 
         {  pChild = pChild->nextSibling;          // Go to Next Processing instruction node
            continue;
         }
        
            
         pChild->get_baseName(&bstr);
         cstr = bstr;
         if(cstr == header) 
         {    
           // Correct header, check the correct property
           //
           pChild->get_text(&bstr);
           cstr = bstr;

           int index = cstr.Find(name,0);
           if(index == -1)
           {  // The property doesn't exist on this processing instruction");
              //
               
              // Assume correct constraint about "xml" processing instruction
              //
              {
                 // must have version="xxx" in first
                 // must have standalone="xxx" in last if exist
                 //
                 cstr2.Empty();
                 int standalone_index = cstr.Find("standalone",0);
                 if(standalone_index != -1)
                 {   cstr2 = cstr.Right( cstr.GetLength() - standalone_index + 1);
                     cstr  = cstr.Left(standalone_index);
                 }
   
                 int version_index = cstr.Find("version",0);
                 if(version_index == -1 && strHeader == "xml")
                 {   
                    CString strTemp = cstr;
                    cstr = "version=\"1.0\" ";
                    cstr += strTemp;
                 }

                 if(strName != "version")
                    cstr += " " + strName + "=\"" + strValue + "\" " + cstr2;
                 else
                    cstr += cstr2;
              }
              
              // Create the new Processing Instruction node
              //
              HRESULT hr;
              MSXML::IXMLDOMProcessingInstruction *pIXMLDOMPInstr = NULL;
              hr = m_plDomDocument->raw_createProcessingInstruction(_bstr_t(strHeader), _bstr_t(cstr), &pIXMLDOMPInstr);

              if(SUCCEEDED(hr))
              {   
                 // We succes the creation of the processing instruction
                 // Replace the node
                 //
                 m_plDomDocument->replaceChild(pIXMLDOMPInstr,pChild);
              }
              else
              {  // Arf, i fails the creation, grrr, again
                 //
                 lasterror="XML_PARSER::Set_Header() : Can't create the new processing instruction node";
                 return false;
              }
              return true;
           }
           else
           {  // The processing instruction node exist, must change it's value !! 
              //
              int start,end;
              start = cstr.Find('"',index);
              if(start == -1)
              {  lasterror = "XML_PARSER::Set_Header() : bad value structure";
                 return false;
              }
              end = cstr.Find('"',start+1);
              if(end == -1) 
              {  lasterror = "XML_PARSER::Set_Header() : bad value structure";
                 return false;
              }

              cstr2 = cstr.Mid(0,start+1) + value + cstr.Mid(end,cstr.GetLength() - end);
              
              MSXML::IXMLDOMNodePtr m_lpNode = NULL;
              MSXML::IXMLDOMProcessingInstruction *pIXMLDOMPInstr = NULL;
              HRESULT hr;
         
              hr = m_plDomDocument->raw_createProcessingInstruction(_bstr_t(strHeader), _bstr_t(cstr2), &pIXMLDOMPInstr);

              if(SUCCEEDED(hr))
              {
                 // We succes the creation of the processing instruction
                 // Replace the node
                 //
                 m_plDomDocument->replaceChild(pIXMLDOMPInstr,pChild);
              }
              else
              {  lasterror="XML_PARSER::Set_Header() : Can't create the new processing instruction node";
                 return false;
              }
              
              return true;
           }
        }
          
        pChild = pChild->nextSibling;   // Next Processing instruction node
     }

     // No processing node for our header 
     //
     { 
        if(strName != "version" && strHeader == "xml")
          cstr = "version=\"1.0\" " + strName + "=\"" + strValue + "\"";
        else
          cstr = strName + "=\"" + strValue + "\"";

        MSXML::IXMLDOMNodePtr m_lpNode = NULL;
        MSXML::IXMLDOMProcessingInstruction *pIXMLDOMPInstr = NULL;
        HRESULT hr;

        hr = m_plDomDocument->raw_createProcessingInstruction(_bstr_t(strHeader), _bstr_t(cstr), &pIXMLDOMPInstr);
        if(SUCCEEDED(hr))
        {  
          if(!empty_xml)
          {  _variant_t NodeRef = (IUnknown *)this->m_pDocRoot;
             m_lpNode = NULL;
             m_lpNode = m_plDomDocument->insertBefore(pIXMLDOMPInstr,NodeRef);
             if(m_lpNode == NULL) lasterror = "PARSER_XML::SetHeader() : Can't insert Processing node after the root document";
             return (m_lpNode != NULL);
          }
          else
          {  m_lpNode = NULL;
             m_lpNode = m_plDomDocument->appendChild(pIXMLDOMPInstr);
             if(m_lpNode == NULL) lasterror = "PARSER_XML::SetHeader() : Can't insert Processing node in the empty document";
             return (m_lpNode != NULL);
          }
        }
        
        lasterror = "PARSER_XML::SetHeader() : Can't create new Processing node";
        return false;
     } 
}

bool XML_PARSER::Get_Header(LPCTSTR header,LPCTSTR name,CString & res)
{
      lasterror = ok;

      MSXML::IXMLDOMNodePtr pChild;                  // Working node
      res.Empty();

      pChild = m_plDomDocument;
      pChild = m_plDomDocument->firstChild;   // Start at first document child

      if(pChild == NULL) 
      {  lasterror = "XML_PARSER::Get_Header() : The XML Document is a null pointer";
         return false;  
      }

      while(pChild != NULL)
      {
          if(pChild->nodeType != MSXML::NODE_PROCESSING_INSTRUCTION) break;

          BSTR bstr;
          CString cstr;
          
          pChild->get_baseName(&bstr);
          cstr = bstr;
          if(cstr == header) 
          {    // Correct header, check the correct property
               //
               pChild->get_text(&bstr);
               cstr = bstr;

               int index = cstr.Find(name,0);
               if(index == -1)
                  return false;
            
               int start,end;
               start = cstr.Find('"',index);
               if(start == -1) 
               {   lasterror = "XML_PARSER::Get_Header() : bad value structure";
                   return false;
               }

               end = cstr.Find('"',start+1);
               if(end == -1) 
               {   lasterror = "XML_PARSER::Get_Header() : bad value structure";
                   return false;
               }


               res = cstr.Mid(start+1,end-start-1);
               return true;
          }
          
          pChild = pChild->nextSibling;   // Next Processing instruction node
     }

     return false;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -