CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 4 FirstFirst 1234 LastLast
Results 31 to 45 of 50

Thread: .xml to .txt

  1. #31
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: .xml to .txt

    Is this a school assignment? What if you used a properly licensed third party library that only depends on the standard library?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  2. #32
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: .xml to .txt

    Is this a school assignment?
    It is assignment but not school assignment.
    What if you used a properly licensed third party library that only depends on the standard library?
    which third party library?

  3. #33
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: .xml to .txt

    Quote Originally Posted by flex567 View Post
    On this picture there are both files. One with 70000 lines and another file with the same amount of data in one line. I think it would be easier to parse the file with 70000 lines.
    Am I correct in thinking that the top picture shows the data file as you obtain it and that from this data as shown in the top picture you want to extract the date, currency name and value?

    If this is the case then you might as well just extract what you want from the one (rather large!) line in the file rather than parsing it first to insert line breaks and then parsing this to extract the data.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #34
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: .xml to .txt

    Am I correct in thinking that the top picture shows the data file as you obtain it
    yes
    this data as shown in the top picture you want to extract the date, currency name and value?
    just values between dates
    rather than parsing it first to insert line breaks and then parsing this to extract the data.
    I didn't parse it for line breaks , the firefox and .xml convertet did it.

  5. #35
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: .xml to .txt

    This should take no more than a few hours to do. You should probably stop hanging around here and get it done. Parsing it as a single line would be trivial.

  6. #36
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: .xml to .txt

    Given the data file as in the top picture, this code is one simple way of extracting the data as the basis for further development.
    Code:
    #include <string>
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    const string xmlname = "bsidata.xml";
    
    bool nlerase(string& st)
    {
    size_t pos;
    
    	while ((pos = st.find('\n')) != string::npos)
    		st.erase(pos, 1);
    
    	return true;
    }
    
    int main()
    {
    ifstream ifs(xmlname.c_str());
    
    	if (!ifs.is_open()) {
    		cout << "Cannot open input file " << xmlname << endl;
    		return 1;
    	}
    
    string date;
    
    	do {
    		string line;
    		size_t spos;
    
    		while (getline(ifs, line, '>') && nlerase(line) && ((spos = line.find("<tecaj")) == string::npos));
    		size_t pos;
    
    		if (ifs.good())
    			if ((pos = line.find("datum", spos + 6)) != string::npos) 
    				date = line.substr(line.find("\"", pos) + 1, 10);
    			else
    				if ((pos = line.find("oznaka", spos + 6)) != string::npos)  {
    					double val;
    					string cur = line.substr(line.find("\"", pos) + 1, 3);
    					ifs >> val;
    
    					cout << date << " " << cur << " " << val << endl;
    				}
    	} while (ifs.good());
    
    	return 0;
    }
    It should extract the data with/without line breaks.

    The cout statement can be replaced with whatever processing is required on the extracted data.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #37
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: .xml to .txt

    I already wrote the program.
    It is pretty much done.

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
    
    using namespace std;
    
    
    int main(){
    
        ifstream in("from_xmlF.txt");
        vector<string> results;
        vector<double> temp_results;
        string line, start_date = R"(datum="2009-07-07">)",
        end_date = R"(datum="2009-07-10">)", currency = "JPY";
    
    
        size_t found;
        string test;
    
        if(in){
    
            while(getline(in,line)){
    
                if(line.find(start_date) != string::npos){
    
                    while(getline(in,line) && (line.find(end_date) == string::npos)){
    
                        if(line.find(currency) != string::npos){
                            results.push_back(line);
                        }
                    }
                }
            }
        }
        else{
            cout << "Failed to open" << endl;
        }
    
        // Extract the results from the line with currency to string and then to vector<double>
            string temp;
            for(const auto &i : results){
                temp_results.push_back(stod(i.substr(36,10)));
        }
    
        // Calculate the final result
            double tempD = temp_results[0] ;
            for(auto &i : temp_results){
                i = i - tempD;
            }
    
            for(const auto &i : temp_results){
                cout << i << endl;
            }
    
    
    
        return 0;

  8. #38
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: .xml to .txt

    Quote Originally Posted by flex567 View Post
    I already wrote the program.
    It is pretty much done.
    Good luck!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #39
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: .xml to .txt

    Quote Originally Posted by flex567 View Post
    It is assignment but not school assignment.
    which third party library?
    I usually just use tinyxml (http://www.grinninglizard.com/tinyxml/). It is a very lightweight, easy to install and very simple to use. It doesn't have a ton of features: you just give it an xml document, and it gives you the DOM. Nothing more, nothing less.

    Unless your XML is about 300M or more, it will get the job done.

    It is quite the small library too (about 10K lines). It hasn't been updated since 2011, but arguably, that's an argument showing it is bug free.

    Alternatively, if you know you have a valid document, you could try implementing a (simple) SAX-style parser (http://en.wikipedia.org/wiki/Simple_API_for_XML)? This would be arguably easier (on the parser side), as all you have to identify are pairs of "<>" pairs, and then call the corresponding callbacks.

    But to be honest, writing an xml parser is something you don't want to do yourself.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  10. #40
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: .xml to .txt

    Quote Originally Posted by flex567 View Post
    I already wrote the program.
    It is pretty much done.
    Except that...
    It doesn't work
    unless you change the program for every new file you receive, since you hardcoded a date.
    a date which doesn't even match the date in the sample fiel in your OP.
    you're assuming the xml is formatted in a line by line approach, which isn't necessarily the case.

    If your teacher has given you a file where it isn't in that format, which your OP seems to allude to,
    then manually/programmaticlaly changing the file to that format so your work gets easier may not be the thing you were supposed to be doing at all.

    If you were given a file X, then I would assume it's your task to do the given job on THAT file and not on a file that was conveniently modified to a format you could work with.
    Because... i fyou ARE allowed to do that.
    why not simply change the xml into plain tekst approach that makes it easy to use
    file >> value >> currency;

    Changing the parameters of the task is NOT an acceptable solution.

  11. #41
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: .xml to .txt

    Quote Originally Posted by laserlight View Post
    Is this a school assignment? What if you used a properly licensed third party library that only depends on the standard library?
    Maybe the idea was to actually write a simple xml parser that will handle xml and have methods to programmatically query the parsed xml tree. Provided it doesn't contain some of the more complex issues of xml (namespaces, custom entities, DTD's, character set encodings, ...) this is very doable (been there, done that, takes about 1000 lines of C++ code), and I would even see that as an exellent exercise (semi advanced level) on here's your simple attempt, and here's why even that will fail, and why you should be using a lib.

    I have needed to write my own this since a lib was out due to space restrictions (needed to work on an embedded device with 64K) and I only needed to read and write simple xml with no fancy stuff.
    If anything, it has given me an even deeper respect for the people that get fully qualifying XML parsers going WITH all those fancy features.

  12. #42
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: .xml to .txt

    Hopefully this is all just a school assignment because writing an xml parser for a work assignment would be a waste of time.

    Sure, I guess there could be some edge case scenarios working on some restricted platforms and compilers where you would need to do it, but otherwise why reinvent the wheel?

  13. #43
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: .xml to .txt

    Quote Originally Posted by Arjay
    Hopefully this is all just a school assignment because writing an xml parser for a work assignment would be a waste of time.
    Your hopes have been partially dashed:
    Quote Originally Posted by flex567 View Post
    It is assignment but not school assignment.
    Only partially though, because it might be a personally assigned exercise from a book or something rather than school or work.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #44
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: .xml to .txt

    A relevant "Most Interesting Man in the World" quote:

    "I don't often write an xml parser, but when I do, I like to start from scratch!"

  15. #45
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: .xml to .txt

    It doesn't work
    Why do you think that doesn't work, It is easily crack-able but my intention was to just write quickly a program that works .

Page 3 of 4 FirstFirst 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured