着手准备下一个项目

本文分享了在嵌入式Linux环境下使用libxml库进行XML解析的经验,包括库的安装、基本类型的介绍及示例代码。
    这两天听说又有新项目了,听头的大概意思是,在语音网关中新加入一功能,机器开启时,把一准备好的xml数据发送到Intel公司的Viiv服务器中去,然后,接收并分析服务器发送回来的xml数据。
      初步分析,给我的时间是:一个半月,另外,具体详细需求:不明,摸着石头过河。

      当时只有一个感觉:Are you crazy? confused_smile.gif

     其一,Viiv的技术刚刚推出,网上能用的资料少得可怜.大部分都是介绍Viiv的.而非如何与Viiv交互的,仅有的资料就要靠一篇十来页的PDF文档.
     其二,由于是嵌入式linux,机种本身不带有xml的parser,需要找一个c语言编写的xml parser集成到linux中.
     其三,xml数据是从机器的什么地方收集,发送回的xml数据经分析后要写入哪地方去.都没有说明.


   今天一天时间,找了一个C的xml parser,名称叫libxml,用了一下,感觉比较好用.最主要的是document挺全的,如果要下此库,可以到它的主页: http://xmlsoft.org/ 上下载。

   此库可以适用多个平台,包括Windows,Linux,Unix,为了以后用得着,以下是我对此库写的一些心得.

   ------------------------------------------------------------------------------------------------------------------------------------
   ------------------------------------------------------------------------------------------------------------------------------------

 首先,此库定义了一些基本的类型,用得比较多:
   a)xmlChar --相当于字符串类型
   b)xmlDoc  -- 整个文档的结构体
   c)xmlNodePtr/xmlNode --结点的结构体

  xmlDoc的结构体如下所示:
 1 ExpandedBlockStart.gif ContractedBlock.gif Structure xmlDocstruct _xmlDoc  dot.gif {
 2InBlock.gif    void *    _private    : application data
 3InBlock.gif    xmlElementType    type    : XML_DOCUMENT_NODE, must be second !
 4InBlock.gif    char *    name    : name/filename/URI of the document
 5InBlock.gif    struct _xmlNode *    children    : the document tree
 6InBlock.gif    struct _xmlNode *    last    : last child link
 7InBlock.gif    struct _xmlNode *    parent    : child->parent link
 8InBlock.gif    struct _xmlNode *    next    : next sibling link
 9InBlock.gif    struct _xmlNode *    prev    : previous sibling link
10InBlock.gif    struct _xmlDoc *    doc    : autoreference to itself End of common p
11InBlock.gif    int    compression    : level of zlib compression
12InBlock.gif    int    standalone    : standalone document (no external refs)
13InBlock.gif    struct _xmlDtd *    intSubset    : the document internal subset
14InBlock.gif    struct _xmlDtd *    extSubset    : the document external subset
15InBlock.gif    struct _xmlNs *    oldNs    : Global namespace, the old way
16InBlock.gif    const xmlChar *    version    : the XML version string
17InBlock.gif    const xmlChar *    encoding    : external initial encoding, if any
18InBlock.gif    void *    ids    : Hash table for ID attributes if any
19InBlock.gif    void *    refs    : Hash table for IDREFs attributes if any
20InBlock.gif    const xmlChar *    URL    : The URI for that document
21InBlock.gif    int    charset    : encoding of the in-memory content actua
22InBlock.gif    struct _xmlDict *    dict    : dict used to allocate names or NULL
23InBlock.gif    void *    psvi    : for type/PSVI informations
24ExpandedBlockEnd.gif}

xmlNode的结构体:
ExpandedBlockStart.gif ContractedBlock.gif Structure xmlNodestruct _xmlNode  dot.gif {
InBlock.gif    
void *    _private    : application data
InBlock.gif    xmlElementType    type    : type number, must be second 
!
InBlock.gif    
const xmlChar *    name    : the name of the node, or the entity
InBlock.gif    
struct _xmlNode *    children    : parent->childs link
InBlock.gif    
struct _xmlNode *    last    : last child link
InBlock.gif    
struct _xmlNode *    parent    : child->parent link
InBlock.gif    
struct _xmlNode *    next    : next sibling link
InBlock.gif    
struct _xmlNode *    prev    : previous sibling link
InBlock.gif    
struct _xmlDoc *    doc    : the containing document End of common p
InBlock.gif    xmlNs 
*    ns    : pointer to the associated namespace
InBlock.gif    xmlChar 
*    content    : the content
InBlock.gif    
struct _xmlAttr *    properties    : properties list
InBlock.gif    xmlNs 
*    nsDef    : namespace definitions on this node
InBlock.gif    
void *    psvi    : for type/PSVI informations
InBlock.gif    unsigned 
short    line    : line number
InBlock.gif    unsigned 
short    extra    : extra data for XPath/XSLT
ExpandedBlockEnd.gif}


以下是示例:
   打开一个xml文件:
   xmlDocPtr doc;
  doc = xmlParseFile(docname);
  if (doc == NULL ) {
     fprintf(stderr,"Document not parsed successfully. \n");
    return;
  }
   获得根元素:
  xmlNodePtr cur;
  cur = xmlDocGetRootElement(doc);
 
  比较元素的值:
 if (xmlStrcmp(cur->name, (const xmlChar *) "story")) {
    fprintf(stderr,"document of the wrong type, root node != story");  
    xmlFreeDoc(doc);
    return;
  }

  获得子结点:
  cur = cur->xmlChildrenNode;
   while (cur != NULL) {
   if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){
      parseStory (doc, cur);
   }
     cur = cur->next;
 }

 获得结点元素的值:
 void
parseStory (xmlDocPtr doc, xmlNodePtr cur) {
 xmlChar *key;
  cur = cur->xmlChildrenNode;
  while (cur != NULL) {
    if ((!xmlStrcmp(cur->name, (const xmlChar *)"keyword"))) {
   1     key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
    printf("keyword: %s\n", key);
    xmlFree(key);
    }
cur = cur->next;
}
    return;
}

注意 1:When we find the "keyword" element, we need to print its contents. Remember that in XML, the text contained within an element is a child node of that element, so we turn to cur->xmlChildrenNode. To retrieve it, we use the function xmlNodeListGetString, which also takes the doc pointer as an argument. In this case, we just print it out.
   意思是说,结点的值就相当于该结点的ChildrenNode,所以,获得某个结点,要想取其值的话,一定要用结点->xmlChildrenNode.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值