Link to home
Start Free TrialLog in
Avatar of sajana
sajana

asked on

Retrieve values from an xml file using c++



 i am not able to retrieve the values from the xml file .
How do u get the value of a specific node ?
My xml file looks like this

<?XML version="1.0">
<values>
<val>
<value1>2</value1>
<value2>4</value2>
</val>
</values>
Now i want to retrieve the value of the value1 node and i have specified in the code

VARIANT *Name;
      CComBSTR str1("value1");
   
   
    node->selectSingleNode(_bstr_t(str1.Copy ()));
      
         node->get_text (&nodeName);
    printf("The  text is %S",nodeName);

But it gives me the value of both the nodes together i.e 2 and 4
i want to retrieve the values based on the tagnames ie the text for value1 is 2 and the text for value2 is 4.
how can you do this?
Avatar of LDC
LDC

What program are you using to parse the xml?
ASKER CERTIFIED SOLUTION
Avatar of talik
talik

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sajana

ASKER

node is a interface pointer and i am using namespaces also.


xml::IXMLDOMNode* node;
CComBSTR str1("value1");

node->selectSingleNode(_bstr_t(str1.Copy ()));
     
      
    node->get_text (&nodeName);
    printf("The  text is %S",nodeName);
//

after i modified my code as
node = node->selectSingleNode (...);
i get a runtime error

It really does not help
Please anyone could guide me as to how to extract the value from a node based on its tagname .

Thanks

Regards
Sajana
Hi Sajana,

It would adwise to use #import for msxml.dll and use smart pointers.

Then, you code should look like this:
(this code compiles and gives right result for me)

#import "msxml.dll" rename_namespace ("xml")

void CTxmlDlg::OnButton1()
{
      OleInitialize (NULL);

      try
      {
            xml::IXMLDOMDocumentPtr pDoc (__uuidof (xml::DOMDocument));
            pDoc->loadXML ("<values><val><value1>2</value1><value2>4</value2></val></values>");

            xml::IXMLDOMNodePtr pRootNode = pDoc->selectSingleNode (_bstr_t (_T ("values")));
            xml::IXMLDOMNodePtr pValNode = pRootNode->selectSingleNode (_bstr_t (_T ("val")));
            xml::IXMLDOMNodePtr pValue1Node = pValNode->selectSingleNode (_bstr_t (_T ("value1")));
            _bstr_t text = pValue1Node->Gettext ();

      }
        catch (_com_error err)
        {
       
        }
      catch (...)
      {

      }

      OleUninitialize ();
}

You can call selectSingleNode for root with a query "values/val/value1" instead.
Additional comment:
 If you want to select a single node ANYWHERE in document you should use double slash before the node name.

For example, you can change my exeample in the following way:

instead of

xml::IXMLDOMNodePtr pRootNode = pDoc->selectSingleNode (_bstr_t (_T ("values")));
xml::IXMLDOMNodePtr pValNode = pRootNode->selectSingleNode (_bstr_t (_T ("val")));
xml::IXMLDOMNodePtr pValue1Node = pValNode->selectSingleNode (_bstr_t (_T ("value1")));

you can write:
xml::IXMLDOMNodePtr pValue1Node = pDoc->selectSingleNode (_bstr_t (_T ("//value1")));

Avatar of sajana

ASKER

Talik -

Thanks a lot .It really works.

You guys are simply great.

I have already graded you for your answer and also given you points  for the same.

Thanks once again

Bye

Sajana