Как создать буфер массива для хранения серии строкового вектора

Это мой объектный класс векторной сборки как класс doinfo, функция printtoxml унаследована от класса doinfo,

#include "stdafx.h"#include <iostream>
#include <vector>
#include <string>
#include <fstream>

#include <sstream>
#include <algorithm>

#include <msclr/marshal_cppstd.h>

#using <System.dll>
#using <System.Xml.dll>
#using <System.Data.dll>

void queryinxpath_();

class doinfo
{
public:
doinfo(const std::string &dono,const std::string &acno, \
const std::string &name,const std::string &stcd, \
const std::string &desc,const std::string &upri, \
const std::string &dqty,const std::string &namb)
:dono_(dono),acno_(acno),name_(name),stcd_(stcd), \
desc_(desc),upri_(upri),dqty_(dqty),namb_(namb)
{}

void printoxml();

const std::string dono_;
const std::string acno_;
const std::string name_;
const std::string stcd_;
const std::string desc_;
const std::string upri_;
const std::string dqty_;
const std::string namb_;

private:

};

void doinfo::printoxml()
{

std::ofstream fo;

fo.open("C:\\test-result.xml",std::ios::out);

fo << "<Do>\n";

fo << "\t<delivery>\n";
fo << "\t\t<dono>" << dono_ << "</dono>\n";
fo << "\t\t<custcode>" << acno_ << "</custcode>\n";
fo << "\t\t<custname>" << name_ << "</custname>\n";
fo << "\t\t<stockcode>" << stcd_ << "</stockcode>\n";
fo << "\t\t<stockdesc>" << desc_ << "</stockdesc>\n";
fo << "\t\t<unitprice>" << upri_ << "</unitprice>\n";
fo << "\t\t<qty>" << dqty_ << "</qty>\n";
fo << "\t\t<amount>" << namb_ << "</amount>\n";
fo << "\t</delivery>\n";

fo << "</Do>\n";

fo.close();

}

void queryinxpath_()
{

std::string dono;
std::string custcode;
std::string custname;
std::string stockcode;
std::string stockdesc;
std::string unitprice;
std::string qty;
std::string amount;System::Xml::XPath::XPathDocument^ doc = gcnew System::Xml::XPath::XPathDocument("C:/test.xml");

System::Xml::XPath::XPathNavigator^ nav = doc->CreateNavigator();

System::Xml::XPath::XPathExpression^ expr = nav->Compile("descendant::delivery[amount > 100]");

System::Xml::XPath::XPathNodeIterator^ iterator=nav->Select(expr);

std::vector<doinfo> don;

while (iterator->MoveNext())
{

System::Xml::XPath::XPathNavigator^ nav_ = iterator->Current->Clone();

nav_->MoveToChild("dono","");
//msclr::interop::marshal_context context;
//dono = context.marshal_as<std::string>(nav_->Value);
dono = msclr::interop::marshal_as<std::string>(nav_->Value);

System::Console::WriteLine("Dono : {0}",msclr::interop::marshal_as<System::String^>(dono));

nav_->MoveToParent();
nav_->MoveToChild("custcode","");

custcode = msclr::interop::marshal_as<std::string>(nav_->Value);
System::Console::WriteLine("Customer Code : {0} ",nav_->Value);

nav_->MoveToParent();
nav_->MoveToChild("custname","");

custname = msclr::interop::marshal_as<std::string>(nav_->Value);
System::Console::WriteLine("Customer Name : {0} ",nav_->Value);

nav_->MoveToParent();
nav_->MoveToChild("stockcode","");

stockcode = msclr::interop::marshal_as<std::string>(nav_->Value);
System::Console::WriteLine("StockCode : {0} ",nav_->Value);

nav_->MoveToParent();
nav_->MoveToChild("stockdesc","");

stockdesc = msclr::interop::marshal_as<std::string>(nav_->Value);
System::Console::WriteLine("Stockdesc : {0} ",nav_->Value);

nav_->MoveToParent();
nav_->MoveToChild("unitprice","");

unitprice = msclr::interop::marshal_as<std::string>(nav_->Value);
System::Console::WriteLine("UnitPrice : {0} ",nav_->Value);

nav_->MoveToParent();
nav_->MoveToChild("qty","");

qty = msclr::interop::marshal_as<std::string>(nav_->Value);
System::Console::WriteLine("Quantity : {0} ",nav_->Value);

nav_->MoveToParent();
nav_->MoveToChild("amount","");

amount = msclr::interop::marshal_as<std::string>(nav_->Value);
System::Console::WriteLine("Amount : {0} ",nav_->Value);

System::Console::WriteLine("\n");

don.push_back(doinfo(dono,custcode,custname,stockcode,stockdesc,unitprice,qty,amount));

}

std::vector<doinfo>::iterator ite;

for (ite=don.begin();ite!=don.end();++ite)
{

ite->printoxml();

}

}

int _tmain(int argc, _TCHAR* argv[])
{
queryinxpath_();

return 0;}

Когда я вернусь к проверке результата «test-result.xml», он покажет, что существует только одна группа узлов доставки. Предполагается, что существует несколько групп узлов доставки, но он сможет захватывать последние узлы в конце цикла.

<Do>
<delivery>
<dono>DM202423</dono>
<custcode>M15</custcode>
<custname>A Company</custname>
<stockcode>ZPPFERG</stockcode>
<stockdesc>PRECISCO ENVELOPE</stockdesc>
<unitprice>0.8200</unitprice>
<qty>200</qty>
<amount>164.00</amount>
</delivery>
</Do>

Любой может помочь мне решить мои проблемы, создав буфер массива внутри класса doinfo, чтобы я мог захватить каждый строковый объект в памяти для дальнейшей распечатки в файл XML.

0

Решение

Я исправил свои проблемы путем передачи ofstream в качестве параметров в функцию-член класса векторного объекта. Вот мои полные коды.

#include "stdafx.h"#include <iostream>
#include <vector>
#include <string>
#include <fstream>

#include <sstream>
#include <algorithm>

#include <msclr/marshal_cppstd.h>

#using <System.dll>
#using <System.Xml.dll>
#using <System.Data.dll>

void queryinxpath_();

class doinfo
{
public:
doinfo(const std::string &dono,const std::string &acno, \
const std::string &name,const std::string &stcd, \
const std::string &desc,const std::string &upri, \
const std::string &dqty,const std::string &namb)
:dono_(dono),acno_(acno),name_(name),stcd_(stcd), \
desc_(desc),upri_(upri),dqty_(dqty),namb_(namb)
{}

void printoxml(std::ofstream &os);

const std::string dono_;
const std::string acno_;
const std::string name_;
const std::string stcd_;
const std::string desc_;
const std::string upri_;
const std::string dqty_;
const std::string namb_;

private:

};

void doinfo::printoxml(std::ofstream &os)
{

os << "\t<delivery>\n";
os << "\t\t<dono>" << dono_ << "</dono>\n";
os << "\t\t<custcode>" << acno_ << "</custcode>\n";
os << "\t\t<custname>" << name_ << "</custname>\n";
os << "\t\t<stockcode>" << stcd_ << "</stockcode>\n";
os << "\t\t<stockdesc>" << desc_ << "</stockdesc>\n";
os << "\t\t<unitprice>" << upri_ << "</unitprice>\n";
os << "\t\t<qty>" << dqty_ << "</qty>\n";
os << "\t\t<amount>" << namb_ << "</amount>\n";
os << "\t</delivery>\n";

}

void queryinxpath_()
{

std::string dono;
std::string custcode;
std::string custname;
std::string stockcode;
std::string stockdesc;
std::string unitprice;
std::string qty;
std::string amount;System::Xml::XPath::XPathDocument^ doc = gcnew System::Xml::XPath::XPathDocument("C:/test.xml");

System::Xml::XPath::XPathNavigator^ nav = doc->CreateNavigator();

System::Xml::XPath::XPathExpression^ expr = nav->Compile("descendant::delivery[amount > 100]");

System::Xml::XPath::XPathNodeIterator^ iterator=nav->Select(expr);

std::vector<doinfo> don;

while (iterator->MoveNext())
{

System::Xml::XPath::XPathNavigator^ nav_ = iterator->Current->Clone();

nav_->MoveToChild("dono","");
//msclr::interop::marshal_context context;
//dono = context.marshal_as<std::string>(nav_->Value);
dono = msclr::interop::marshal_as<std::string>(nav_->Value);

System::Console::WriteLine("Dono : {0}",msclr::interop::marshal_as<System::String^>(dono));

nav_->MoveToParent();
nav_->MoveToChild("custcode","");

custcode = msclr::interop::marshal_as<std::string>(nav_->Value);
System::Console::WriteLine("Customer Code : {0} ",nav_->Value);

nav_->MoveToParent();
nav_->MoveToChild("custname","");

custname = msclr::interop::marshal_as<std::string>(nav_->Value);
System::Console::WriteLine("Customer Name : {0} ",nav_->Value);

nav_->MoveToParent();
nav_->MoveToChild("stockcode","");

stockcode = msclr::interop::marshal_as<std::string>(nav_->Value);
System::Console::WriteLine("StockCode : {0} ",nav_->Value);

nav_->MoveToParent();
nav_->MoveToChild("stockdesc","");

stockdesc = msclr::interop::marshal_as<std::string>(nav_->Value);
System::Console::WriteLine("Stockdesc : {0} ",nav_->Value);

nav_->MoveToParent();
nav_->MoveToChild("unitprice","");

unitprice = msclr::interop::marshal_as<std::string>(nav_->Value);
System::Console::WriteLine("UnitPrice : {0} ",nav_->Value);

nav_->MoveToParent();
nav_->MoveToChild("qty","");

qty = msclr::interop::marshal_as<std::string>(nav_->Value);
System::Console::WriteLine("Quantity : {0} ",nav_->Value);

nav_->MoveToParent();
nav_->MoveToChild("amount","");

amount = msclr::interop::marshal_as<std::string>(nav_->Value);
System::Console::WriteLine("Amount : {0} ",nav_->Value);

System::Console::WriteLine("\n");

don.push_back(doinfo(dono,custcode,custname,stockcode,stockdesc,unitprice,qty,amount));

}

std::ofstream fo;

fo.open("c:\\test-result.xml",std::ios::out);

std::vector<doinfo>::iterator ite;

fo << "<Do>\n";

for (ite=don.begin();ite!=don.end();++ite)
{

ite->printoxml(fo);

}fo << "</Do>\n";

fo.close();

}

int _tmain(int argc, _TCHAR* argv[])
{
queryinxpath_();

return 0;}
0

Другие решения


По вопросам рекламы [email protected]