c++ - 使用qt c++检查Xml中是否存在根元素

标签 c++ xml qt

I have an Xml file:

 <?xml version="1.0" encoding="utf-8" ?>
 <AppConfig>
 <DefaultCulture></DefaultCulture>
 <StartupMessageTitle></StartupMessageTitle>
 <StartupMessageText></StartupMessageText>
 <StartupMessageIcon>Information</StartupMessageIcon>
 <DefaultProfileSettings>
 <DriverName>wia</DriverName>
 <UseNativeUI>false</UseNativeUI>
 <IconID>0</IconID>
 <MaxQuality>false</MaxQuality>
 <AfterScanScale>OneToOne</AfterScanScale>
 <Brightness>0</Brightness>
 <Contrast>0</Contrast>
 <BitDepth>C24Bit</BitDepth>
 <PageAlign>Left</PageAlign>
 <PageSize>Letter</PageSize>
 <Resolution>Dpi100</Resolution>
 <PaperSource>Glass</PaperSource>
 </DefaultProfileSettings>
 <!--
 <AutoSaveSettings>
 <FilePath></FilePath>
 <ClearImagesAfterSaving>false</ClearImagesAfterSaving>
 <Separator>FilePerPage</Separator>
 </AutoSaveSettings>
  -->
 </AppConfig>

I need to check if root elements "AutoSaveSettings" exists in xml using qt c++ . if the root elements exists then remove the commented line before and after the Auto save settings? How can we do it in qt c++. How do I perform this operation in c++.Check if exists start element or root element

  #include <QtCore>
  #include <QtXml/QDomDocument>
  #include <QDebug>
  #include <QFile>
  #include <QXmlStreamReader>

  bool elementExists(const QFile &file, const QString &elementName)      
  {

     QXmlStreamReader reader(&file);

     while (reader.readNextStartElement())
      {
        if(reader.name() == elementName)
         {
           return true;
         }
     }
     return false;
   }

  int main(int argc,char *argv[])
   {
     QCoreApplication a(argc,argv);

     QDomDocument document;
     QFile file = "C:/Program Files (x86)/NAPS2/appsettings.xml";
     if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
     {
         qDebug()<<"Failed to open the file";
         return -1;
     }
     else{
         if(!document.setContent(&file))
         {
             qDebug()<< "Failed to load Document";
             return -1;
         }
         file.close();
     }

     elementExists(file,"AutoSaveSettings");

     qDebug()<< "Finished";

     return a.exec();
    }

最佳答案

尝试这样的事情:

bool elementExists( QFile & file, const QString & elementName){

    QXmlStreamReader reader (&file);

    while (reader.readNextStartElement()){
        if(reader.name() == elementName) return true; 
       //elementName should be "AutoSaveSettings"
    }
    return false;
}

编辑:另一种方法是使用 QDomDocument

不推荐,因为不再主动维护 QDomDocument

bool elementExists( QFile & file,const QString & elementName){

    QDomDocument reader;
    reader.setContent(&file, true);
    QDomNodeList elementList =  reader.elementsByTagName(elementName);
    return elementList.size()>0;
}

关于c++ - 使用qt c++检查Xml中是否存在根元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43040074/

相关文章:

c++ - 在编译时使用模板遍历结构字段

C++方法声明问题

java - 将 CData 添加到格式不正确的 XML 元素

android - QML 映射 : get coordinates when tap the screen

c++ - 在 Linux C++ 中获取 PTY 的最简单方法

c++ - 在没有类声明的情况下使用 Qt 的 Q_DECLARE_FLAGS 和 Q_DECLARE_OPERATORS_FOR_FLAGS

c++ - std::set,lower_bound 和 upper_bound 是如何工作的?

c++ - 将方程的值赋给变量有问题

javascript - 如何将 XML 内容附加到 HTML 元素,就好像它是 HTML 一样并应用 CSS 样式?

java - java中使用多线程解析xml并写入txt文件