libxml2 如何进行字符串处理

5 篇文章 0 订阅
5 篇文章 0 订阅

libxml2

xmlReadMemory

xmlParseMemory

 

都是处理xml文件的,就没直接处理xml字符串的函数

 

1.     xmlParseMemory,字符串转为XML文档

2.     xmlDocGetRootElement,获取XML文档根节点

3.     xmlStrcmp,比较XML字符串,与strcmp差不多

4.     curr = curr->xmlChildrenNodeXML节点指针指向第一个子节点

5.     curr = curr->nextXML节点指针指向下一个兄弟节点

6.     xmlNodeGetContent,获取XML节点的内容

7.     xmlFreeDoc,释放节点,与free差不多

 

#include <iconv.h>  
#include <libxml/parser.h>  
#include <libxml/xmlmemory.h>  
  
#include "soapH.h"  
#include "ExchangeRateWebServiceSoap12.nsmap"  
  
#define FIELD_LEN 16  
  
int conv_charset(const char *dest, const char *src, char *input, size_t ilen, char *output, size_t olen) {  
    iconv_t conv = iconv_open(dest, src);  
    if ( conv == (iconv_t) -1 )  
        return -1;  
    memset(output, 0, olen);  
    if ( iconv(conv, &input, &ilen, &output, &olen) )  
        return -1;  
    iconv_close(conv);  
    return 0;  
}  
  
int main(int argc, char **argv) {  
    if ( argc != 2 && argc != 3 ) {  
        printf("Usage: %s type [end_point]\n", argv[0]);  
        printf("\ttype = A : all rate\n");  
        printf("\ttype = B : basic rate\n");  
        printf("\ttype = C : cross rate\n");  
        exit(-1);  
    }  
  
    struct soap soap;  
    soap_init(&soap);  
    // don't set is OK  
    //soap_set_mode(&soap, SOAP_C_UTFSTRING);  
  
    struct _ns1__getExchangeRate request;  
    struct _ns1__getExchangeRateResponse response;  
  
    request.theType = argv[1];  
    char *endpoint = NULL;  
    if ( argc == 3 )  
        endpoint = argv[2];  
    if ( soap_call___ns3__getExchangeRate(&soap, endpoint, NULL, &request, &response) == SOAP_OK ) {  
        int len = strlen(response.getExchangeRateResult->__any);  
        xmlDocPtr pdoc = xmlParseMemory(response.getExchangeRateResult->__any, len);  
        xmlNodePtr root = xmlDocGetRootElement(pdoc);  
        xmlNodePtr curr = root;  
        while ( xmlStrcmp(curr->name, (const xmlChar *) "getExchangeRate") )  
            curr = curr->xmlChildrenNode;  
        for ( curr = curr->xmlChildrenNode; curr; curr = curr->next ) {  
            xmlNodePtr data;  
            for ( data = curr->xmlChildrenNode; data; data = data->next ) {  
                char ifield[FIELD_LEN];  
                char ofield[FIELD_LEN];  
                strcpy(ifield, xmlNodeGetContent(data));  
                if ( conv_charset("GBK", "UTF-8", ifield, strlen(ifield), ofield, FIELD_LEN) )  
                    printf("%s\t%s\n", data->name, ifield);  
                else  
                    printf("%s\t%s\n", data->name, ofield);  
            }  
            printf("\n");  
        }  
        xmlFreeDoc(pdoc);  
    }  
    else {  
        soap_print_fault(&soap, stderr);  
    }  
  
    soap_destroy(&soap);  
    soap_end(&soap);  
    soap_done(&soap);  
    return 0;  
}  
  gcc -O2 -o exchange exchange.c soapC.c soapClient.c ../../stdsoap2.c -I../.. -I/usr/include/libxml2 -L../.. -lgsoap -lxml2

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值