问题描述
我正在尝试将 id 添加到 cpp 中的 QDomElements。我成功生成了一个kml文件。我想以增量方式将 id 添加到文档、地标等 kml 标签中。如何将 id 添加到 kml 文件中的标签。
QDomProcessingInstruction header = createProcessingInstruction(QStringLiteral("xml"),QStringLiteral("version=\"1.0\" encoding=\"UTF-8\""));
appendChild(header);
QDomElement kmlElement = createElement(QStringLiteral("kml"));
kmlElement.setAttribute(QStringLiteral("xmlns"),"http://www.opengis.net/kml/2.2");
QDomElement m_rootDocumentElement; m_rootDocumentElement =
createElement(QStringLiteral("Document"));
kmlElement.appendChild(m_rootDocumentElement);
for(int i = 0; i < qgeoCordinateList.count(); i++){
QDomElement wpPlacemarkElement = createElement("Placemark");
QDomElement wpPlacemarkElement1 = elementById("4");
m_rootDocumentElement.appendChild(wpPlacemarkElement);
wpPlacemarkElement.appendChild(wpPlacemarkElement1);
addTextElement(wpPlacemarkElement,"name",QString::number(i+1));
QDomElement pointElement = createElement("Point");
wpPlacemarkElement.appendChild(pointElement);
addTextElement(pointElement,"Index",QString::number(i));
}
QTextStream stream(&file);
stream << header;
stream << kmlElement;
qDebug() << "kml Data :" << kmlElement.toElement().text().simplified();
file.close();
解决方法
To add Id into "QDomElement" tag add line where you want to add tag for QDomElement
wpPlacemarkElement.setAttribute(QStringLiteral("id"),QString::number(i));
// i is index of for loop
// add this line into for loop