如何通过循环将数据从一个TXT文件中的XML文件导入到C++中? 我试图通过C++将一些数据从一个基本的TXT文件中输入到一个XML文件中。我知道如何编写标记,但如何从硬编码转换为添加一些动态列表?我在利用课堂来帮助我 #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://snap.berkeley.edu" << 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"; }

如何通过循环将数据从一个TXT文件中的XML文件导入到C++中? 我试图通过C++将一些数据从一个基本的TXT文件中输入到一个XML文件中。我知道如何编写标记,但如何从硬编码转换为添加一些动态列表?我在利用课堂来帮助我 #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://snap.berkeley.edu" << 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"; },c++,xml,file-writing,C++,Xml,File Writing,您需要将insertdata中的此输出定向到xml对象: << tag("l") << chardata() << line << endtag() 一种方法是将对xml的引用作为参数传递: void insertdata(XmlStream &x) { ... x << tag("l") << chardata() << line <<

您需要将insertdata中的此输出定向到xml对象:

  << tag("l") 
  << chardata() << line 
  << endtag()
一种方法是将对xml的引用作为参数传递:

void insertdata(XmlStream &x) {
    ...
    x << tag("l") 
    << chardata() << line 
    << endtag();
    ...
然后在main中适当地调用它:

    ...
    << tag("list");

    insertdata(xml);

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

很简单。使用XML库。你不需要重复努力。