Home  >  Article  >  Backend Development  >  Detailed introduction to CMarkup class operation Xml summary

Detailed introduction to CMarkup class operation Xml summary

黄舟
黄舟Original
2017-03-16 17:01:003604browse

一、下载Markup.cpp 和 Markup.h

二、将此两个文件放置于工程目录下

三、在需要用到CMarkup的地方 #<a href="http://www.php.cn/wiki/137.html" target="_blank">include</a> "Markup.h" 

当然VC中还需要配置一下环境

在VC6.0下:

a.在Markup.cpp的顶端加上 #include"stdafx.h" 或者

b.关闭Markup.cpp的预编译头设置,具体方法如下:

  Project->Setting  弹出ProjectSetting对话框,在左边的文件树下选择Markup.cpp,然后在 "Settings for" 下拉框下选择 "All Configurations",选择 C/C++标签页,

接着在Category下拉框下选中 "Precompiled Headers"选项,选中下面的"Not Using Precompiled Headers." 单选按钮即可

CMarkup的基本使用

例如要读取如下UserInfos.xml的内容

<?xml version="1.0" encoding="UTF-8" ?>   
 <UserInfos>  
  <UserInfo>  
   <name>WangYao</name>   
   <age>25</age>   
  </UserInfo>  
  <UserInfo>  
   <name>Hisin</name>   
   <age>27</age>   
  </UserInfo>  
 </UserInfos>

源代码如下:

CMarkup xml;  
bool    flag;  
  
//加载Xml文件  
flag = xml.Load("d:\\UserInfos.xml");  
if (!flag)  
{  
    AfxMessageBox(TEXT("加载d:\\UserInfos.xml失败,请检查"));  
    return;  
}  
  
//定位到Root Elem  
xml.ResetPos();  
flag = xml.FindElem("UserInfos");  //Root Elem为<UserInfos></UserInfos>  
if (!flag)  
{  
    return;  
}  
  
xml.IntoElem();   //进入根节点  
while(xml.FindElem(TEXT("UserInfo")))  
{  
    xml.IntoElem();   //进入<UserInfo></UserInfo>  
  
    //获取name节点数据  
    flag = xml.FindElem(TEXT("name"));  
    if (flag)  
    {  
        CString cstrName;  
        cstrName = xml.GetData();  
        AfxMessageBox(cstrName);  
    }  
  
    //获取age节点数据  
    xml.ResetMainPos();   //保证不管name节点和age节点的顺序如何,都能找到age节点  
    flag = xml.FindElem(TEXT("age"));  
    if (flag)  
    {  
        CString cstrAge;  
        cstrAge = xml.GetData();  
        AfxMessageBox(cstrAge);  
    }  
  
    xml.OutOfElem();  //跳出<UserInfo></UserInfo>  
}  
xml.OutOfElem();  //跳出根节点

当然实现同样的功能也可以不进入UserInfo节点,源码如下,请仔细对比

xml.IntoElem();   //进入根节点  
while(xml.FindElem(TEXT("UserInfo")))  
{  
    //获取name子节点数据  
    flag = xml.FindChildElem(TEXT("name"));  
    if (flag)  
    {  
        CString cstrName;  
        cstrName = xml.GetChildData();  
        AfxMessageBox(cstrName);  
    }  
  
    //获取age子节点数据  
    xml.ResetChildPos();   //保证不管name子节点和age子节点的顺序如何,都能找到age子节点  
    flag = xml.FindChildElem(TEXT("age"));  
    if (flag)  
    {  
        CString cstrAge;  
        cstrAge = xml.GetChildData();  
        AfxMessageBox(cstrAge);  
    }  
}  
xml.OutOfElem();  //跳出根节点

Tips:<br>

1.IntoElem与OutOfElem方法应成对使用

2.关于重置xml的Pos的函数

ResetPos Resets the current position to the start of the document
ResetMainPos Resets the current main position to before the first sibling
ResetChildPos Resets the current child position to before the first child

 3.SavePos 与 RestorePos 复原xml Pos

SavePos Saves the current position with an optional string name using a hash map
RestorePos Goes to the position saved with SavePos<br>

比如:

...
xml.SavePos(Text("abc"));
OpXml(xml);  //该函数可能会改变xml的Pos,则可以利用SavePos与RestorePos复原该函数执行前xml的Pos
xml.RestorePos(Text("abc"));
...

The above is the detailed content of Detailed introduction to CMarkup class operation Xml summary. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn