Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using CMarkup class for creating the xml file in visual studio 2010 using vc++.

I am facing a problem in xml. After creation of xml file i can add the details in the xml file and later i appended the xml file but i cant view the tags in the xml file whereas i can view them in the notepad.

CMarkup xmlWrite;

C++
sPath = L"D:\Login.xml";
xmlWrite.Load(sPath); 
int nItem;
 xmlWrite.Open(sPath, xmlWrite.MDF_APPENDFILE); 
xmlWrite.AddElem(L"Records"); xmlWrite.IntoElem(); 
xmlWrite.AddElem(L"Login"); xmlWrite.IntoElem(); 
xmlWrite.AddElem(L"LoginID", getLoginstr); 
xmlWrite.AddElem(L"Password", getEncrypted); 
xmlWrite.AddElem( L"DateTime", getDateTime/*(LPCTSTR)CTime::GetCurrentTime().Format("%d-%b-%Y %H:%M:%S")*/ ); 
strXML = xmlWrite.GetDoc(); xmlWrite.SetDoc(strXML); 
struct SampItem 
{ 
LPCTSTR szLogin; LPCTSTR szPassword; 
LPCTSTR szDateTime; 
} 
aItems[] = { getLoginstr, getEncrypted, getDateTime }; 
//xmlWrite.SetDoc(NULL);
//xmlWrite.AddElem(_T("Records")); 
//xmlWrite.IntoElem(); 
for (nItem = 0; aItems[nItem].szLogin; ++nItem) 
{ 
xmlWrite.AddChildElem( _T("Login") );
 xmlWrite.IntoElem();
 xmlWrite.AddChildElem( _T("LoginID"), aItems[nItem].szLogin ); 
xmlWrite.AddChildElem( _T("Password"), aItems[nItem].szPassword ); 
xmlWrite.AddChildElem( _T("DateTime"), aItems[nItem].szDateTime ); 
xmlWrite.OutOfElem(); break; //xmlWrite.Save(sPath);
 } 
xmlWrite.Save(sPath); xmlWrite.Close();


if anyone having idea plz reply to my question.

waiting for soon reply.

thanks in advance..
Posted
Updated 22-Jul-12 15:55pm
v2
Comments
Kenneth Haugland 22-Jul-12 21:36pm    
need code snipplet to answer that.
Kumar 09 22-Jul-12 21:48pm    
the following is the code:

CMarkup xmlWrite;
sPath = L"D:\Login.xml";
xmlWrite.Load(sPath);
int nItem;

xmlWrite.Open(sPath, xmlWrite.MDF_APPENDFILE);
xmlWrite.AddElem(L"Records");
xmlWrite.IntoElem();
xmlWrite.AddElem(L"Login");
xmlWrite.IntoElem();
xmlWrite.AddElem(L"LoginID", getLoginstr);
xmlWrite.AddElem(L"Password", getEncrypted);
xmlWrite.AddElem( L"DateTime", getDateTime/*(LPCTSTR)CTime::GetCurrentTime().Format("%d-%b-%Y %H:%M:%S")*/ );
strXML = xmlWrite.GetDoc();

xmlWrite.SetDoc(strXML);

struct SampItem { LPCTSTR szLogin; LPCTSTR szPassword; LPCTSTR szDateTime; } aItems[] =
{
getLoginstr, getEncrypted, getDateTime
};

//xmlWrite.SetDoc(NULL);
//xmlWrite.AddElem(_T("Records"));
//xmlWrite.IntoElem();
for (nItem = 0; aItems[nItem].szLogin; ++nItem)
{
xmlWrite.AddChildElem( _T("Login") );
xmlWrite.IntoElem();
xmlWrite.AddChildElem( _T("LoginID"), aItems[nItem].szLogin );
xmlWrite.AddChildElem( _T("Password"), aItems[nItem].szPassword );
xmlWrite.AddChildElem( _T("DateTime"), aItems[nItem].szDateTime );
xmlWrite.OutOfElem();
break;

//xmlWrite.Save(sPath);
}

xmlWrite.Save(sPath);
xmlWrite.Close();

this is the code which i wrote.
Kumar 09 22-Jul-12 22:12pm    
did u got any solution
Kenneth Haugland 22-Jul-12 22:21pm    
http://www.firstobject.com/dn_markAddNode.htm
or
http://www.firstobject.com/dn_marknodes.htm
?
Kenneth Haugland 22-Jul-12 22:18pm    
I dont.... I have used xml before but in C# not C++, This looks a little forign to me....

1 solution

I have used CMarkup a lot in past projects. It has some great features and it also allows you to create documents without a single root element (can be used for log files where you just append entries to the end of the file), but this is not well-formed XML and other tools will typically fail to load it.

I will assume your goal is to create XML with a "Records" root element something like this:
XML
<records>
  <login>
  ....
  </login>
  <login>
  ....
  </login>
  <login>
  ....
  </login>
</records>

I do not believe you will be able to load the document in append mode to do this, but CMarkup is pretty fast, so you can load the document and add new Login elements to the end of the existing Login elements without worrying too much about the performance.

To use CMarkup for maintaining an XML document laid out as I have shown, I have modified your code and removed some of the test ing you had.
CMarkup xmlWrite;
sPath = "D:\Login.xml";
    
xmlWrite.Load(sPath);

// If the root element "Records" does not exist, create it.
if (!xmlWrite.FindElem( _T("Records") ))
{
    xmlWrite.AddElem( _T("Records") );
}

// Navigate into the root element.
if (xmlWrite.IntoElem())
{
    // AddElem() adds a new element at the end of the existing elements.
    if (xmlWrite.AddElem( _T("Login") ) && xmlWrite.IntoElem())
    {
        xmlWrite.AddElem( _T("LoginID"), getLoginstr);
        xmlWrite.AddElem( _T("Password"), getEncrypted);            
        xmlWrite.AddElem( _T("DateTime"), getDateTime);
        xmlWrite.OutOfElem();
    }

    // Since the file will be closed, you don't really have to navigate out
    // of the root here, but it is just good practice to always have a
    // matching IntoElem()/OutOfElem() pair.
    xmlWrite.OutOfElem();
}

xmlWrite.Save();


To retrieve stored values for a specific Login, you open the file, find the root element and navigate into it. Then you just use a while loop to go through all the Login elements. For every login element, you retrieve the value of the LoginID child element and compare it to the one you are looking for and once you find it, you retrieve the values of the Password and DateTime child elements.

I put this function together to show you how simple it is, but I did not test if it compiles or runs - it should be close to working.
bool GetLogin(LPCTSTR szFileName, 
              LPCTSTR szId,
              CString& strPassword,
              CString& strDateTime)
{
    bool bReturn = false;
    CMarkup markup;

    // Load the file.
    if (markup.Load( szFileName ))
    {
        // Find the root element and navigate into it.
        if (markup.FindElem( _T("Records") ) && markup.IntoElem())
        {
            // Go through all the elements until we find the right one.
            while (markup.FindElem( _T("Login") ))
            {
                // Read the ID of this Login.
                if (markup.FindChildElem( _T("LoginID") ))
                {
                    CString strId = markup.GetChildData();
					
                    // Check if it is the one we are looking for.
                    if (strId.CompareNoCase( szId ) == 0)
                    {
                        // Read the password element.
                        if (markup.FindChildElem( _T("Password") ))
                        {
                            strPassword = markup.GetChildData();
                        }

                        // Read the date/time element.
                        if (markup.FindChildElem( _T("DateTime") ))
                        {
                            strDateTime = markup.GetChildData();
                        }

                        // Stop going through elements and return "true".
                        bReturn = true;
                        break;
                    }
                }
            }

            markup.OutOfElem();
        }
    }

    return bReturn;
}



Soren Madsen
 
Share this answer
 
v3
Comments
Kumar 09 23-Jul-12 6:16am    
thank u for the suggestion but at one go it displays two values but it should not display like that, if i stored like this for first time
records
login
....
/login

/records
next time i want to append values like <login> and <Password> at different intervals.

as i am working on dialog based applications i am creating user login data in xml file. i want to append data to xml file after first login elements ends...
SoMad 23-Jul-12 16:49pm    
I expected you to say you were getting duplicate entries, that is what I meant when I mentioned it looked like you had tried different approaches. Just remove the second part of the code that adds elements in the loop.

CMarkup xmlWrite;
sPath = L"D:\Login.xml";

xmlWrite.Load(sPath);

// If the root element "Records" does not exist, create it.
if (!xmlWrite.FindElem(L"Records"))
{
xmlWrite.AddElem(L"Records");
}

// Navigate into the root element.
if (xmlWrite.IntoElem())
{
// AddElem() adds a new element at the end of the existing elements.
if (xmlWrite.AddElem(L"Login") && xmlWrite.IntoElem())
{
xmlWrite.AddElem(L"LoginID", getLoginstr);
xmlWrite.AddElem(L"Password", getEncrypted);
xmlWrite.AddElem(L"DateTime", getDateTime);
xmlWrite.OutOfElem();
}

xmlWrite.OutOfElem();
}

xmlWrite.Save();

Soren Madsen
Kumar 09 23-Jul-12 22:51pm    
hi soren,

i got the solution thank u.
Kumar 09 24-Jul-12 4:20am    
I want to find the elements and its containing data from the xml file.

Actually i added more than 5 login and password details in the xml file and i want to search whether the login name is exists in the xml file or not and retrieve the data from xml file.

any help regarding this xml using CMarkup.
SoMad 24-Jul-12 5:40am    
Sure. Just like when you are adding elements, you open the file, find the root element and navigate into it. Then you just use a while loop to go through all the Login elements. For every login element, you retrieve the value of the LoginID child element and compare it to the one you are looking for and once you find it, you retrieve the values of the Password and DateTime child elements.

I put a little function together and will add it to the solution. I did not test if the function works, but it should be close to working.
Feel free to also up-vote my solution if this is what you are looking for :)

Soren Madsen

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900