c++ - Qt XML 输入

标签 c++ xml qt

我正在尝试使用 Qt 工具包在 C++ 程序中从 XML 文件输入数据。我的 XML 数据的格式如下:

`<item>
    <title>title<\title>
    <tree_loc1>0<\tree_loc1>
    <parent>parent<\parent>
    <description>description<\description>
    <other_info>other info<\other_info>
    <location>location<\location>
    <last_modified>Mar 28 2009 8:16 pm<\last_modified>
    <radio>0<\radio>
 </item>`

目前我必须在 XML 中读取的函数如下。不幸的是,它识别出第一个标签(标题)中的数据,然后将所有 future 访问该数据的尝试返回为 NULL。我传入的 subRoot 是 domDocument.documentElement()。我对 XML 很陌生,对 Qt 也有些陌生,非常感谢您在解决我的问题时提供的任何帮助!非常感谢。

void XmlHandler::readXML(QStandardItemModel *model, QDomNode subRoot){
  QDomElement node;
  QString title;
  int row;
  QString parent;
  QString description;
  QString other_info;
  QString location;
  QString last_modified;
  QString radio;
  QString value;
  bool flag;

  if (subRoot.isNull())
    return; // error condition

  for (int i = 0; i < N_STRINGS; i++){
    node = subRoot.firstChildElement(tagName[i]); // returns NULL all but the 1st time
    value = DEFAULT_VALUE;

    value = node.text();
    switch (i) {
      case 0:
      title = value;
          break;
      case 1:
      row = value.toInt();
      break;
      case 2:
      parent = value;
      break;
      case 3:
      description = value;
          break;
      case 4:
      other_info = value;
      break;
      case 5:
      location = value;
      break;
      case 6:
      last_modified = value;
      break;
      case 7:
          radio = value;
      break;
      }
  }
}

最佳答案

您的 XML 数据不是有效的 XML - 反斜杠应该是正斜杠。例如:

<title>title<\title>

应该是:

<title>title</title>

关于c++ - Qt XML 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/695515/

相关文章:

c++ - 什么时候适合使用继承?

c++ - 将 Q3DSurface 嵌入到 QQuickItem 中

javascript - 为什么下面的代码会导致 QML 中的 TypeError

c++ - 如何让QTableView在没有聚焦的情况下自动更新内容?

c++ - Qt 将附加参数传递给插槽并保留发出的信号数据

c++ - 按键时更改 GLFWwindow 的背景

c# - 使用可选标签反序列化 XML

javascript - SOAP 请求中缺少激活全局自定义操作所需的输入字段

c++ - 如何使用 libxml2 的 SAX 接口(interface)来解析流式 xml 文件?

C++ 质量-#include