Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6201849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:37:46+00:00 2026-05-24T04:37:46+00:00

My objective is to read an XML text file and split each word and

  • 0

My objective is to read an XML text file and split each word and tag into there own line in an array.

For example, if I input this text into my program:

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

I would get this:

<note>
<to>
Tove
</to>
<from>
...

Right now I have code that can successfully do this but only with the words so instead of the above list I get:

note
to
Tove
...

I want to keep the tags or I wont be able to do what I want with it. So I have been trying to get it to also add the tags but have been failing

Okay so here is my code:

//While the file is not empty
while(fgets(buffer, sizeof(buffer), stdin) != NULL){
    int first = 0;
    int last = 0;

    //While words are left in line
    while(last < INITIAL_SIZE && buffer[last] != '\0'){
        int bool = 0;
        //Tag detected
        if(buffer[last] == '<'){
            while(buffer[last] != '>'){
                last++;
            }

            bool = 1;
        }else{
            //While more chars are in the word
            while(last < INITIAL_SIZE && isalpha(buffer[last])){
                last++;
            }
        }
        //Word detected
        if(first < last){
            //Words array is full, add more space
            if(numOfWords == sizeOfWords){
                sizeOfWords = sizeOfWords + 10;
                words = (char **) realloc(words, sizeOfWords*sizeof(char *));
            }               
            //Allocate memory for array
            words[numOfWords] = (char *) calloc(last-first+1, sizeof(char));


            for(i = 0; i < (last-first); i++){
                words[numOfWords][i] = buffer[first + i];
            }
            //Add terminator to "new word"
            words[numOfWords][i] = '\0';
            numOfWords++;   
        }           
        //Move "Array Pointers" accordingly
            last++;
            first = last;
    }       
}

Any one have any idea, with the above code this is the printout:

<note
<to
Tove
to 
<from
Jani
from
<heading
...
Don
t
forget
me
this
weekend
</body
</note

So after this wall of text, does anyone have any idea on how I can modify my current code to get this to work? Or does anyone else have an alternative?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:37:47+00:00Added an answer on May 24, 2026 at 4:37 am

    Even though it is highly doubtful that anyone would ever use this I got it to work by using Boolean type logic.

    while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
        int first = 0;
        int last = 0;
    
        //While words are left in line
        while (last < INITIAL_SIZE && buffer[last] != '\0' && buffer[last] != '\n'){
            int Bool = 0;
            //Tag detected
            if (buffer[last] == '<'){
                while (buffer[last] != '>')
                    last++;
                Bool = 1;
            }else
                //While more chars are in the word
                while(last < INITIAL_SIZE && !isspace(buffer[last]) && buffer[last] != '<')
                    last++;
    
            //Word detected
            if (first < last) {
                //Words array is full, add more space
                if (numOfWords == sizeOfWords) {
                    sizeOfWords = sizeOfWords + 10;
                    words = (char **) realloc(words, sizeOfWords * sizeof(char *));
                }
                //Allocate memory for array
                words[numOfWords] = (char *) calloc(last - first + 1, sizeof(char));
    
                int xHolder = 0;
                if(buffer[first] == '/'){
                    words[numOfWords][0] = '<';
                    xHolder++;
                    Bool++;
                }
                for (i = 0; i < (last - first + Bool); i++) {
                    words[numOfWords][xHolder] = buffer[first + i];
                    xHolder++;
                }
                //Add terminator to "new word"
                words[numOfWords][i] = '\0';
                numOfWords++;
            }
            //Move "Array Pointers" accordingly
            last++;
            first = last;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

any recommendations for saving four strings into an own file format that is read
I have a question about writing your own init methods in objective-c. I've read
I'm learning objective-c and keep bumping into the @ symbol. It is used in
I have several XML files that I wish to read attributes from. My main
Is there anyway to do Files Handling in Objective-C? I am just trying to
I can directly read the XML Feed using jQuery, but when I display the
So I read the documentation about including an external Javascript file directly in the
Having recently read up a on Objective-C it strikes me as a fairly neat
I am newbie in Objective. As I read many tutorial, @property has a variable
I'm new to objective-c, for an academic reason I need to read CPU speed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.