Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i have used belowpiece of code it is reading only xml data present inside XML files.but i am unable to run by giving like the following(XML String):
<note date="07/22/2011"><to>John Doe</to><from>Dave Smith</from><body>Still up for lunch?</body></note>

C#
#include <stdio.h>
#include <libxml/xmlreader.h>
 
int main(int argc, char **argv)
{
    if(argc != 2)
        return (0);
 
    const char *fileName = argv[1];
 
    xmlTextReaderPtr reader;
 
    reader = xmlReaderForFile(fileName, NULL, 0);
 
    const char *temp;
 
    int i;
 
    while(xmlTextReaderRead(reader)) {
        switch(xmlTextReaderNodeType(reader)) {
            case XML_READER_TYPE_ELEMENT:
 
            for(i = 0 ; i < xmlTextReaderDepth(reader) ; i++)
                printf("\t");
 
            temp = (char *)xmlTextReaderConstName(reader);
 
            printf("Element: %s", temp);
 
            while(xmlTextReaderMoveToNextAttribute(reader)) {
                temp = (char *)xmlTextReaderConstName(reader);
                printf("  %s", temp);
 
                temp = (char *)xmlTextReaderConstValue(reader);
                printf("=\"%s\"", temp);
            }
 
            xmlTextReaderMoveToElement(reader);
 
            printf("\n");
 
            continue;
 
        case XML_READER_TYPE_TEXT:
            temp = (char *)xmlTextReaderConstValue(reader);
 
            for(i = 0 ; i < xmlTextReaderDepth(reader) ; i++)
                printf("\t");
 
            printf("\t%s", temp);
 
            printf("\n");
 
            continue;
        }
    }
 
    xmlFreeTextReader(reader);
    xmlCleanupParser();
 
    return 0;
}
Posted
Updated 27-Mar-12 3:43am
v2

1 solution

Well of course not!

It does say xmlReaderForFile after all, doesn't it?

Why aren't you just using xmlReaderForMemory[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Mar-12 19:33pm    
Up-voted with my 5.
I had to remove my answer. I certainly failed to see the question was about C, thanks for your note.
--SA
enhzflep 27-Mar-12 19:35pm    
Okay, that's very kind of you.
Only problem is I can't vote for your answer. There were some good points contained within, but alas - it is all lost to the ether!
Best regards.. :)
Sergey Alexandrovich Kryukov 27-Mar-12 22:57pm    
Nothing to worry about...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900