【问题标题】:Why does TinyXml2 put XMLDeclaration at the end?为什么 TinyXml2 把 XMLDeclaration 放在最后?
【发布时间】:2022-10-23 19:21:02
【问题描述】:

我正在使用 TinyXml2 v8.0.0 创建一个 XML 缓冲区以发送到 API。该示例包括一个声明。我正在通过以下方式实现这一点:

XMLDocument doc;
doc.InsertEndChild(doc.NewDeclaration());
XMLElement* pRoot = doc.NewElement("Stuff");
doc.InsertFirstChild(pRoot);

NewDeclaration 的文档指出:

如果text 参数是null,则使用标准声明。:

<?xml version="1.0" encoding="UTF-8"?>

您可以在https://github.com/leethomason/tinyxml2/blob/master/xmltest.cpp#L1637 中将此视为测试

但是当我将缓冲区打印出来时,声明已放在缓冲区末尾的换行符之后:

<Stuff>
</Stuff>

<?xml version="1.0" encoding="UTF-8"?>

有谁知道为什么会这样?我希望它位于缓冲区的开头,没有换行符。

【问题讨论】:

  • 看起来 tinyxml 在这方面简直是一团糟。考虑另一种工具。

标签: c++ tinyxml2


【解决方案1】:

大概是因为您告诉它将声明作为EndChild 并将Stuff 元素作为FirstChild

【讨论】:

  • 我之前尝试使用InsertFirstChild 添加声明,但没有任何区别。但是,您的建议有效。也许InsertFirstChild 总是添加到开头?
  • @ parsley72 如果您查找它,您会看到 InsertFirstChild 总是添加到开头,并且 InsertEndChild 总是添加到末尾。
猜你喜欢
  • 2012-01-05
  • 1970-01-01
  • 2023-03-25
  • 2011-10-13
  • 1970-01-01
  • 2021-12-05
  • 2022-10-08
  • 1970-01-01
  • 2011-07-19
相关资源
最近更新 更多