How can I feed data via loop into an XML file from an txt file in C++?



I am trying to feed some data from a basic txt file into an XML file via C++. I understand how to write the tags, but how do I alternate from hard-coding it to adding some dynamic list? I am using this class to help me.



#include <fstream>
#include <iostream>
#include <string>
#include "xmlwriter.h"

using namespace std;
using namespace xmlw;

int main()
{
ofstream f("output.xml");
XmlStream xml(f);

xml << prolog() // write XML file declaration

<< tag("blocks")
<< attr("version") << "1"
<< attr("app") << "Snap! 4.0, http://ift.tt/wSF09L"

<< tag("block-definition")
<< attr("category") << "sensing"
<< attr("type") << "command"
<< attr("s") << "data reporter"

<< tag("header") << endtag()
<< tag("code") << endtag()
<< tag("inputs") << endtag()

<< tag("script")
<< tag("block")
<< attr("s") << "doSetVar"
<< tag("l")
<< chardata() << "datalist"
<< endtag()

<< tag("block")
<< attr("s") << "reportNewList"

<< tag("list")

insertdata();


<< endtag("block-definition"); // close all tags up to specified

// look: I didn't close "sample-tag", it will be closed in XmlStream destructor

return 0;
}

void insertdata(){
string line;
ifstream myfile ("DATALOG.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
<< tag("l")
<< chardata() << line
<< endtag()
}
myfile.close();
}

else cout << "Unable to open file";
}

No comments:

Post a Comment