V
V
VitaliiUA2019-01-23 17:05:08
C++ / C#
VitaliiUA, 2019-01-23 17:05:08

TinyXML2 Write array to XML File how to implement?

I need to write an array to an XML file, I found the SetText method, but if you call it but the second call overwrites, I understand that you need to convert it to an array of chars, it only accepts it, if you know some better option, please answer
It should be like this,

<root>
 <primes> 101 103 107 149 743  751… </primes>
</root>

Here is my code.
XMLDocument Doc;
  XMLNode * pRoot = Doc.NewElement("root");
  Doc.InsertFirstChild(pRoot);
  XMLElement * pElement = Doc.NewElement("primes");
  for (const auto &it : First_interval) {
    pElement->SetText(it);
  }
  for (const auto &it : Second_interval) {
    pElement->SetText(it);
  }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2019-01-23
@Rsa97

Arrays in xml ̶o̶ ̶e̶sh̶ё̶ ̶i̶z̶v̶r̶a̶sch̶e̶n̶i̶e are usually written something like this:

<root>
  <primes>
    <prime>101</prime>
    <prime>103</prime>
    <prime>107</prime>
    ...
  </primes>
</root>
What you are trying to do is not an array but a string. It must be formed in advance and written down already formed by one call to SetText.

V
VitaliiUA, 2019-01-23
@VitaliiUA

Thank you, I kind of made a crutch, but it looks terrible

std::stringstream ss;
  char c[400];
  XMLDocument Doc;
  XMLNode * pRoot = Doc.NewElement("root");
  Doc.InsertFirstChild(pRoot);
  XMLElement * pElement = Doc.NewElement("primes");
  for (const auto &it : First_interval) {
    ss << it;
  }
  for (const auto &it : Second_interval) {
    ss << it;
  }
  for (int i = 0,j=3; i < 400; i++,j+=4) {
    if(c[i]!=' ')ss >> c[i];
    if(j<400)c[j] = ' ';
  }
  pElement->SetText(c);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question