c programming with xml


 
Thread Tools Search this Thread
Top Forums Programming c programming with xml
# 1  
Old 10-29-2004
c programming with xml

Hi all,
I am suppose to write a program that will take in a xml file and display the value of each attribute that is in the xml file. i have some code but i didn't know where should some of the file be written.

Can some one help me?

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

// #include "parsedoc.h"

void
getReference (xmlDocPtr doc, xmlNodePtr cur) {

  xmlChar *uri;
  cur = cur->xmlChildrenNode;
  while (cur != NULL) {
    if ((!xmlStrcmp(cur->name, (const xmlChar *)"reference"))) {
      uri = xmlGetProp(cur, "uri");
      printf("uri: %s\n", uri);
      xmlFree(uri);
    }
    cur = cur->next;
  }
  return;
}


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

  char *docname;
  xmlDocPtr doc;

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

  docname = argv[1];
  doc = parseDoc (docname);
  if ( doc != NULL ) {
    xmlNodePtr cur;
    cur = xmlDocGetRootElement(doc);
    getReference (doc, cur);
    xmlFreeDoc(doc);
  }

  return (1);
}

I didn't know how to continue from here.
Give me some ideas or clue if possible.
Or you can msn me dianazheng@hotmail.com.
# 2  
Old 10-29-2004
Seems this code is using libxml2 tool. So you need to download and install libxml2 into your OS, then compile your code.

Normally there will be some problem when you install libxml2. But hopefully you could get lucky.
# 3  
Old 10-29-2004
Do you have any examples that is close to what i am trying to do now, or would you mind giving me some examples.

Thanks in advance..
# 4  
Old 10-30-2004
I need HELP!!!!

When i run my C programe with a makefile that i have created, it give me this error message, can anyone tell me what is the meaning of
undefined reference to `xmlStrcmp'


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

// search for and print out text associated with "keyword" tag
void
//parseStory (xmlDocPtr doc, xmlNodePtr cur) {
parseFile (xmlDocPtr doc, xmlNodePtr cur) {

  xmlChar *key;
 
  // look for "keyword" tag among storyinfo's children
  cur = cur->xmlChildrenNode;
  while (cur != NULL) {
    
    if ((!xmlStrcmp(cur->name, (const xmlChar *) "name"))) {
      key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
      printf("Name: %s\n", key);
      //printf("keyword: %s\n", key);
      xmlFree(key);
    }
    
    if ((!xmlStrcmp(cur->name, (const xmlChar *) "description"))) {
      key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
      printf("Description: %s\n", key);
      //printf("keyword: %s\n", key);
      xmlFree(key);
    }
    
    if ((!xmlStrcmp(cur->name, (const xmlChar *) "exits"))) {
      key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
      printf("Exits: %s\n", key);
      //printf("keyword: %s\n", key);
      xmlFree(key);
    }
    cur = cur->next;
  }
  return;
}

static void
parseDoc(char *docname) {

  xmlDocPtr doc;
  xmlNodePtr cur;
  //char *cur;           //add

  doc = xmlParseFile(docname);

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

  cur = xmlDocGetRootElement(doc);

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

  // root tag must be "story"
  if (!xmlStrcmp(cur->name, (const xmlChar *) "gameworld")) {
    fprintf(stderr,"Document of the wrong type, root node != gameworld");
    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);

  return (1);
}

The error message
Code:
$ make task2a
cc `xml2-config --cflags` -c task2a.c
cc task2a.o -o readworld1 `xml2-config --libs`
cc   task2a.o readworld1   -o task2a
collect2: ld terminated with signal 11 [Segmentation fault]
readworld1(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/gcc-lib/i486-linux/3.3.4/../../../crt1.o(.rodata+0x0):../sysdeps/i386/elf/start.S:47: first defined here
readworld1(.data+0x4): In function `__data_start':
: multiple definition of `__dso_handle'
/usr/lib/gcc-lib/i486-linux/3.3.4/crtbegin.o(.data+0x0): first defined here
readworld1(.init+0x0): In function `_init':
/disk/hdc2/glibc/debian-build/glibc_2.3.2.ds1-13.test3/glibc-2.3.2.ds1/build-tree/i386-libc/csu/crti.S:35: multiple definition of `_init'
/usr/lib/gcc-lib/i486-linux/3.3.4/../../../crti.o(.init+0x0):/disk/hdc2/glibc/debian-build/glibc_2.3.2.ds1-13.test3/glibc-2.3.2.ds1/build-tree/i386-libc/csu/crti.S:12: first defined here
readworld1(.text+0x0): In function `_start':
../sysdeps/i386/elf/start.S:47: multiple definition of `_start'
/usr/lib/gcc-lib/i486-linux/3.3.4/../../../crt1.o(.text+0x0):../sysdeps/i386/elf/start.S:47: first defined here
readworld1(.text+0x29f): In function `main':
: multiple definition of `main'
task2a.o(.text+0x1db): first defined here
readworld1(.fini+0x0): In function `_fini':
/disk/hdc2/glibc/debian-build/glibc_2.3.2.ds1-13.test3/glibc-2.3.2.ds1/build-tree/i386-libc/csu/crti.S:51: multiple definition of `_fini'
/usr/lib/gcc-lib/i486-linux/3.3.4/../../../crti.o(.fini+0x0): first defined here
readworld1(*ABS*+0x8049bf4): In function `__init_array_start':
task2a.c: multiple definition of `_GLOBAL_OFFSET_TABLE_'
/usr/lib/gcc-lib/i486-linux/3.3.4/../../../crt1.o(.got.plt+0x0):../sysdeps/i386/elf/start.S:47: first defined here
readworld1(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/gcc-lib/i486-linux/3.3.4/../../../crt1.o(.rodata+0x4):../sysdeps/i386/elf/start.S:53: first defined here
readworld1(.data+0x0): In function `__data_start':
: multiple definition of `__data_start'
/usr/lib/gcc-lib/i486-linux/3.3.4/../../../crt1.o(.data+0x0):../sysdeps/i386/elf/start.S:47: first defined here
readworld1(.text+0xc4): In function `parseFile':
: multiple definition of `parseFile'
task2a.o(.text+0x0): first defined here
/usr/lib/gcc-lib/i486-linux/3.3.4/../../../crt1.o(.dynamic+0x0):../sysdeps/i386/elf/start.S:47: multiple definition of `_DYNAMIC'
task2a.o(.text+0x2c): In function `parseFile':
: undefined reference to `xmlStrcmp'
task2a.o(.text+0x4d): In function `parseFile':
: undefined reference to `xmlNodeListGetString'
task2a.o(.text+0x86): In function `parseFile':
: undefined reference to `xmlStrcmp'
task2a.o(.text+0xa7): In function `parseFile':
: undefined reference to `xmlNodeListGetString'
task2a.o(.text+0xe0): In function `parseFile':
: undefined reference to `xmlStrcmp'
task2a.o(.text+0x101): In function `parseFile':
: undefined reference to `xmlNodeListGetString'
task2a.o(.text+0x145): In function `parseDoc':
: undefined reference to `xmlParseFile'
task2a.o(.text+0x170): In function `parseDoc':
: undefined reference to `xmlDocGetRootElement'
task2a.o(.text+0x199): In function `parseDoc':
: undefined reference to `xmlFreeDoc'
task2a.o(.text+0x1b1): In function `parseDoc':
: undefined reference to `xmlStrcmp'
task2a.o(.text+0x1d5): In function `parseDoc':
: undefined reference to `xmlFreeDoc'
make: *** [task2a] Error 1

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pull multiple XML tags from the same XML file in Shell.?

I'm searching for the names of a TV show in the XML file I've attached at the end of this post. What I'm trying to do now is pull out/list the data from each of the <SeriesName> tags throughout the document. Currently, I'm only able to get data the first instance of that XML field using the... (9 Replies)
Discussion started by: hungryd
9 Replies

2. Shell Programming and Scripting

Splitting a single xml file into multiple xml files

Hi, I'm having a xml file with multiple xml header. so i want to split the file into multiple files. Sample.xml consists multiple headers so how can we split these multiple headers into multiple files in unix. eg : <?xml version="1.0" encoding="UTF-8"?> <ml:individual... (3 Replies)
Discussion started by: Narendra921631
3 Replies

3. Shell Programming and Scripting

Split xml file into multiple xml based on letterID

Hi All, We need to split a large xml into multiple valid xml with same header(2lines) and footer(last line) for N number of letterId. In the example below we have first 2 lines as header and last line as footer.(They need to be in each split xml file) Header: <?xml version="1.0"... (5 Replies)
Discussion started by: vx04
5 Replies

4. UNIX for Dummies Questions & Answers

From iOS programming to Linux system programming

Hello. I like Linux and C programming language. Allways wanted to understand kernel and become a Linux system programmer. And I also like Objective-C and iOS. These two programming areas have relations: 1. Linux and iOS are UNIX-like systems, POSIX compliant. 2. It is useful to know C language... (2 Replies)
Discussion started by: Rockatansky
2 Replies

5. Shell Programming and Scripting

How to add Xml tags to an existing xml using shell or awk?

Hi , I have a below xml: <ns:Body> <ns:result> <Date Month="June" Day="Monday:/> </ns:result> </ns:Body> i have a lookup abc.txtt text file with below details Month June July August Day Monday Tuesday Wednesday I need a output xml with below tags <ns:Body> <ns:result>... (2 Replies)
Discussion started by: Nevergivup
2 Replies

6. Shell Programming and Scripting

Shell Command to compare two xml lines while ignoring xml tags

I've got two different files and want to compare them. File 1 : HTML Code: <response ticketId="944" type="getQueryResults"><status>COMPLETE</status><description>Query results fetched successfully</description><recordSet totalCount="1" type="sms_records"><record... (1 Reply)
Discussion started by: Shaishav Shah
1 Replies

7. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies

8. Programming

xml and c programming for unix

Does anyone can tell me what is the best way to post a xml request to a web service on C. I am using expat for parsing the response, but until now i build my xml request with a bunch of strcat functions, then connect to the port and send it that way. I am wondering if there are libraries or a... (3 Replies)
Discussion started by: loquito
3 Replies

9. UNIX for Dummies Questions & Answers

Carreer:Networking Programming in Unix (C programming Language)

Hello, I am trying to learn Networking Programming in C in unix enviorment. I want to know how good it is to become a network programmer. i am crazy about Network programming but i also want to opt for the best carreer options. Anybody experienced Network Programmer, please tell me is my... (5 Replies)
Discussion started by: vibhory2j
5 Replies
Login or Register to Ask a Question