954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to sort an xml file or a text file?

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


Example

Country

Japan

100

China

108

Italy

101

Russia

744

HunterFish
Newbie Poster
5 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

you mean so that the result looks like this?
0000000000
1654


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";
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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?

Streel
Newbie Poster
1 post since May 2009
Reputation Points: 10
Solved Threads: 0
 

Go for an xml parser such as tinyXml etc.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Thank you guys for the help.

I want to sort it by description(). Like this one.


Example

Contact

a

1654

Laptop

sip:contact@minisip.org

m

l.jl

Phone

0000000000

As you can see the value of the 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.

HunterFish
Newbie Poster
5 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

can you give some samples?

HunterFish
Newbie Poster
5 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You