CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2001
    Location
    LoS AnGeLeS
    Posts
    251

    Searching a XML document

    Hi, I am using XML 2.6 under C++. I have huge document that I have to search for specific tags. For example
    <info>
    <profile>
    <Name age="20">Rachel<Name/>
    <Name age="22">John<Name/>
    <profile/>
    </info>

    when I seach the above document for Name tag, I should be given those two nodes. What function can I use to do this?
    Also, what function do I use to get the attribute of these nodes?

    Thank you,

  2. #2
    Join Date
    Aug 2001
    Location
    Russia, Moscow
    Posts
    26
    Hi

    You can use XSL features.
    If you are using MSXML, ther is method IXMLDOMNode.selectNodes(XPath)
    where XPath is expression like this '//profile/name'.
    Indeed your XPath can be very sophisticated.

    For a full xpath syntax look through specification.
    Good luck
    Denis.

  3. #3
    Join Date
    Apr 2002
    Location
    Germany
    Posts
    224
    there are 2 ways of searching for a node in a large document
    here is my code:

    Code:
      string l_sSearchPattern;
      string l_sResult;
      IXMLDOMNode* myM3NameNode = NULL;
      HRESULT hr;
      // search for one node containing all tree attributes
      l_sSearchPattern += "//node()[@ComponentID=\""  +  
                          XMLHelperFunctions::longToString(M3MessageRef.getComponentID())  +  "\" and ";
      l_sSearchPattern += "@ElementID=\""             +
                          XMLHelperFunctions::longToString(M3MessageRef.getElementID())    +  "\" and ";
      l_sSearchPattern += "@EventID=\""               +  
                          XMLHelperFunctions::longToString(M3MessageRef.getEventID())      +  "\"]";
      hr = XMLHelperFunctions::searchNodeInDocument(myXMLDoc,l_sSearchPattern,&myM3NameNode);
      if ( hr != S_OK )
      {
        hr = XMLHelperFunctions::searchNodeInDocument(myXMLDoc2,l_sSearchPattern,&myM3NameNode);
        if( hr != S_OK )
        {
          l_sSearchPattern.erase();
          // search for nodes containing componentID with a subnode containing elementID and a subnode
          // containing the specified eventID
          l_sSearchPattern += "//node()[@ComponentID=\""  +  
                              XMLHelperFunctions::longToString(M3MessageRef.getComponentID())  +  "\"]//node()[ ";
          l_sSearchPattern += "@ElementID=\""             +
                              XMLHelperFunctions::longToString(M3MessageRef.getElementID())    +  "\"]//node()[ ";
          l_sSearchPattern += "@EventID=\""               +  
                              XMLHelperFunctions::longToString(M3MessageRef.getEventID())      +  "\"]";
          hr = XMLHelperFunctions::searchNodeInDocument(myXMLDoc,l_sSearchPattern,&myM3NameNode);
          if( hr != S_OK )
          {
            hr = XMLHelperFunctions::searchNodeInDocument(myXMLDoc2,l_sSearchPattern,&myM3NameNode);
            if( hr != S_OK )
    the helperfunctions:
    Code:
    HRESULT XMLHelperFunctions::searchNodeInDocument( IXMLDOMDocument* pDoc,
                                                      const string &sSearchPattern,
                                                      IXMLDOMNode** pReturnNode )
    {
      HRESULT hr = pDoc->selectSingleNode( asciiToBSTR(sSearchPattern.c_str()), pReturnNode );
    
      return hr;
    }
    
    //=================================================================================================
    
    HRESULT XMLHelperFunctions::searchNodeInDocument( IXMLDOMDocument* pDoc, 
                                                      const string &sSearchPattern, 
                                                      IXMLDOMNodeList** pReturnList )
    {
      HRESULT hr = pDoc->selectNodes( asciiToBSTR(sSearchPattern.c_str()), pReturnList);
    
      return hr;
    }
    HRESULT XMLHelperFunctions::getNodeAttributeValue( IXMLDOMNode* pNode,
                                                       const string &sAttribute,
                                                       string &sAttributeValue )
    {
      VARIANT l_varValue;
      VariantInit( &l_varValue );
      l_varValue.vt = VT_BSTR;
    
      HRESULT hr = getNodeAttributeValueVariant( pNode, sAttribute, &l_varValue );
    
      if( S_OK == hr )
      {
        sAttributeValue = BSTRtoString( l_varValue.bstrVal );
        VariantClear( &l_varValue );
      }
      return hr;
    }
    and to get the attribute you want:

    Code:
        hr = XMLHelperFunctions::getNodeAttributeValue(myM3NameNode,"Name",l_sResult);
    let me know if this serves your needs!
    CU

    Ingo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured