Click to See Complete Forum and Search --> : Searching a XML document


ravash5000
June 13th, 2002, 07:14 PM
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,

dkar
June 14th, 2002, 12:44 AM
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

mao_03
June 14th, 2002, 06:50 AM
there are 2 ways of searching for a node in a large document
here is my 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:

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:


hr = XMLHelperFunctions::getNodeAttributeValue(myM3NameNode,"Name",l_sResult);


let me know if this serves your needs!