- 论坛徽章:
- 0
|
//parsepsml.c
#include <stdio.h>;
#include <string.h>;
#include <stdlib.h>;
#include <malloc.h>;
#include <libxml/xmlmemory.h>;
#include <libxml/parser.h>;
#include "libpq-fe.h"
void
parsePacket (xmlDocPtr doc, xmlNodePtr cur,char *docname) {
xmlChar *key;
char *strtmp;
char * sch[6];
int i =0;
cur = cur->;xmlChildrenNode;
while (cur != NULL) {
key = xmlNodeListGetString(doc, cur->;xmlChildrenNode, 1);
if(key!=NULL)
{
printf("%s\n", key);
sch=(char *)malloc(sizeof(char)* 300);
strcpy(sch ,key) ;
printf("%s\n",sch);
i++;
}
cur = cur->;next;
}
printf("%s\n",sch[0]);
printf("%s\n",sch[1]);
printf("%s\n",sch[2]);
printf("%s\n",sch[3]);
printf("%s\n",sch[4]);
printf("%s\n",sch[5]);
char schar[]="insert into packet_summary_tbl values('";
strcat(schar,docname);
strcat(schar,"'," ;
strcat(schar,sch[0]);
strcat(schar,",'" ;
strcat(schar,sch[1]);
strcat(schar,"','" ;
strcat(schar,sch[2]);
strcat(schar,"','" ;
strcat(schar,sch[3]);
strcat(schar,"','" ;
strcat(schar,sch[4]);
strcat(schar,"','" ;
strcat(schar,sch[5]);
strcat(schar,"',null" ;
strcat(schar," " ;
fprintf(stderr,schar);
savetodb(schar);
int jj;
for(jj=0;jj<6;jj++)
{
if ( NULL !=sch[jj] )
{
free(sch[jj]);
sch[jj] = NULL;
}
}
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,docname);
}
cur = cur->;next;
}
xmlFreeDoc(doc);
return;
}
int
main(int argc, char **argv) {
char *docname;
char *a;
char *b;
if (argc <= 1) {
printf("Usage: %s docname\n", argv[0]);
return(0);
}
docname = argv[1];
parseDoc (docname);
return (1);
}
int savetodb(char charsql[])
{
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;
}
else
{
fprintf(stderr,"Connection is well");
}
res=PQexec(conn,charsql);
if( PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr,"Exec Query Fauled!\n");
PQclear(res);
return 0;
}
PQfinish(conn);
PQclear(res);
return 0;
}
//所要解析的xml文件.0630.xml
<?xml version="1.0"?>;
<psml version="0" creator="ethereal/0.10.4">;
<structure>;
<section>;No.</section>;
<section>;Time</section>;
<section>;Source</section>;
<section>;Destination</section>;
<section> rotocol</section>;
<section>;Info</section>;
</structure>;
<packet>;
<section>;1</section>;
<section>;15:39:42.508837</section>;
<section>;61.172.247.211</section>;
<section>;178.100.101.106</section>;
<section>;TCP</section>;
<section>;7201 & 3234 [PSH, ACK] Seq=3005779493 Ack=1025335966 Win=65462 Len=6</section>;
</packet>;
<packet>;
<section>;2</section>;
<section>;15:39:42.625011</section>;
<section>;178.100.101.106</section>;
<section>;61.172.247.211</section>;
<section>;TCP</section>;
<section>;3234 & 7201 [ACK] Seq=1025335966 Ack=3005779499 Win=17018 Len=0</section>;
</packet>;
<packet>;
<section>;3</section>;
<section>;15:39:42.739142</section>;
<section>;61.172.247.211</section>;
<section>;178.100.101.106</section>;
<section>;TCP</section>;
<section>;7201 & 3234 [PSH, ACK] Seq=3005779499 Ack=1025335966 Win=65462 Len=24</section>;
</packet>;
</psml>;
//运行环境redhat9..数据库结构如下:
test=# \d packet_summary_tbl;
Table "public.packet_summary_tbl"
Column | Type | Modifiers
----------+---------------------------+-----------
p_id | character varying(30) |
p_no | integer |
p_time | time(6) without time zone |
src_ip | inet |
dest_ip | inet |
protocol | character varying(20) |
p_info | text |
p_memo | text |
/////我运行的错误为:
[postgres@zjhc411 libxml]$ ./parsepsml 0630.xml
1
1
15:39:42.508837
15:39:42.508837
61.172.247.211
61.172.247.211
178.100.101.106
178.100.101.106
TCP
TCP
7201 >; 3234 [PSH, ACK] Seq=3005779493 Ack=1025335966 Win=65462 Len=6
7201 >; 3234 [PSH, ACK] Seq=3005779493 Ack=1025335966 Win=65462 Len=6
1
15:39:42.508837
61.172.247.211
178.100.101.106
TCP
7201 >; 3234 [PSH, ACK] Seq=3005779493 Ack=1025335966 Win=65462 Len=6
段错误
[postgres@zjhc411 libxml]$
----------------只能解析xml文件中的第一个packet的数据.不知道是什么原因,是不是跟内存释放有关系???? |
|