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?
sajanaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LDCCommented:
What program are you using to parse the xml?
0
talikCommented:
Hi,

Try to modify your code as following:

//assuming you're using smart pointers
node = node->selectSingleNode (...);

otherwise, you still get the text from the "root" node, that really shoud be 2 and 4.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sajanaAuthor Commented:
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
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

talikCommented:
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.
0
talikCommented:
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")));

0
sajanaAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.