时间:2021-05-20
前言
Libxml2 是一个xml c语言版的解析器,本来是为Gnome项目开发的工具,是一个基于MIT License的免费开源软件。它除了支持c语言版以外,还支持c++、PHP、Pascal、Ruby、Tcl等语言的绑定,能在Windows、Linux、Solaris、MacOsX等平台上运行。功能还是相当强大的,相信满足一般用户需求没有任何问题。
libxml2常用数据类型
xmlChar是libxml2中的字符类型,在库中的所有字符,字符串都是基于这个数据类型的。
xmlChar*是指针类型,很多函数都会返回一个动态分配的内存的xmlChar*类型的变量,因此,在使用这类函数时要记得释放内存,否则会导致内存泄漏,例如这样的用法:
场景
1.libxml2基本上算是xml的C/C++标准读写库. 在linux,macOS里是默认支持. 可惜在Windows上有自己专有的msxml, 所以并不支持libxml2, 恶心的是msxml还不是标配, 还要必须另外下载安装, 所以作为Windows上优先选择的XML库, 就是可跨平台的libxml2.
2.xml的sax读取库expat也是比较优秀的选择, 可惜不支持写.
3.一般的写库方式是生成一整个DOM结构, 之后把这个DOM结构输出到XML格式的文本里, 可调用自带写函数或标准io函数. 这样的缺点是如果生成这个DOM结构过于大, 会导致在生成这个DOM结构时内存暴涨,之后再输出到内存里,这时候内存又暴涨一次,最后从内存输出到文件里.
说明
1.DOM结构存储非常浪费内存, 如果数据量大时, 但是元素的父子关系, 文本值,属性值等等很浪费内存. 如果我们可以按照每个元素来输出的话,最好输出完就释放元素内存, 那么能最大限度的利用内存资源.
2.局部输出元素可以最大限度使用系统的资源, 比如IO输出需要权限限制的函数, 或者输出到界面等
例子
以下例子是windows上使用libxml2, 用mingw编译出的libxml2, 使用_wfopen来打开unicode编码的文件路径.
输出文件:
<?xml version="1.0" encoding="UTF-8"?><ROOT><one> <node1>contentÖÐÎÄ of node 1</node1> <node2/> <node3 attribute="yes" foo="bar">this node has attributes</node3> <node4>other way to create content (which is also a node)</node4> </one><one> <node1>content of node 1</node1> <node2/> <node3 attribute="yes" foo="bar">this node has attributes</node3> <node4>other way to create content (which is also a node)</node4> </one><one> <node1>content of node 1</node1> <node2/> <node3 attribute="yes" foo="bar">this node has attributes</node3> <node4>other way to create content (which is also a node)</node4> </one><one> <node1>content of node 1</node1> <node2/> <node3 attribute="yes" foo="bar">this node has attributes</node3> <node4>other way to create content (which is also a node)</node4> </one><one> <node1>content of node 1</node1> <node2/> <node3 attribute="yes" foo="bar">this node has attributes</node3> <node4>other way to create content (which is also a node)</node4> </one><one> <node1>content of node 1</node1> <node2/> <node3 attribute="yes" foo="bar">this node has attributes</node3> <node4>other way to create content (which is also a node)</node4> </one><one> <node1>content of node 1</node1> <node2/> <node3 attribute="yes" foo="bar">this node has attributes</node3> <node4>other way to create content (which is also a node)</node4> </one><one> <node1>content of node 1</node1> <node2/> <node3 attribute="yes" foo="bar">this node has attributes</node3> <node4>other way to create content (which is also a node)</node4> </one><one> <node1>content of node 1</node1> <node2/> <node3 attribute="yes" foo="bar">this node has attributes</node3> <node4>other way to create content (which is also a node)</node4> </one><one> <node1>content of node 1</node1> <node2/> <node3 attribute="yes" foo="bar">this node has attributes</node3> <node4>other way to create content (which is also a node)</node4> </one></ROOT>总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
前些天用python处理xml的转换的一个小程序,用来把xml,xsl转换成html。用的libxml2,所以还要先安装了libxml2模块才能使用。#-*-c
本文实例讲述了Python将xml和xsl转换为html的方法。分享给大家供大家参考。具体分析如下:这里需要用libxml2,所以还要先安装了libxml2模块
SAX将dd.xml解析成html。当然啦,如果得到了xml对应的xsl文件可以直接用libxml2将其转换成html。复制代码代码如下:#!/usr/bin/
一、下载安装包http://ftp://xmlsoft.org/libxml2/libxml2-2.9.4.tar.gz二、安装python-develcent
最近要做一个VRP的算法,测试集都是放在Xml文件中,而我的算法使用C++来写,所以需要用C++来读取Xml文件。在百度上搜“C++读取Xml文件”,可以出来很