SUCCESS...almost

so i got my xml changing program to work finally but i had to scrap part of it.
now i would like some guidence if any one has the time.

i got this program to find a specific tag in an xml file and change the information between the tag---

<me>cat</me>--- to ---<me>woot</me>

but with xml there can be multiple tags named <me>,for example. in my old program i also had it search for the current information and tag so i always had the right tag set then it changed the old info to the new user input.

i need help changeing the code to search for the right "tag set" and a way to include spaces in the new information so i can output a line of text and not just one word.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
    string tag, mess, info, file;
    int i,j,n,m,x,c,d;
    char a[100], b[500];
    cout << "Enter tag: ";
    cin >> tag;
    cout << "Enter current info: ";
    cin >> info;
    cout << "Enter new info: ";
    cin >> mess;
    ifstream myfile;
    myfile.open("example.xml", ios::in | ios::out);
    for(j =0; !myfile.eof(); j++)
    {
            b[j] = myfile.get();
    }
    myfile.close();
    file = b;
    n = tag.size() + 2;
    i = file.find("<" + tag + ">");
    m = file.find("</" + tag + ">");
    file.replace(i + n,m-i-n, mess);
    j = file.size();
    while(file[j] != '>')
    {             
                  file.erase(j);
                  j--;
    }
    ofstream mydoc;
    mydoc.open("example.xml");
    mydoc << file;
    mydoc.close();
    system("pause");
    return 0;
}


thanks for the help...
I don't understand the question. Here's a little rewrite of your program. I removed the latter loop as it didn't seem to be doing anything useful (and has an out-of-bound access).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

int main()
{
    string tag, infoOld, infoNew;
    cout << "Enter tag: ";
    getline(cin, tag);
    cout << "Enter current info: ";
    getline(cin, infoOld);
    cout << "Enter new info: ";
    getline(cin, infoNew);

    string file;
    char c;
    ifstream myfile("example.xml");
    while(myfile.get(c)) file.push_back(c);
    myfile.close();

    int n = tag.size() + 2;
    int i = file.find("<" + tag + ">");
    int m = file.find("</" + tag + ">");
    file.replace(i + n, m-i-n, infoNew);

    ofstream mydoc("example.xml");
    mydoc << file;
    mydoc.close();
}

the loop was for removing any excess char at the end of the file so that it will run. i kept getting extra char's at the end of the file.
<? xml version="1.0" ?>
<kyle>
<me>woot</me>
<me>what</me>
<me>were</me>
</kyle>

i needed to find a way to search for the '<me>' ? '</me>' so i can change its info and not the the others.

by the way what does this do?
while(myfile.get(c)) file.push_back(c);

Last edited on
So before replacing the substring found between the start and end tags, you want to compare it to the given old value. If it is not equal, you must loop back and find the next tag match, which means you must start after this one.

The while loop you refer to reads myfile char-by-char until EOF, appending each char read to the string called file.
thanks for explaining the loop hammurabi
and what you said in your first paragraph is correct but to add on to it, when it does find the correct information then it switchs out the old with the new.
So you want something like this after myfile.close();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    size_t tagSize = tag.size() + 2, start = 0, end = 0;
    while (true)
    {
        start = file.find ("<" + tag + ">", end);
        if (start == -1) break;
        end = file.find ("</" + tag + ">", start);
        if (end == -1) break;
        if (file.substr (start+tagSize, end-start-tagSize) == infoOld)
        {
            file.replace (start+tagSize, end-start-tagSize, infoNew);
            ofstream mydoc ("example.xml");
            mydoc << file;
            mydoc.close();
        }
    }

yes it worked thanks guys
I forgot to put a break; right after mydoc.close(); (within that block).
Topic archived. No new replies allowed.