用C解析xml
2008-02-23 05:26:08来源:互联网 阅读 ()
#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语言高效编程的的四招技巧
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash