如何使用Qt应用程序将Id添加到kml文件中的“QDomElement”标签

问题描述

我正在尝试将 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...