LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-08-2011, 06:46 AM   #1
shankar.489
Member
 
Registered: Jan 2011
Posts: 53

Rep: Reputation: 0
Unhappy 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
 
Old 03-08-2011, 08:21 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,712

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by shankar.489 View Post
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
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.
 
1 members found this post helpful.
Old 03-08-2011, 10:40 PM   #3
shankar.489
Member
 
Registered: Jan 2011
Posts: 53

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by TB0ne View Post
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
 
Old 03-09-2011, 12:54 AM   #4
shankar.489
Member
 
Registered: Jan 2011
Posts: 53

Original Poster
Rep: Reputation: 0
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

Last edited by shankar.489; 03-10-2011 at 04:18 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Is *libqt4-xml* equivalent of *libxml2* with respect to *XML* parsing ? Aquarius_Girl Linux - Software 1 11-25-2010 07:34 PM
g++ Atom/RSS XML parsing library marf Linux - General 1 05-24-2008 01:39 PM
Porting to Fedora gcc 4.0 -parsing backward slash("\") comma(",") code_blew Programming 1 04-26-2006 04:07 PM
Standard XML parsing library? pembo13 Programming 3 01-27-2006 11:45 AM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration