Solved

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

Posted on 2004-08-21
9
509 Views
Last Modified: 2011-09-20
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
Comment
Question by:codelearner
9 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11864512
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11864544
0
 

Author Comment

by:codelearner
ID: 11874087
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 100 total points
ID: 11874213
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
 

Author Comment

by:codelearner
ID: 11874411
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11875042
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
 
LVL 3

Assisted Solution

by:ruff_ryder
ruff_ryder earned 50 total points
ID: 11877130
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
 
LVL 19

Accepted Solution

by:
drichards earned 150 total points
ID: 11878131
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
 

Author Comment

by:codelearner
ID: 11884049
Hi drichards,
Let me try both of your examples and let you know.

Thanks,
codelearner
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question