Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 520
  • Last Modified:

Need help how to generate xml file by using VC++.NET

Can anyone help me how to write the format below and save it as "myCatalog.xml> by using VC++.NET. I would like a working sample code because I spent lot of time to read in MSDN but I still got stuck.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!--  Edited with XML Spy v4.2  -->
<CATALOG>
  <CD>
     <TITLE>Empire Burlesque</TITLE>
     <ARTIST>Bob Dylan</ARTIST>
     <COUNTRY>USA</COUNTRY>
     <COMPANY>Columbia</COMPANY>
     <PRICE>10.90</PRICE>
     <YEAR>1985</YEAR>
  </CD>
  <CD>
     <TITLE>Hide your heart</TITLE>
     <ARTIST>Bonnie Tyler</ARTIST>
     <COUNTRY>UK</COUNTRY>
     <COMPANY>CBS Records</COMPANY>
     <PRICE>9.90</PRICE>
     <YEAR>1988</YEAR>
  </CD>
</CATALOG>

Thank you very much.
codelearner
0
codelearner
Asked:
codelearner
3 Solutions
 
Jaime OlivaresSoftware ArchitectCommented:
An XML file is just a text file, no science involved, that's why you don't find nothing in MSDN.
As you know what are you records, your field names, and the structure of them, just use CFile and a String object to generate it.
0
 
codelearnerAuthor Commented:
Actually, I expect some example codes like:

int main(int argc, char* argv[])
{
  createRoot <CATALOG>
  createNode <CD>
  createChildNode <Title>
  addTextForChildNode("Empire Burlesque")
  EndNode <CD>
  End <CATALOG>
}

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Jaime OlivaresSoftware ArchitectCommented:
If you don't use a library like one of mentioned, you can't have a code like your sample. But as I told you, you can write a simple text file, something like (maybe some bug):

int main(int argc, char *argv[])
{
     char *title = "Empire Burlesque";

      FILE *fp = fopen("test.xml");
      fputs("<CATALOG>", fp);
      fputs("    <CD>", fp);
      fprintf(fp, "        <TITLE>%s</TITLE>\n\r", title);
      fputs("    </CD>", fp);
      fputs("</CATALOG>", fp);
      fclose (fp);
}
0
 
codelearnerAuthor Commented:
I went to the link above, which is http://www.codeproject.com/cpp/xmlmanager.asp, downloaded and followed its instruction. However, it didn't compile. I understand what you said. But it would be helpful if you can give me a working code using MSXML and that would be great for me to start from there, and I will increase the point.
0
 
Jaime OlivaresSoftware ArchitectCommented:
Well, a C++ example of MSXML doesn't look as simple as your sample. Take a look to:
http://www.endurasoft.com/vcd/msxml1.htm
0
 
ruff_ryderCommented:
If you are using the .NET library from Visual C++, have a look at the System.Xml namespace in the MSDN docs. There is a XmlDocument class there with accompanying classes that will allow you to create your required xml structure in code. Give it a try and if you need more help, just drop a note ;)
0
 
drichardsCommented:
If you're doing unmanaged C++, you can use MSXML (COM-based):

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/xmobjxmldomdocument2.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/xmmscxmldomobjects.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/dom_reference.asp

Here's some sample code to give you the flavor.  You need to #include objbase.h, comdef.h and msxml2.h and link against msxml2.lib for this to work.
-------------------------------------------------------------------
//
// Helper routine to add a child element to an existing element
//
IXMLDOMElement* AppendChild(IXMLDOMDocument2 *pDoc, IXMLDOMElement *pParent, OLECHAR *name, OLECHAR *value)
{
    IXMLDOMElement *pElement = NULL;
    _bstr_t tagName(name);

    // Normally you would check the HRESULT's.
    HRESULT hr = pDoc->createElement(tagName, &pElement);
    if ( value != NULL )
    {
        _bstr_t text(value);
        hr = pElement->put_text(text);
    }
    hr = pParent->appendChild(pElement, NULL);
    return pElement;
}

//
// Routine to build your sample XML
//
void DoXML()
{
    HRESULT hr = ::CoInitialize(NULL);
    IXMLDOMDocument2 *pDoc = NULL;
    hr = ::CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument2, (void**)&pDoc);
    _bstr_t docElem(OLESTR("<?xml version='1.0' encoding='ISO-8859-1' ?>\n<CATALOG/>"));
    VARIANT_BOOL success = VARIANT_TRUE;
    hr = pDoc->loadXML(docElem, &success);
    IXMLDOMElement *pDocRoot = NULL;
    hr = pDoc->get_documentElement(&pDocRoot);

    IXMLDOMElement *pElement = AppendChild(pDoc, pDocRoot, OLESTR("CD"), NULL);
    IXMLDOMElement *pNew = AppendChild(pDoc, pElement, OLESTR("TITLE"), OLESTR("Empire Burlesque"));
    pNew->Release();
    pNew = AppendChild(pDoc, pElement, OLESTR("ARTIST"), OLESTR("Bob Dylan"));
    pNew->Release();
    pNew = AppendChild(pDoc, pElement, OLESTR("COUNTRY"), OLESTR("USA"));
    pNew->Release();
    pNew = AppendChild(pDoc, pElement, OLESTR("COMPANY"), OLESTR("Columbia"));
    pNew->Release();
    pNew = AppendChild(pDoc, pElement, OLESTR("PRICE"), OLESTR("10.90"));
    pNew->Release();
    pNew = AppendChild(pDoc, pElement, OLESTR("YEAR"), OLESTR("1985"));
    pNew->Release();
    pElement->Release();
    pElement = AppendChild(pDoc, pDocRoot, OLESTR("CD"), NULL);
    pNew = AppendChild(pDoc, pElement, OLESTR("TITLE"), OLESTR("Hide Your Heart"));
    pNew->Release();
    pNew = AppendChild(pDoc, pElement, OLESTR("ARTIST"), OLESTR("Bonnie Tyler"));
    pNew->Release();
    pNew = AppendChild(pDoc, pElement, OLESTR("COUNTRY"), OLESTR("UK"));
    pNew->Release();
    pNew = AppendChild(pDoc, pElement, OLESTR("COMPANY"), OLESTR("CBS Records"));
    pNew->Release();
    pNew = AppendChild(pDoc, pElement, OLESTR("PRICE"), OLESTR("9.90"));
    pNew->Release();
    pNew = AppendChild(pDoc, pElement, OLESTR("YEAR"), OLESTR("1988"));
    pNew->Release();
    pElement->Release();

    pDocRoot->Release();
    _variant_t v(OLESTR("C:\\myCatalog_1.xml"));
    hr = pDoc->save(v);
    pDoc->Release();

    ::CoUninitialize();
}
-------------------------------------------------

If you're doing managed C++, you can use the .NET XML classes:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxmldocumentclasstopic.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxml.asp

The sample code for .NET classes is a bit ore compact since you don't need all the COM management stuff (Release() and HRESULT's).
----------------------------------------
//
// Helper method for adding child element to existing element
//
XmlNode* XMLMaker::AppendChild(XmlDocument *pDoc, XmlNode *pParent, String *tagName, String *value)
{
    XmlNode *pResult = pDoc->CreateElement(tagName);
    if ( value != NULL )
    {
        pResult->InnerText = value;
    }
    pParent->AppendChild(pResult);
    return pResult;
}

//
// Main routine to create your sample XML
//
void XMLMaker::MakeDoc()
{
    XmlDocument *pDoc = new XmlDocument();
    pDoc->LoadXml(S"<?xml version='1.0' encoding='ISO-8859-1' ?>\n<CATALOG/>");
    XmlElement *pDocRoot = pDoc->DocumentElement;
    XmlNode *pNode = AppendChild(pDoc, pDocRoot, S"CD", NULL);
    AppendChild(pDoc, pNode, S"TITLE", S"Empire Burlesque");
    AppendChild(pDoc, pNode, S"ARTIST", S"Bob Dylan");
    AppendChild(pDoc, pNode, S"COUNTRY", S"USA");
    AppendChild(pDoc, pNode, S"COMPANY", S"Columbia");
    AppendChild(pDoc, pNode, S"PRICE", S"10.90");
    AppendChild(pDoc, pNode, S"YEAR", S"1985");
    pNode = AppendChild(pDoc, pDocRoot, S"CD", NULL);
    AppendChild(pDoc, pNode, S"TITLE", S"Hide Your Heart");
    AppendChild(pDoc, pNode, S"ARTIST", S"Bonnie Tyler");
    AppendChild(pDoc, pNode, S"COUNTRY", S"UK");
    AppendChild(pDoc, pNode, S"COMPANY", S"CBS Records");
    AppendChild(pDoc, pNode, S"PRICE", S"9.90");
    AppendChild(pDoc, pNode, S"YEAR", S"1988");

    pDoc->Save(S"C:\\myCatalog_2.xml");
}

0
 
codelearnerAuthor Commented:
Hi drichards,
Let me try both of your examples and let you know.

Thanks,
codelearner
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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