CMarkup XML树无法显示 [英] CMarkup XML Tree unable to display

查看:57
本文介绍了CMarkup XML树无法显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用CMarkup类使用vc ++在Visual Studio 2010中创建xml文件.

我在xml中遇到问题.创建xml文件后,我可以在xml文件中添加详细信息,稍后再附加xml文件,但无法在xml文件中查看标签,而可以在记事本中查看它们.

CMarkup xmlWrite;

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;

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..

推荐答案

我在过去的项目中经常使用CMarkup.它具有一些很棒的功能,它还允许您创建没有单个根元素的文档(可用于日志文件,只需在其中将条目追加到文件末尾即可),但这不是格式正确的XML,其他工具也会通常无法加载.

我将假设您的目标是使用"Records"根元素创建XML,如下所示:
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:
<records>
  <login>
  ....
  </login>
  <login>
  ....
  </login>
  <login>
  ....
  </login>
</records>


我不相信您可以在附加模式下加载文档来做到这一点,但是CMarkup相当快,因此您可以加载文档并将新的Login元素添加到现有Login元素的末尾,而不必太担心性能.

要使用CMarkup维护我所显示的布局的XML文档,我已经修改了您的代码并删除了一些测试.


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();



要检索特定登录名的存储值,请打开文件,找到根元素并导航到其中.然后,您只需要使用while循环来遍历所有Login元素.对于每个登录元素,都将检索LoginID子元素的值,并将其与所需的值进行比较,找到后,便会检索Password和DateTime子元素的值.

我将这个函数放在一起以向您展示它是多么简单,但是我没有测试它是否可以编译或运行-它应该接近工作了.



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




Soren Madsen


这篇关于CMarkup XML树无法显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆