LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Xml parsing using "libxml2.so" library (https://www.linuxquestions.org/questions/linux-newbie-8/xml-parsing-using-libxml2-so-library-867191/)

shankar.489 03-08-2011 06:46 AM

Xml parsing using "libxml2.so" library
 
hi Folks,

i need a sample xml parser program which parses given xml file and prints its contents

iam very new to this area and iam using libxml2.so library

I wish that provided example should be in C only

thanks for your help in adv

~shankar:study:

TB0ne 03-08-2011 08:21 AM

Quote:

Originally Posted by shankar.489 (Post 4282661)
hi Folks,

i need a sample xml parser program which parses given xml file and prints its contents
iam very new to this area and iam using libxml2.so library
I wish that provided example should be in C only
thanks for your help in adv
~shankar:study:

Did you even try to look this up?? First hit in Google, is from the XML site:
http://www.xmlsoft.org/examples/index.html

complete with examples. Also, asking a question, and telling the people who volunteer what KIND of answer you want is a bit rude.

shankar.489 03-08-2011 10:40 PM

Quote:

Originally Posted by TB0ne (Post 4282744)
Did you even try to look this up?? First hit in Google, is from the XML site:
http://www.xmlsoft.org/examples/index.html

complete with examples. Also, asking a question, and telling the people who volunteer what KIND of answer you want is a bit rude.

thanks for for kind way of conveying my faults, i will try to never repeat this flaw again

shankar.489 03-09-2011 12:54 AM

hi Folks,

i have done upto this in xml file reader and i need your help inorder to move further

my sample xmlfile is like this

<breakfast_menu>
-
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
-
<description>
two of our famous Belgian Waffles with plenty of real maple syrup
</description>
<calories>650</calories>
</food>
-
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
-
<description>
light Belgian waffles covered with strawberries and whipped cream
</description>
<calories>900</calories>
</food>
</breakfast_menu>

myxmlparsercode

static void processNode(xmlTextReaderPtr reader)
{
const xmlChar *name, *value;

name = xmlTextReaderConstName(reader);
if (name == NULL)
name = BAD_CAST "--";

value = xmlTextReaderConstValue(reader);

printf("name1:%s",name);
if (value == NULL)
printf("\n");
else
{
if (xmlStrlen(value) > 40)
printf(" %.40s...\n", value);
else
printf("value1:%s\n", value);
}
}

static void streamFile(const char *filename)
{
xmlTextReaderPtr reader;
int ret;

reader = xmlReaderForFile(filename, NULL, 0);
if (reader != NULL) {
ret = xmlTextReaderRead(reader);
while (ret == 1) {
processNode(reader);
ret = xmlTextReaderRead(reader);
}
xmlFreeTextReader(reader);
if (ret != 0) {
fprintf(stderr, "%s : failed to parse\n", filename);
}
} else {
fprintf(stderr, "Unable to open %s\n", filename);
}
}

int main(int argc, char **argv) {
if (argc != 2)
return(1);

LIBXML_TEST_VERSION

streamFile(argv[1]);

xmlCleanupParser();

xmlMemoryDump();
return(0);
}

output.txt


name1:breakfast_menu
name1:#textvalue1:
-

name1:food
name1:#textvalue1:

name1:name
name1:#textvalue1:Belgian Waffles
name1:name
name1:#textvalue1:

name1: price
name1:#textvalue1:$5.95
name1: price
name1:#textvalue1:
-

name1:description
name1:#text
two of our famous Belgian Waffles with ...
name1:description
name1:#textvalue1:

name1:calories
name1:#textvalue1:650
name1:calories
name1:#textvalue1:

name1:food
name1:#textvalue1:


i expect my output to be like this
:

breakfast_menu
name:food
price:5.95
Description: XXXXXXXXXXXXXXXXXX
Calories: 20
.
.

i found this sample code from internet. its parsing my XML file well but i dont know how to eleminate unecessary string "#text"

why this comming to me..!?

how can i eleminate this ?
or
what are the change i can made to my program to get ecpected output..!

please do reminded that "name1","value1" strings are printed by me i can eliminate those

all your comments/modifications/suggestions are greatly accepted.

please help iam newbiee in this parsing

~shankar


All times are GMT -5. The time now is 06:18 AM.