0

I am trying to read some XML code from a website, and am having a bit of trouble figuring out where my errors are. Using the code from this extremely useful post, I am trying to read a file that I have saved to my desktop ("H:\MyName\Desktop\secLendingXML.cfm.xml"). The code is below:

#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#import <msxml6.dll> rename_namespace(_T("MSXML"))

int main(/*int argc, char* argv[]*/)
{
HRESULT hr = CoInitialize(NULL); 
if (SUCCEEDED(hr))
{
    try
    {
        MSXML::IXMLDOMDocument2Ptr xmlDoc;
        hr = xmlDoc.CreateInstance(__uuidof(MSXML::DOMDocument60), NULL, CLSCTX_INPROC_SERVER);
        // TODO: if (FAILED(hr))...
        if (xmlDoc->load(_T("H:\MyName\Desktop\secLendingXML.cfm.xml")) != VARIANT_TRUE)
        {
            printf("Unable to load input.xml\n");
        }
        else
        {
            printf("XML was successfully loaded\n");

            xmlDoc->setProperty("SelectionLanguage", "XPath");
            MSXML::IXMLDOMNodeListPtr wheels = xmlDoc->selectNodes("/Car/Wheels/*");
            printf("Car has %u wheels\n", wheels->Getlength());

            MSXML::IXMLDOMNodePtr node;
            node = xmlDoc->createNode(MSXML::NODE_ELEMENT, _T("Engine"), _T(""));
            node->text = _T("Engine 1.0");
            xmlDoc->documentElement->appendChild(node);
            hr = xmlDoc->save(_T("output.xml"));
            if (SUCCEEDED(hr))
                printf("output.xml successfully saved\n");
        }
    }
    catch (_com_error &e)
    {
        printf("ERROR: %ws\n", e.ErrorMessage());
    }
    CoUninitialize();
}
system("PAUSE");
return 0;

}

The message "Unable to load input.xml" always shows, so I know I'm not having an error, but that the code is unable to load my XML file.

Do I need to save the XML file in a different location? Is the ".cfm" before the ".xml" screwing with the reading process?

To give an idea of my intended direction, I want to be able to load the XML file from the New York Fed website and read it into some sort of data file where I can automate the data retrieving process. That way, whenever the website is updated, I will automatically be notified, and it will be reflected by the change in the data file that I will have saved somewhere on my computer. If anyone also wants to help with how I go about that part - poll intervals, tracking website changes, etc. - that would also be appreciated.

Thank you for any and all help.

Community
  • 1
  • 1
weskpga
  • 2,017
  • 7
  • 29
  • 43

1 Answers1

1

Do you need to escape the backslashes in your path string? e.g.

xmlDoc->load(_T("H:\\MyName\\Desktop\\secLendingXML.cfm.xml")
Neutrino
  • 8,496
  • 4
  • 57
  • 83
  • Nailed it! Thanks, I totally spaced on that one. I'll leave it open in case anyone has some advice for the second part of my question, but I'll accept your answer once I'm allowed. – weskpga Jun 14 '12 at 15:51
  • do you think you could take a look at the XML file from the NY Fed. I can't seem to get it into a neat dataset. – weskpga Jun 14 '12 at 19:19