着手准备下一个项目

    这两天听说又有新项目了,听头的大概意思是,在语音网关中新加入一功能,机器开启时,把一准备好的xml数据发送到Intel公司的Viiv服务器中去,然后,接收并分析服务器发送回来的xml数据。
      初步分析,给我的时间是:一个半月,另外,具体详细需求:不明,摸着石头过河。

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

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

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


以下是示例:
   打开一个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.

posted @ 2006-06-08 17:27  shipfi  阅读(420)  评论(0编辑  收藏  举报