ZC: xmlXPathEvalExpression(...) 当 xpath的字符串中 包含中文的时候,返回NULL,暂时不知道该怎么处理了...

ZC: 下面是测试的一些代码/文件,留着以后再研究吧...

1、Qt5.3.2

2、XML 的节点的属性中包含中文(XML保存成 UTF-8的格式)

<?xml version="1.0" encoding="utf-8" ?>
<root> <newNode2>content changed</newNode2>
<newNode3 newAttr="YES">newNode3 content</newNode3>
<ceshi attribute="测试">测试一下</ceshi>
<node2 attribute="no">NODE CONTENT</node2> <son>
<grandson>This is a grandson node</grandson>
<newGrandSon>new content</newGrandSon></son>
</root>

3、测试代码:

  ZC: 尝试了 使用 UTF-8的字符串、本地编码格式的字符串,都解析不到 我要的节点...

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
//#include <iconv.h> #include <QDebug>
#include <QTextCodec> MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
} MainWindow::~MainWindow()
{
delete ui;
} int code_convert(char* from_charset, char* to_charset, char* inbuf,
int inlen, char* outbuf, int outlen)
{
iconv_t cd;
char** pin = &inbuf;
char** pout = &outbuf;
cd = iconv_open(to_charset,from_charset);
if(cd == )
return -;
memset(outbuf,,outlen);
if(iconv(cd,(const char**)pin,(unsigned int *)&inlen,pout,(unsigned int*)&outlen)
== -)
return -;
iconv_close(cd);
return ;
} //UNICODE码转为GB2312码
//成功则返回一个动态分配的char*变量,需要在使用完毕后手动free,失败返回NULL
char* u2g(char *inbuf)
{
int nOutLen = * strlen(inbuf) - ;
char* szOut = (char*)malloc(nOutLen);
if (- == code_convert("utf-8","gb2312",inbuf,strlen(inbuf),szOut,nOutLen))
{
free(szOut);
szOut = NULL;
}
return szOut;
} //GB2312码转为UNICODE码
//成功则返回一个动态分配的char*变量,需要在使用完毕后手动free,失败返回NULL
char* g2u(char *inbuf)
{
int nOutLen = * strlen(inbuf) - ;
char* szOut = (char*)malloc(nOutLen);
if (- == code_convert("gb2312","utf-8",inbuf,strlen(inbuf),szOut,nOutLen))
{
free(szOut);
szOut = NULL;
}
return szOut;
} xmlXPathObject* Get_NodeSet(xmlDoc* _pDoc, const xmlChar *szXpath)
{
xmlXPathContextPtr context;
xmlXPathObjectPtr result; context = xmlXPathNewContext(_pDoc);
if (context == NULL)
{
//printf("context is NULL\n");
return NULL;
} result = xmlXPathEvalExpression(szXpath, context);
xmlXPathFreeContext(context);
if (result == NULL)
{
//printf("xmlXPathEvalExpression return NULL\n");
return NULL;
} if (xmlXPathNodeSetIsEmpty(result->nodesetval))
{
xmlXPathFreeObject(result);
//printf("nodeset is empty\n");
return NULL;
} return result;
} void MainWindow::on_pbtnXPath_clicked()
{
xmlDocPtr doc = NULL; //定义解析文档指针
xmlNodePtr curNode = NULL; //定义结点指针(你需要它为了在各个结点间移动) char *szDocName = "F:/ZZ_Qt5/Qt532_vs2010/build-libxml2_zz-z-Debug/debug/ChangedXml.xml"; doc = xmlReadFile(szDocName, "GB2312", XML_PARSE_RECOVER); //解析文件
//doc = xmlReadFile(szDocName, "UTF-8", XML_PARSE_RECOVER); if (NULL == doc)
{
qDebug() << "Document not parsed successfully.";
return;
} char* pcCeShi = "测试";
QTextCodec *pCodec = QTextCodec::codecForName("GBK");
QString strCeShi = pCodec->toUnicode(pcCeShi); //QString str = "/root/node2[@attribute='no']";
QString str = "/root/node2[@attribute='"+strCeShi+"']";
QByteArray ba = str.toUtf8();
//QByteArray ba = str.toLocal8Bit();
char pc[] = {};
memcpy(&pc[], ba.data(), ba.length());
//pc[ba.length()] = '\0'; char *p0 = "/root/node2[@attribute='测试']";
char* p1 = g2u(p0);
char pc1[] = {};
memcpy(&pc1[], p1, strlen(p1)); //xmlChar *szXpath =BAD_CAST ("/root/node2[@attribute='no']");
xmlChar *szXpath = BAD_CAST (p1);
xmlXPathObjectPtr app_result = Get_NodeSet(doc, szXpath); //查询并得到结果 if (NULL == app_result)
{
qDebug() << "app_result is NULL";
return;
}
xmlChar *szValue = NULL;
if(app_result)
{
xmlNodeSetPtr nodeset = app_result->nodesetval;
for (int i = ; i < nodeset->nodeNr; i++)
{
curNode = nodeset->nodeTab[i];
if(curNode != NULL)
{
szValue = xmlGetProp(curNode,BAD_CAST "attribute");
if (szValue != NULL)
{
qDebug() << "attribute = " << (char*)szValue;
xmlFree(szValue);
} szValue = xmlNodeGetContent(curNode);
if (szValue != NULL)
{
qDebug() << "content = " << (char*)szValue;
xmlFree(szValue);
}
}
}
xmlXPathFreeObject (app_result);
}
xmlFreeDoc(doc); free(p1);
}

4、

5、

6、

libxml2的xpath检索中文的更多相关文章

  1. jQuery EasyUI 1.4.4 Combobox无法检索中文输入的问题

    在项目里使用了EasyUI的Combobox,当ComboBox的item是英文时,都能正常检索出对应项,但是如果使用中文输入法输入几个字母然后通过按shift键输入时,奇怪的事情发生了,combob ...

  2. [libxml2]_[XML处理]_[使用libxml2的xpath特性修改xml文件内容]

    场景: 1.在软件需要保存一些配置项时,使用数据库的话比较复杂,查看内容也不容易.纯文本文件对utf8字符支持也不好. 2.这时候使用xml是最佳选择,使用跨平台库libxml2. 3.基于xpath ...

  3. jQuery EasyUI Combobox无法检索中文输入的问题

    在项目里使用了EasyUI的Combobox,当ComboBox的item是英文时,都能正常检索出对应项,但是如果使用中文输入法输入几个字母然后通过按shift键输入时,奇怪的事情发生了,combob ...

  4. libxml2用xpath进行查找

    xml文档 <?xml version="1.0" encoding="UTF-8"?> <radios> <radio> ...

  5. EF检索中文失败的解决办法

    1. MYSQL: 保证所有的的列都是UTF8格式. 2. VS2010: 在data server建立连接时,选择advance,将chracterset设成utf8,这样在VS2010里查看和更改 ...

  6. Mybatis使用MySQL模糊查询时输入中文检索不到结果怎么办--转自http://www.jb51.net/article/88236.htm

    这篇文章主要介绍了Mybatis使用MySQL模糊查询时输入中文检索不到结果的解决办法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下   项目开发中,在做Mybatis动态查询时,遇到了 ...

  7. Django--全文检索功能

    经过两个月的时间,毕设终于算是把所有主要功能都完成了,最近这一周为了实现全文检索的功能,也算是查阅了不少资料,今天就在这里记录一下,以免以后再用到时抓瞎了~ 首先介绍一下我使用的Django全文检索逻 ...

  8. Sphinx和coreseek检索引擎

    Sphinx是检索英文用,coreseek是检索中文用. Sphinx(斯芬克斯)是一个基于SQL的全文检索引擎,可以结合MySQL,PostgreSQL做全文搜索,它可以提供比数据库本身更专业的搜索 ...

  9. Java 关于中文乱码处理的经验总结【转载】

    为什么说乱码是中国程序员无法避免的话题呢?这个首先要从编码机制上说起,大家都是中文和英文的编码格式不是一样,解码也是不一样的!如果中国的程序员不会遇到乱码,那么只有使用汉语编程.汉语编程是怎么回事我也 ...

随机推荐

  1. 升级到0.9 log4jmongodb(mongo-java-driver 3.x)后,报No server chosen by WritableServerSelector from cluster description ClusterDescription

    接上一篇http://www.cnblogs.com/zhjh256/p/6690003.html. 17-04-11 13:47:54.676 INFO cluster-ClusterId{valu ...

  2. Eureka-zookeeper的服务发现替代方案

    参考: https://my.oschina.net/thinwonton/blog/1622905 http://www.open-open.com/lib/view/open14269407225 ...

  3. python 爬取历史天气

    python 爬取历史天气 官网:http://lishi.tianqi.com/luozhuangqu/201802.html # encoding:utf-8 import requests fr ...

  4. redhat7.4+shell离线安装docker

    首先要准备离线安装包 1.docker官网下载 2.docker-compose gitbub官网下载 3.我的docker-compose install-docker.sh #!/bin/sh u ...

  5. php mysqli 的使用方法

    原文链接:https://blog.csdn.net/solly793755670/article/details/52217456 Mysqli是php5之后才有的功能 需要修改php.ini的配置 ...

  6. Centos7,配置防火墙,开启端口

    原文链接:https://blog.csdn.net/u013410747/article/details/61696178 适用于CentOS 7 64位的指令: .查看已开放的端口(默认不开放任何 ...

  7. 51Nod 1667 概率好题 - 容斥原理

    题目传送门 无障碍通道 有障碍通道 题目大意 若$L_{i}\leqslant x_{i} \leqslant R_{i}$,求$\sum x_{i} = 0$以及$\sum x_{i} < 0 ...

  8. fastjson常用方法

    fastjson是一款alibaba公司开发的json工具包.json经常被使用在数据传输方面,因此特意对它的一些常用方法做备注,欢迎看客在评论区补充或指出问题. 首先定义一个实体类,用于我们进行对象 ...

  9. Read.csv: some rows are missing

    read.csv in R doesn't import all rows from csv file The OP indicates that the problem is caused by q ...

  10. c# 之 Microsoft.Practices.EnterpriseLibrary连接Oracle

    首先下载Microsoft Enterprise Library 5.0:http://www.microsoft.com/en-us/download/details.aspx?id=15104,这 ...