使用libxml2对xml进行SAX读取
2012-11-23 12:23
281 查看
//下面的程序是从这里的例子修改过来的
//查找并打印出<w:sectPr>元素,属性值,和它的子元素值
//属性值必须在双引号里面,否则libxml通过SAX解析不了
//查找并打印出<w:sectPr>元素,属性值,和它的子元素值
//属性值必须在双引号里面,否则libxml通过SAX解析不了
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <string> #include <iostream> #include "libxml/SAX2.h" #include "libxml/xmlstring.h" int g_element_count = 0; static void OnStartElementNs( void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI, int nb_namespaces, const xmlChar **namespaces, int nb_attributes, int nb_defaulted, const xmlChar **attributes) { if (!xmlStrcmp(localname, BAD_CAST"sectPr")) { ++g_element_count; std::cout<<"<<<<<<<<<<<<<<<<<<<<<<"<<std::endl<<std::endl; } if (g_element_count) { std::cout<<"element : "<< prefix<<":"<<localname << std::endl; for (int i=0; i<nb_attributes; ++i) { std::string str = (char *)attributes[5*i+3]; xmlChar *attr_value = xmlCharStrdup(str.substr(0, str.find("\"")).c_str()); std::cout<<"<<<< "<<attributes[5*i+1]<<":"<<attributes[5*i]<< "=" << attr_value <<std::endl; xmlFree(attr_value); } } } static void OnEndElementNs( void* ctx, const xmlChar* localname, const xmlChar* prefix, const xmlChar* URI ) { if (!xmlStrcmp(localname, BAD_CAST"sectPr")) { --g_element_count; } } static void OnCharacters(void *ctx, const xmlChar *ch, int len) { if (g_element_count) { std::string str = (char *)ch; std::string value = str.substr(0, str.find('<')); std::cout<<value<<std::endl; } } int FindElement(FILE *f) { char chars[1024]; int res = fread(chars, 1, 4, f); if (res <= 0) { return 1; } xmlInitParser(); xmlSAXHandler SAXHander; memset(&SAXHander, 0, sizeof(xmlSAXHandler)); SAXHander.initialized = XML_SAX2_MAGIC; SAXHander.startElementNs = OnStartElementNs; SAXHander.endElementNs = OnEndElementNs; SAXHander.characters = OnCharacters; xmlParserCtxtPtr ctxt = xmlCreatePushParserCtxt( &SAXHander, NULL, chars, res, NULL ); while ((res = fread(chars, 1, sizeof(chars)-1, f)) > 0) { if(xmlParseChunk(ctxt, chars, res, 0)) { xmlParserError(ctxt, "xmlParseChunk"); return 1; } } xmlParseChunk(ctxt, chars, 0, 1); xmlFreeParserCtxt(ctxt); xmlCleanupParser(); return 0; } int main(int argc, char *argv[]) { FILE *fd = fopen(argv[1], "rb"); FindElement(fd); fclose(fd); return 0; }
相关文章推荐
- 使用libxml2进行xml开发(一)
- 使用JavaEE的ServerAuthModule模块和web.xml进行相应配置,实现对用户的权限控制
- 使用XmlDocument操作XML进行查询、增加、修改、删除、保存应用的实例(转载)
- javaweb学习总结十三(dom4j方式对XML文档进行解析以及Xpath的使用)
- 使用DateTimeOffset 对xml中的日期时间格式时区进行处理
- c#使用xml格式化数据进行获取
- 使用HttpClient进行http post/get方法的调用,以及使用dom4j解析xml
- XML_使用dom4j对xml文件进行解析_.flv
- spring3 混合使用 XML 与注解(Annotation)进行 Bean 的配置
- 使用XPATH对XML数据进行解析
- 使用JAXP进行SAX解析(XMLReaderFactory、XMLReader 、SAXParserFactory与SAXParser)
- 使用js对xml进行迭代遍历
- Unity3D 使用XML进行简单的配置文件改动
- 【XML解析】(1)Java下使用JAXP中的DOM解析方式对XML文档进行解析
- 接收XML数据并使用XSL样式表进行格式化
- c++builder使用XMLDocument操作XML进行增,删,改,读!
- 学习《spring 3.x企业应用开发实战》之使用OXM进行对象XML映射
- 使用JAXP进行SAX解析(XMLReaderFactory、XMLReader 、SAXParserFactory与SAXParser)
- 使用 Apache WSS4J 和 WebSphere DataPower SOA Appliances 对 XML 文档进行签名和验证
- 大型Java项目中使用maven进行管理,pom.xml文件中build的配置