用C解析xml

2008-02-23 05:26:08来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

//程式代码1,编译通过,运行的时候错误,运行能够把一个xml文档解析出来,但是执行conndb()时候,老是报"段错误",其实conndb()程式跟程式代码2几乎相同.但是为什么运行在代码1中老是报错????大家能否给我看一下.


#include <stdio.h>;
#include <string.h>;
#include <stdlib.h>;
#include <libxml/xmlmemory.h>;
#include <libxml/parser.h>;

#include "libpq-fe.h"

void
parsePacket (xmlDocPtr doc, xmlNodePtr cur) {
char str[5][50];
xmlChar *key;
int i=0;
cur = cur->;xmlChildrenNode;
while (cur != NULL) {
key = xmlNodeListGetString(doc, cur->;xmlChildrenNode, 1);
if(key!=NULL)
{
printf("%s\n", key);
}
// xmlFree(key);
cur = cur->;next;
}
return;
}

static void
parseDoc(char *docname) {

xmlDocPtr doc;
xmlNodePtr cur;

doc = xmlParseFile(docname);

if (doc == NULL ) {
fprintf(stderr,"Document not parsed successfully. \n");
return;
}

cur = xmlDocGetRootElement(doc);

if (cur == NULL) {
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return;
}

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 *)"packet"))){
parsePacket (doc, cur);
}

cur = cur->;next;
}

xmlFreeDoc(doc);
return;
}

int
main(int argc, char **argv) {

char *docname;

if (argc <= 1) {
printf("Usage: %s docname\n", argv[0]);
return(0);
}

docname = argv[1];
parseDoc (docname);
conndb();
return (1);
}

int conndb()
{
PGconn *conn;
PGresult *res;
char *pghost = NULL;
char *pgport =NULL;
char *pgoptions =NULL;
char *pgtty = NULL;
char *dbname ="test";
int i = 0,t = 0,s,k;
conn = PQsetdb(pghost,pgport,pgoptions,pgtty,dbname);
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr,"Connection to database '%s' failed!\n",dbname);
return 0;
PQfinish(conn);
}
res=PQexec(conn,"insert into test_tbl values('22','22')");
if(PQresultStatus(res)!= PGRES_COMMAND_OK)
{
return 0;
fprintf(stderr,"insert failed");
}

PQfinish(conn);
PQclear(res);
return 1;
}
//------程式代码2.编译通过,运行也没有问题.因此,数据库这边是没有问题的

#include <stdio.h>;
#include <libpq-fe.h>;

int main()
{
PGconn *conn;
PGresult *res;
char *pghost = NULL;
char *pgport =NULL;
char *pgoptions =NULL;
char *pgtty = NULL;
char *dbname ="test";/**数据库名*/
int i = 0,t = 0,s,k;
conn = PQsetdb(pghost,pgport,pgoptions,pgtty,dbname);
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr,"Connection to database '%s' failed!\n",dbname);
PQfinish(conn);
return 0;
}
//res = PQexec(conn,"SELECT * FROM test_tbl");
PQexec(conn,"insert into test_tbl values('11','11')");
PQfinish(conn);
PQclear(res);
return 0;
}



标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: C 中处理XML文档

下一篇: C语言高效编程的的四招技巧

热门词条
热门标签