Link to home
Start Free TrialLog in
Avatar of 4eyesgirl
4eyesgirl

asked on

Need sample code for MSXML in C/C++

Hi expert,

I search around the codes in MSDN but they are not very clear to me.  This is the first time I use MSXML.

I would like a sample program to do the following (this can help me to understand how to use MSXML to store data in a XML file.

For example, here is the example XML file
<?xml version="1.0"?>
<?xml:stylesheet type="text/xsl" href="stock.xsl"?>
<petstpre xmlns:dt="urn:schemas-microsoft-com:datatypes">
 <user name="abc">
  <pets>
   <pet name="tiger">
   <type>cat</type>
   <color>white</color>
   </pet>
   <pet name="kitty">
   <type>cat</type>
   <color>black</color>
   </pet>  
   <pets>
 </user>
 <user name="xyz">
  <pets>
   <pet name="riley">
   <type>dog</type>
   <color>brown</color>
   </pet>
   <pet name="romeo">
   <type>bird</type>
   <color>yellow</color>
   </pet>  
   <pets>
  </user>
  <user name="def">
  <pet name="heidi">
   <type>dog</type>
   <color>white</color>
   </pet>
  </user>
</petstore>

I want a sample program does the following:
Load data from this XML file.
Add all data from the XML to the XMLDOMDocument object
Query the data from the XMLDOMDocument (for example, search the owner's name= "abc" and return the list of pets for this user.
Insert new pet for this user ("abc").
Delete pet for this user ("abc")

At this point, I should be modify this XMLDOMDocument object already and write it back to the external file.

I hope to hear from you. THX
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

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 4eyesgirl
4eyesgirl

ASKER

Any idea why I am getting this link error?

atlsd.lib(atlbase.obj) : error LNK2001: unresolved external symbol __CrtDbgReport
This error is caused by #include <atlbase.h>
Problem solves.  It is related to how I create the project.

I am still looking at the codes and see if it make sense to you.  I will let you know hows going.

Thanks
Jkr -


I download the C++ Wrapper classes for the COM interfaces of Microsoft XML parser (MSXML)") from code project and convert the sln file to VS2005 and I am getting the following compiling error.  Any idea why?

1> WINVER not defined. Defaulting to 0x0502 (Windows Server 2003)
1>c:\documents and settings\lofana\my documents\download\code projects\xml parser\source\interfacewrapper.h(274) : error C2491: 'CInterfaceCallingWrapper<T>::CInterfaceCallingWrapper' : definition of dllimport function not allowed
1>c:\documents and settings\lofana\my documents\download\code projects\xml parser\source\interfacewrapper.h(281) : error C2491: 'CInterfaceCallingWrapper<T>::CInterfaceCallingWrapper' : definition of dllimport function not allowed
1>c:\documents and settings\lofana\my documents\download\code projects\xml parser\source\interfacewrapper.h(287) : error C2491: 'CInterfaceCallingWrapper<T>::CInterfaceCallingWrapper' : definition of dllimport function not allowed
1>c:\documents and settings\lofana\my documents\download\code projects\xml parser\source\interfacewrapper.h(292) : error C2491: 'CInterfaceCallingWrapper<T>::~CInterfaceCallingWrapper' : definition of dllimport function not allowed
1>c:\documents and settings\lofana\my documents\download\code projects\xml parser\source\interfacewrapper.h(301) : error C2491: 'CInterfaceCallingWrapper<T>::operator T *' : definition of dllimport function not allowed
1>c:\documents and settings\lofana\my documents\download\code projects\xml parser\source\interfacewrapper.h(308) : error C2491: 'CInterfaceCallingWrapper<T>::operator *' : definition of dllimport function not allowed
1>c:\documents and settings\lofana\my documents\download\code projects\xml parser\source\interfacewrapper.h(315) : error C2491: 'CInterfaceCallingWrapper<T>::operator &' : definition of dllimport function not allowed
1>c:\documents and settings\lofana\my documents\download\code projects\xml parser\source\interfacewrapper.h(322) : error C2491: 'CInterfaceCallingWrapper<T>::operator ->' : definition of dllimport function not allowed
1>c:\documents and settings\lofana\my documents\download\code projects\xml parser\source\interfacewrapper.h(330) : error C2491: 'CInterfaceCallingWrapper<T>::operator =' : definition of dllimport function not allowed
1>c:\documents and settings\lofana\my documents\download\code projects\xml parser\source\interfacewrapper.h(337) : error C2491: 'CInterfaceCallingWrapper<T>::operator !' : definition of dllimport function not allowed
1>c:\documents and settings\lofana\my documents\download\code projects\xml parser\source\interfacewrapper.h(357) : error C2491: 'CInterfaceCallingWrapper<T>::Attach' : definition of dllimport function not allowed
1>c:\documents and settings\lofana\my documents\download\code projects\xml parser\source\interfacewrapper.h(367) : error C2491: 'CInterfaceCallingWrapper<T>::Detach' : definition of dllimport function not allowed
1>c:\documents and settings\lofana\my documents\download\code projects\xml parser\source\interfacewrapper.h(378) : error C2491: 'CInterfaceCallingWrapper<T>::CopyTo' : definition of dllimport function not allowed
1>c:\documents and settings\lofana\my documents\download\code projects\xml parser\source\interfacewrapper.h(385) : error C2491: 'CInterfaceCallingWrapper<T>::CoCreateInstance' : definition of dllimport function not allowed
1>c:\documents and settings\lofana\my documents\download\code projects\xml parser\source\interfacewrapper.h(395) : error C2491: 'CInterfaceCallingWrapper<T>::CoCreateInstance' : definition of dllimport function not allowed
1>c:\documents and settings\lofana\my documents\download\code projects\xml parser\source\interfacewrapper.h(402) : error C2491: 'CInterfaceCallingWrapper<T>::P' : definition of dllimport function not allowed
1>c:\documents and settings\lofana\my documents\download\code projects\xml parser\source\interfacewrapper.h(409) : error C2491: 'CInterfaceCallingWrapper<T>::GetP' : definition of dllimport function not allowed
Hm, are you sure you added all files from the sample to your project? (yes, stupid Q, yet I have to ask it)?
I download the http://www.codeproject.com/KB/cpp/msxmlcpp.aspx (source and Demo) and I compile the DOMXMLDemo project and got the above errors.  Any idea?
What compiler are you using? I just tried the same vith VS 2005 and got


1>------ Build started: Project: MSXMLCPP, Configuration: Debug Win32 ------
1>Compiling...
1>StdAfx.cpp
1> WINVER not defined. Defaulting to 0x0502 (Windows Server 2003)
1>Compiling...
1>DOMXML.cpp
1>InterfaceWrapper.cpp
1>MSXMLCPP.cpp
1>SAXXML.cpp
1>Generating Code...
1>Compiling resources...
1>Compiling manifest to resources...
1>Linking...
1>.\MSXMLCPPD.def(17) : warning LNK4017: DESCRIPTION statement not supported for the target platform; ignored
1>   Creating library .\../Output/msxmlcppD.lib and object .\../Output/msxmlcppD.exp
1>Embedding manifest...
1>Build log was saved at "file://e:\tmp\MSXMLWrap\Source\Debug\BuildLog.htm"
1>MSXMLCPP - 0 error(s), 1 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Open in new window

I am using Visual 2005.

I don't really have to get this compile.  What I need is to get my original questions to be answer.

DO you have a simple codes that will help me better?

So far, I am able to write codes to load data from an xml file.

But I want to add a node, remove a node, and write the xmldoc structure back to the file.

Here is my codes:
MSXML2::IXMLDOMDocumentPtr pXMLDoc  = NULL;
HRESULT hr = pXMLDoc .CreateInstance(__uuidof(MSXML2::DOMDocument40));
VARIANT_BOOL bSuccess=false;
 
CComVariant         varFile;
varFile = "MessageService.xml";
hr = pXMLDoc->load(varFile);

MSXML2::IXMLDOMElementPtr pXMLDocElement = NULL;
 pXMLDocElement = pXMLDoc->documentElement;
 MSXML2::IXMLDOMNodeListPtr pXMLDomNodeList = NULL;
 pXMLDomNodeList = pXMLDocElement->selectNodes("//MessageService/Users/User");

for (int i = 0; i < pXMLDomNodeList->length; i++)
{
     pXMLDomNodeList->get_item(i,&node);
     node->get_nodeType(&type);
     if (type!=NODE_ELEMENT) return false;
     CComQIPtr<IXMLDOMElement> e(node);
     node->get_nodeName(&bn);
     CComVariant val(VT_EMPTY);
      _variant_t varValue;
     e->getAttribute(L"name",&varValue);
     string s = _bstr_t(varValue);
     cout << "login id is " << s.c_str() << endl;
}

//How to access child nodes? (For example, each user has a list of messages)
//How to remove a child nodes? (Can I remove //MessageService/Users/User name=johnsmith/msd id=1?)

and how to write this back into a file?



Considering the XML file as below:

<?xml version="1.0"?>
<MessageService desc="Self Learning MSXML">
      <Users>
    <User name="johnsmith" password="12345678">         
                <Msg id="1">
                      <Data>How are you doing?</Data>
                      <Sender name="bibi"/>
                </Msg>
    </User>
    <User name="kevinking" password="23456785">                    
                <Msg id="1">
                      <Data>I am doing fine!</Data>
                      <Sender name="johnsmith"/>
                </Msg>
                <Msg id="2">
                      <Data>I am a newbie.</Data>
                      <Sender name="johnsmith"/>
                </Msg>         
    </User>
  </Users>  
</MessageService>

 
BTW - I can compile the MSXMLCPP, but not the DOMXMLDemo project.
JKR -

Are you able to compile the DOMXMLDemo project?
JKR -
Are you still working on this question?  Can you compile the DOMXMLDemo project?
Did you ever get this to work? I am having similar issues using eVC++ 4.0 and MFC. Difficult to find any working examples.