操作XML数据的C++类CMarkup

CMarkup是优秀操作XML数据的C++类,处理速度相当的快。

而且相对简洁很多。

 

基本接口如下:

来自CMarkup的官网http://www.firstobject.com/dn_markup.htm

Initialization

 

LoadPopulates the CMarkup object from a file and parses it
SetDocPopulates the CMarkup object from a string and parses it

 

Output

 

SaveWrites the document to file
GetDocReturns the whole document as a markup string
GetDocFormattedReturns the formatted markup string of the whole document

 

File mode

 

OpenOpens file, initiating file mode for read or write (and append is a special case of write mode)
CloseCloses file and ends file mode
FlushFor file write mode, this flushes any partial document in memory (up to the closing tags) and the file stream itself

 

Changing the current position

 

FindElemLocates next element, optionally matching tag name or path
FindChildElemLocates next child element matching tag name or path
FindPrevElemLocates previous element, optionally matching tag name
FindPrevChildElemLocates previous child element, optionally matching tag name
FindNodeLocates next node, optionally matching node type(s)
IntoElemGo "into" current main position element such that it becomes the current parent position
OutOfElemMakes the current parent position into the current main position
ResetPosResets the current position to the start of the document
ResetMainPosResets the current main position to before the first sibling
ResetChildPosResets the current child position to before the first child

 

Adding to the Document

 

AddElemAdds an element after the current main position element or last sibling
InsertElemInserts an element before the current main position element or first sibling
AddChildElemAdds an element after the current child position element or last child
InsertChildElemInserts an element before the current child position element or first child
AddSubDocAdds a subdocument after the current main position element or last sibling
InsertSubDocInserts a subdocument before the current main position element or first sibling
AddChildSubDocAdds a subdocument after the current child position element or last child
InsertChildSubDocInserts a subdocument before the current child position element or first child
AddNodeAdds a node after the current node or at the end of the parent element content
InsertNodeinserts a node before the current node or at the beginning of the parent element content

 

Removing from the Document

 

RemoveElemRemoves the current main position element including child elements
RemoveChildElemRemoves the current child position element including its child elements
RemoveNodeRemoves the current node
RemoveAttribRemoves the specified attribute from the current main position element
RemoveChildAttribRemoves the specified attribute from the current child position element

 

Getting Values

 

GetDataReturns the string value of the current main position element or node
GetChildDataReturns the string value of the current child position element
GetElemContentReturns the string markup content of the current main position element including child elements
GetSubDocReturns the subdocument markup string of the current main position element including child elements
GetChildSubDocReturns the subdocument markup string of the current child position element including child elements
GetAttribReturns the string value of the specified attribute of the main position element (or processing instruction)
GetChildAttribReturns the string value of the specified attribute of the child position element
HasAttribReturns true if the specified attribute exists in the main position element (or processing instruction)
HasChildAttribReturns true if the specified attribute exists in the child position element
GetTagNameReturns the tag name of the main position element (or processing instruction)
GetChildTagNameReturns the tag name of the child position element
FindGetDataLocates the next element matching the specified path and returns the string value

 

Setting Values

 

SetDataSets the value of the current main position element or node
SetChildDataSets the value of the current child position element
SetElemContentSets the markup content of the current main position element
SetAttribSets the value of the specified attribute of the current main position element (or processing instruction)
SetChildAttribSets the value of the specified attribute of the current child position element
FindSetDataLocates the next element matching the specified path and sets the value

 

Other Info

 

GetAttribNameReturns the name of attribute specified by number for the current main position element
GetNodeTypeReturns the node type of the current node
GetElemLevelReturns the level of the current main position
GetElemFlagsReturns the current main position element's flags
SetElemFlagsSets the current main position element's flags
GetOffsetsObtains the document text offsets of the current main position
GetAttribOffsetsObtains the document text offsets of the specified attribute in the current main position

 

Remembering positions

 

SavePosSaves the current position with an optional string name using a hash map
RestorePosGoes to the position saved with DE>SavePosDE>
SetMapSizeSets the size of a map for use with the DE>SavePosDE> and DE>RestorePosDE> methods
GetElemIndexReturns the integer index of the current main position element
GotoElemIndexSets the current main position element to that of the given integer index
GetChildElemIndexReturns the integer index of the current child position element
GotoChildElemIndexSets the current child position element to that of the given integer index
GetParentElemIndexReturns the integer index of the current parent position element
GotoParentElemIndexSets the current parent position element to that of the given integer index
GetElemPathReturns a string representing the absolute path of the main position element
GetChildElemPathReturns a string representing the absolute path of the child position element
GetParentElemPathReturns a string representing the absolute path of the parent position element

 

Document Status

 

IsWellFormedDetermines if document has a single root element and properly contained elements
GetResultReturns result markup from last parse or file operation
GetErrorReturns English error/result synopsis string from last parse or file operation
GetDocFlagsReturns the document flags
SetDocFlagsSets the document flags
GetDocElemCountReturns the number of elements in the document

 

Static Utility Functions

 

ReadTextFileReads a text file into a string
WriteTextFileWrites a string to a text file
GetDeclaredEncodingReturns the encoding name as a string from the XML declaration
EscapeTextReturns the string with special characters encoded for markup
TextReturns the string with special characters unencoded for a string value
UTF8ToAConverts a UTF-8 string to a non-Unicode ("ANSI") string
AToUTF8Converts a non-Unicode ("ANSI") string to UTF-8
UTF16To8Converts a UTF-16 string to UTF-8
UTF8To16Converts a UTF-8 string to UTF-16
EncodeBase64Encodes a binary data buffer to a Base64 string
DecodeBase64Encodes a Base64 string to a binary data buffer

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
XML对应的C是指在C语言中用于解析和操作XML文件的。在C语言中,没有像Java或C#那样的内置XML相关的库,因此需要通过使用第三方库或自己编写代码来实现对XML文件的解析和操作。 常见的用于处理XML的第三方库有libxml2和Expat。这些库提供了一系列的函数和数据结构,可以方便地实现XML文件的读取、解析和生成。 XML文件的读取和解析可以通过以下代码示例实现: #include <stdio.h> #include <libxml/parser.h> int main() { xmlDocPtr doc; xmlNodePtr cur; doc = xmlReadFile("example.xml", NULL, 0); if (doc == NULL) { printf("Failed to parse XML file."); return 1; } cur = xmlDocGetRootElement(doc); if (cur == NULL) { printf("Empty XML file."); xmlFreeDoc(doc); return 1; } // 遍历XML文件中的节点并进行相应的操作 // ... xmlFreeDoc(doc); return 0; } 通过上述代码,可以读取名为example.xmlXML文件,并获取根节点以便后续的操作。你可以根据自己的需求,使用相应的函数和数据结构来实现对XML文件的解析和操作。 当然,如果你对这些第三方库不甚满意,也可以根据自己的需求,自行编写C代码来实现对XML文件的解析和操作。实现这样的C需要使用C的字符串操作函数、文件操作函数等,较为复杂,在此不再详述。 总的来说,XML对应的C,即通过第三方库或自己编写代码实现的用于解析和操作XML文件的,可以方便地读取、解析和生成XML文件,满足对XML文件的各种需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值