How to sort an xml file or a text file using gtkmm?
This is the sample of the file that I want to sort.

<LIST>
<name>
Example
</name>
<country>
<name>
Country
</name>
<pop>
<desc>
Japan
</desc>
<uri>
100
</uri>
</pop>
<pop>
<desc>
China
</desc>
<uri>
108
</uri>
</pop>
<pop>
<desc>
Italy
</desc>
<uri>
101
</uri>
</pop>
<pop>
<desc>
Russia
</desc>
<uri>
744
</uri>
</pop>
</country>
</LIST>

Recommended Answers

All 9 Replies

you mean so that the result looks like this?

0000000000
1654
</contact>
</desc>
</desc>
</desc>
</desc>
</name>
</name>
</phonebook>
</pop>
</pop>
</pop>
</pop>
</uri>
</uri>
</uri>
</uri>
<contact>
<desc>
<desc>
<desc>
<desc>
<name>
<name>
<phonebook>
<pop>
<pop>
<pop>
<pop>
<uri>
<uri>
<uri>
<uri>
Contact
Example
Laptop
Phone
a
l.jl
m
sip:contact@minisip.org
Press any key to continue . . .

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
using namespace std;
std::string strings[] = {
    "<phonebook>",
    "<name>",
    "Example",
    "</name>",
    "<contact>",
    "<name>",
    "Contact",
    "</name>",
    "<pop>",
    "<desc>",
    "Phone",
    "</desc>",
    "<uri>",
    "0000000000",
    "</uri>",
    "</pop>",
    "<pop>",
    "<desc>",
    "Laptop",
    "</desc>",
    "<uri>",
    "sip:contact@minisip.org",
    "</uri>",
    "</pop>",
    "<pop>",
    "<desc>",
    "a",
    "</desc>",
    "<uri>",
    "1654",
    "</uri>",
    "</pop>",
    "<pop>",
    "<desc>",
    "m",
    "</desc>",
    "<uri>",
    "l.jl",
    "</uri>",
    "</pop>",
    "</contact>",
    "</phonebook>", 
};
const int sz = sizeof(strings) / sizeof(strings[0]);
int main()
{
    vector<string> theList;
    for(int i = 0; i < sz; i++)
        theList.push_back(strings[i]);
    std::sort(theList.begin(), theList.end());
    for(int i = 0; i < sz; i++)
        cout << theList[i] << "\n";
}

sorry posted on accident and i cant find out how to delete it

I assume you want to sort the data that is contained in the XML file, and not the XML file itself.

Just sorting the XML makes no sense, since what you get is no longer XML.

HunterFish has no idea about Text or XML file. Thanks to Ancient Dragon; Dashing code, Indeed.

well he asking how to do this with a C++ IDE so thats one question down.

But Hunter Fish i just finished off an assignment that needs to do this kind of problem how i solved was using a Stack to store all opening tags

Then compared all closing tag with the top item in the stack.

This program was to evalute if the XML tags where formated correctly. is this what your after?

Member Avatar for iamthwee

Go for an xml parser such as tinyXml etc.

Thank you guys for the help.

I want to sort it by description(<desc> </desc>). Like this one.

<phonebook>
<name>
Example
</name>
<contact>
<name>
Contact
</name>
<pop>
<desc>
a
</desc>
<uri>
1654
</uri>


</pop>

<pop>
<desc>
Laptop
</desc>
<uri>
sip:contact@minisip.org
</uri>
</pop>

<pop>
<desc>
m
</desc>
<uri>
l.jl
</uri>
</pop>

<pop>
<desc>
Phone
</desc>
<uri>
0000000000
</uri>
</pop>

</contact>
</phonebook>

As you can see the value of the <desc> <desc> was the only data that was sorted. How can I able to manage to sort it without sorting the other text that contained the file.

I would create a structure like this:

struct data
{
    std::vector<string> items;
    std::string sortfield;
};

now create a vector of those items: vector<data> theList; Next read the file into the vector theList. Then you can easily sort the structures by any html tag you want, and finally write out the vectors back to a data file.

can you give some samples?

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.