Hello how can I read xml file without using xml librariy in C++

here

http://www.cplusplus.com/forum/general/140931/

Recommended Answers

All 8 Replies

Without using a library, you're simply going to have to do it yourself.

Frankly, you need to start by thinking rather than coding. You can read the xml file easily. Open it, read in data. But that just gets you a lot of strings. How do you intend to organise what you read?

I want to put that xml file record in a struct and ignore the tags

So design the struct first. Then code up the struct. Write code to create a struct, and code to put data into a struct. By the time you've done that, you'll have almost solved your problem.

If you know for sure that there will only be one <address book>, and it will only contain <contact>, and each <contacty> will have the same fields, it becomes quite simple.

#include<cctype>
#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;

const unsigned short int numbersAdd=100;
const unsigned short int chr_len=30;

struct address{
    char name[chr_len];
    char street[chr_len];
    char state[chr_len];
    int post;

    };

void readFile(char[], ifstream&);
address records[numbersAdd];
int main(){
    char xml[30];

    cout<<"Anter file name ";
    cin.getline(xml, 30);
    ifstream read;
    readFile(xml, read);
    return 0;
    }



void readFile(char file[], ifstream& read){
    char line[chr_len];

    read.open(file, ios::in);
    unsigned short int i=0;

    if(read.fail())
        cout<<"Sorry could not read";

    else{

        while (read.good()){
            read>>line;//what to do here?
                        //how to put in struct


            }

        }
    read.close();

    }
/*int checkXtension(char xml[]){
    int len=0;
    len=strlen(xml);
    if(xml[len-1]=='l' && xml[(len-1)-1]=='m' && xml[(len-1)-2]=='x' && xml[(len-1)-3]=='.' )
        return 1;

    else
        return 0;

    }

void displayAddress(char[], address record[]){

}

void displayAddresses(int number1, int number2, address record[]){


}*/

what to put there? There you put code to analyse the line and do something with whatever you find there.

cant figure out that...trying from strtok to strcmp to ignore..nothings working :(

strstr

The thing about xml is that each tag doesn't have to be on a separate line - an entire file can be one line. You have to parse it directly. You can either do this yourself, or use a library such as xerxes. I have done it both ways. Generally, for really high speed or high volume needs, I roll my own parser and generic structures to model the xml found.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.