• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

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?
0
sajana
Asked:
sajana
  • 3
  • 2
1 Solution
 
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now