CMarkUp读写XML(转)
Fast start to XML in C++
Enough bull. You want to create XML or read and find things in XML. All you need to know about CMarkup is that it is just one object per XML document (for the API design concept see EDOM). And by the way the free firstobject XML Editor generates C++ source code for creating and navigating your own XML documents with CMarkup.
Creating an XML Document
To create an XML document, instantiate a CMarkup object and call AddElem to create the root element. At this point your document would simply contain the empty root element e.g. <ORDER/>. Then call IntoElem to go "inside" the ORDER element so that you can create child elements under the root element (i.e. the root element will be the "container" of the child elements).
The following example code creates an XML document.
CMarkup xml;
xml.AddElem( "ORDER" );
xml.IntoElem();
xml.AddElem( "ITEM" );
xml.IntoElem();
xml.AddElem( "SN", "132487A-J" );
xml.AddElem( "NAME", "crank casing" );
xml.AddElem( "QTY", "" );
This code generates the following XML. The root is the ORDER element; notice that its start tag<ORDER>is at the beginning and end tag</ORDER>is at the bottom. When an element is under (i.e. inside or contained by) a parent element, the parent's start tag is before it and the parent's end tag is after it. The ORDER element contains one ITEM element. That ITEM element contains 3 child elements: SN, NAME, and QTY.
<ORDER><ITEM><SN>132487A-J</SN><NAME>crank casing</NAME><QTY>1</QTY></ITEM></ORDER>
As shown in the example, you create elements under an element by calling IntoElem to make your current main position (or "place holder") into your current parent position so you can begin adding child elements. CMarkup maintains a current position in order to keep your source code shorter and simpler. This same position logic is used when navigating a document.
You can write the above document to file with Save:
xml.Save( "C:\\Sample.xml" );
And you can retrieve the XML into a string with GetDoc:
MCD_STR strXML = xml.GetDoc();
Markup.h defines MCD_STR to the string type you compile CMarkup for, so we use MCD_STR in these examples, but you can use your own string type explicitly (e.g. std::string or CString).
Navigating an XML Document
You can navigate the data right inside the same CMarkup object you created in the example above; just call ResetPos if you want to go back to the beginning of the document. Or you can populate a new CMarkup object:
CMarkup xml;
From a file with Load:
xml.Load( "C:\\Sample.xml" );
Or from an XML string with SetDoc:
xml.SetDoc( strXML );
In the following example, we go inside the root ORDER element and loop through all ITEM elements with FindElem to get the serial number and quantity of each with GetData. The serial number is treated as a string and the quantity is converted to an integer using atoi (MCD_2PCSZ is defined in Markup.h to return the string's const pointer).
xml.FindElem(); // root ORDER element
xml.IntoElem(); // inside ORDER
while ( xml.FindElem("ITEM") )
{
xml.IntoElem();
xml.FindElem( "SN" );
MCD_STR strSN = xml.GetData();
xml.FindElem( "QTY" );
int nQty = atoi( MCD_2PCSZ(xml.GetData()) );
xml.OutOfElem();
}
For each item we find, we call IntoElem to interrogate its child elements, and then OutOfElemafterwards. As you get accustomed to this type of navigation you will know to check in your loops to make sure there is a corresponding OutOfElem call for every IntoElem call.
Adding Elements and Attributes
The above example for creating a document only created one ITEM element. Here is an example that creates multiple items loaded from a previously populated data source, plus a SHIPMENT information element in which one of the elements has an attribute we set with SetAttrib.
CMarkup xml;
xml.AddElem( "ORDER" );
xml.IntoElem(); // inside ORDER
for ( int nItem=; nItem<aItems.GetSize(); ++nItem )
{
xml.AddElem( "ITEM" );
xml.IntoElem(); // inside ITEM
xml.AddElem( "SN", aItems[nItem].strSN );
xml.AddElem( "NAME", aItems[nItem].strName );
xml.AddElem( "QTY", aItems[nItem].nQty );
xml.OutOfElem(); // back out to ITEM level
}
xml.AddElem( "SHIPMENT" );
xml.IntoElem(); // inside SHIPMENT
xml.AddElem( "POC" );
xml.SetAttrib( "type", strPOCType );
xml.IntoElem(); // inside POC
xml.AddElem( "NAME", strPOCName );
xml.AddElem( "TEL", strPOCTel );
This code generates the following XML. The root ORDER element contains 2 ITEM elements and a SHIPMENT element. The ITEM elements both contain SN, NAME and QTY elements. The SHIPMENT element contains a POC element which has a type attribute, and NAME and TEL child elements.
<ORDER><ITEM><SN>132487A-J</SN><NAME>crank casing</NAME><QTY>1</QTY></ITEM><ITEM><SN>4238764-A</SN><NAME>bearing</NAME><QTY>15</QTY></ITEM><SHIPMENT><POC type="non-emergency"><NAME>John Smith</NAME><TEL>555-1234</TEL></POC></SHIPMENT></ORDER>
Finding Elements
The FindElem method goes to the next sibling element. If the optional tag name argument is specified, then it goes to the next element with a matching tag name. The element that is found becomes the current element, and the next call to FindElem will go to the next sibling or matching sibling after that current position.
When you cannot assume the order of the elements, you must move the position back before the first sibling with ResetMainPos in between your calls to the FindElem method. Looking at the ITEM element in the above example, if someone else is creating the XML and you cannot assume the SN element is before the QTY element, then call ResetMainPos before finding the QTY element.
{
xml.IntoElem();
xml.FindElem( "SN" );
MCD_STR strSN = xml.GetData();
xml.ResetMainPos();
xml.FindElem( "QTY" );
int nQty = atoi( MCD_2PCSZ(xml.GetData()) );
xml.OutOfElem();
}
To find the item with a particular serial number, you can loop through the ITEM elements and compare the SN element data to the serial number you are searching for. By specifying the "ITEM" element tag name in the FindElem method we ignore all other sibling elements such as the SHIPMENT element. Also, instead of going into and out of the ITEM element to look for the SN child element, we use the FindChildElem and GetChildData methods for convenience.
xml.ResetPos(); // top of document
xml.FindElem(); // ORDER element is root
xml.IntoElem(); // inside ORDER
while ( xml.FindElem("ITEM") )
{
xml.FindChildElem( "SN" );
if ( xml.GetChildData() == strFindSN )
break; // found
}
You are NOT on your own
This site has all kinds of examples of doing various XML operations. CMarkup has been widely used for many years. Of course it doesn't do everything, but almost every purpose has at least been discussed. Don't hesitate to ask if you have questions. A good place to go next is the CMarkup Methods.
CMarkUp读写XML(转)的更多相关文章
- C#读写xml文件的常用方法
已知有一个XML文件(bookshop.xml)如下: <?xml version="1.0" encoding="gb2312" ?> <b ...
- PHP读写XML文件的四种方法
PHP对XML文件进行读写操作的方法一共有四种,分别是:字符串方式直接读写.DOMDocument读写. XMLWrite写和XMLReader读.SimpleXML读写,本文将依次对这四种方法进行介 ...
- 在.net中序列化读写xml方法的总结
在.net中序列化读写xml方法的总结 阅读目录 开始 最简单的使用XML的方法 类型定义与XML结构的映射 使用 XmlElement 使用 XmlAttribute 使用 InnerText 重命 ...
- Linux Shell脚本读写XML文件
在Linux下如何用Shell脚本读写XML?现有一个config.xml <?xml version="1.0" encoding="UTF-8"?&g ...
- ADO.NET 快速入门(六):读写 XML
ADO.NET 和 DataSet 可以读写 XML Schema 和 XML.获取更多信息,请参考 How do I...Use XML and the DataSet? DataSet 的 S ...
- C#_在.net中序列化读写xml方法的总结
阅读目录 开始 最简单的使用XML的方法 类型定义与XML结构的映射 使用 XmlElement 使用 XmlAttribute 使用 InnerText 重命名节点名称 列表和数组的序列化 列表和数 ...
- Java 读写XML文件 API--org.dom4j
om4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个十分优秀的JavaXML API,具有性能优异.功能强大和极其易使用的特点,同时它也是一个开放源代码的软件 ...
- Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件
Fixflow引擎解析(四)(模型) - 通过EMF扩展BPMN2.0元素 Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件 Fixflow引擎解析(二)(模型) - BPMN ...
- 三种读写XML的方法
XML文件是一种常用的文件格式,例如WinForm里面的app.config以及Web程序中的web.config文件,还有许多重要的场所都有它的身影.Xml是Internet环境中跨平台的,依赖于内 ...
随机推荐
- 【原创】C++中对象的序列化
1.对象序列化 对象的序列化是指将对象的状态信息转换为可以存储或者传输的形式的过程.对象的反序列化是与序列化相反的过程. 在序列化期间,对象将其当前的状态写入到临时或者永久性的存储区,可以通过从存储区 ...
- 空值排序(oracle/sqlserver)
oracle认为 null 最大. 升序排列,默认情况下,null值排后面. 降序排序,默认情况下,null值排前面. 改变空值办法: (1)用nvl函数或decode函数将null转换为一特定值 替 ...
- php 远程下载图片到本地
大家好,从今天开始,小弟开始写写博客,把自己在工作中碰到的问题的解决方法纪录下来,方便以后查找,也给予别人方便,小弟不才,第一次写博客,有什么不足之处请指出,谢谢! 今天纪录的是怎么通过PHP远程把图 ...
- python中 and 和 or 运算的核心思想 ——— 短路逻辑
python中 and 和 or 运算的核心思想 --- 短路逻辑 1. 包含一个逻辑运算符 首先从基本的概念着手,python中哪些对象会被当成 False 呢?而哪些又是 True 呢? 在Pyt ...
- Scala学习文档-各种使用模式的情况
模式在变量定义中 在定义val或者var的时候,可以使用模式替代简单的标识符,如可以使用模式拆分元组,并把每个值分配给变量 val myTuple = (123,"abc") va ...
- 在开启bin-log日志下Mysql报错
This function has none of DETERMINISTIC, NO SQL解决办法 创建存储过程时 出错信息: ERROR 1418 (HY000): This function ...
- Light OJ 1095 Arrange the Numbers(容斥)
给定n,m,k,要求在n的全排列中,前m个数字中恰好有k个位置不变,有几种方案?首先,前m个中k个不变,那就是C(m,k),然后利用容斥原理可得 ans=ΣC(m,k)*(-1)^i*C(m-k,i) ...
- 《Programming WPF》翻译 第6章 5.我们进行到哪里了?
原文:<Programming WPF>翻译 第6章 5.我们进行到哪里了? WPF提供了资源工具,让我们运用在用户界面中,动态并具有一致性.我们可以在资源字典中存储任意资源,并且可以遍及 ...
- Find the largest multiple of 3 解答
Question Given an array of non-negative integers. Find the largest multiple of 3 that can be formed ...
- 基于微信公众平台的开发(清华大学第二讲)_Alien的笔记
基于微信公众平台的开发(清华大学第二讲)_Alien的笔记 基于微信公众平台的开发(清华大学第二讲)