Results 1 to 4 of 4

Thread: XML validation error

  1. #1
    Join Date
    Sep 2010
    Location
    Moscow
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default XML validation error

    I have "Premateure end of document" error in at line 1 column 0 of my file, when try to validate it with XSD schema. I've tried to validate it in different software and online validators and they told me, that everything correct. So I decide, that file wasn't read correct, but it can't be cos I use it after validation. So maybe scheme wasn't reade correct? Please help me to find error.

    it's part of constructor:
    Qt Code:
    1. QFile tmlFile("..\\tml.xsd");
    2. if (!tmlFile.open(QIODevice::ReadOnly))
    3. {
    4. Message::sendMessage(ERROR, "Can't find validation scheme!", "check if it's on it's place", true);
    5. return;
    6. }
    7. mTmlSchema = new QXmlSchema();
    8. if (!mTmlSchema->load(&tmlFile))
    9. {
    10. tmlFile.close();
    11. Message::sendMessage(ERROR, "Validation scheme is'n valid", "use some software like Liquid XML studio to validate scheme and fix errors", true);
    12. return;
    13. }
    14.  
    15. ScanForObjects();
    16. ScanForLevels();
    17.  
    18. tmlFile.close();
    To copy to clipboard, switch view to plain text mode 

    ScanForObjects method code:
    Qt Code:
    1. void
    2. XmlLoader::ScanForObjects()
    3. {
    4. ScanXMLForObjects("..\\Resources\\Objects.xml", &mMapsMap[GLOBAL]);
    5. ScanXMLForObjects("..\\Resources\\LogicalObjects.xml", &mMapsMap[LOGICAL]);
    6. ScanXMLForObjects("..\\Resources\\PhysicalObjects.xml", &mMapsMap[PHYSICAL]);
    7. ScanXMLForObjects("..\\Resources\\GraphicalObjects.xml", &mMapsMap[GRAPHICAL]);
    8. }
    To copy to clipboard, switch view to plain text mode 

    ScanXMLForObjects method code where bool isValid return false:
    Qt Code:
    1. void
    2. XmlLoader::ScanXMLForObjects(QString filename, elementsMap* mapToFill)
    3. {
    4. QXmlSchemaValidator objectsTmlValidator(*mTmlSchema);
    5. objectsTmlValidator.setMessageHandler(this);
    6.  
    7. QDomDocument objectsDocument("Objects");
    8. QFile objectsFile(filename);
    9. if (!objectsFile.open(QIODevice::ReadWrite))
    10. {
    11. return;
    12. }
    13. if (!objectsDocument.setContent(&objectsFile))
    14. {
    15. objectsFile.close();
    16. return;
    17. }
    18. bool isValid = objectsTmlValidator.validate(&objectsFile, QUrl::fromLocalFile(objectsFile.fileName()));
    19. objectsFile.close();
    20.  
    21. QDomElement currentObjectElement =
    22. objectsDocument.documentElement().firstChildElement().firstChildElement("Object");
    23. while (!currentObjectElement.isNull())
    24. {
    25. (*mapToFill)[currentObjectElement.attribute("name").toStdString()] = currentObjectElement;
    26. currentObjectElement = currentObjectElement.nextSiblingElement("Object");
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: XML validation error

    Please provide a minimum compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Location
    Moscow
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: XML validation error

    Ah. I thought that it's not good to place so mush code here, but:

    this is header code
    Qt Code:
    1. #pragma once
    2.  
    3. #include <QtXml/QDomElement>
    4. #include <QtXmlPatterns/QXmlSchema>
    5. #include <QtXmlPatterns/QAbstractMessageHandler>
    6. #include <QtCore/QString>
    7.  
    8. #include <map>
    9.  
    10. #define gXML ::XmlLoader::getSingletone()
    11.  
    12. class XmlLoader: public QAbstractMessageHandler
    13. {
    14. public:
    15. enum dataType
    16. {
    17. GLOBAL = 0,
    18. LOGICAL = 1,
    19. PHYSICAL = 2,
    20. GRAPHICAL = 3,
    21. DATA_TYPE_COUNT = 4
    22. };
    23. private:
    24. typedef std::map<std::string, QDomElement> elementsMap;
    25.  
    26. // map of all levels, that was exist at the time of program started
    27. std::map<dataType, elementsMap> mMapsMap;
    28. elementsMap mLevelsMap;
    29. QXmlSchema* mTmlSchema;
    30.  
    31. static XmlLoader* mInstance;
    32.  
    33. protected:
    34. virtual void handleMessage(QtMsgType type,
    35. const QString& description,
    36. const QUrl& identifier,
    37. const QSourceLocation& sourceLocation);
    38.  
    39. private:
    40. XmlLoader();
    41. void ScanForLevels();
    42. void ScanForObjects();
    43. void ScanXMLForObjects(QString filename, elementsMap* mapToFill);
    44.  
    45. public:
    46. static XmlLoader* getSingletone();
    47. QDomElement* getLevelElement(std::string name);
    48. QDomElement* getObjectElement(dataType type, std::string name);
    49. QStringList getAvialableLevelsList();
    50. };
    To copy to clipboard, switch view to plain text mode 

    And .cpp code
    Qt Code:
    1. #include "xml_loader.h"
    2. //#include "message.h"
    3.  
    4. #include <QtXml/QDomDocument>
    5. #include <QtXmlPatterns/QXmlSchemaValidator>
    6.  
    7. #include <QtCore/QFile>
    8. #include <QtCore/QDir>
    9. #include <QtCore/QUrl>
    10.  
    11. XmlLoader* XmlLoader::mInstance = NULL;
    12.  
    13. XmlLoader*
    14. XmlLoader::getSingletone()
    15. {
    16. if (mInstance)
    17. {
    18. return mInstance;
    19. }
    20. else
    21. {
    22. mInstance = new XmlLoader();
    23. return mInstance;
    24. }
    25. }
    26.  
    27. XmlLoader::XmlLoader()
    28. :QAbstractMessageHandler()
    29. {
    30. QFile tmlFile("..\\tml.xsd");
    31. if (!tmlFile.open(QIODevice::ReadOnly))
    32. {
    33. // Message::sendMessage(ERROR, "Can't find validation scheme!", "check if it's on it's place", true);
    34. return;
    35. }
    36. mTmlSchema = new QXmlSchema();
    37. if (!mTmlSchema->load(&tmlFile))
    38. {
    39. tmlFile.close();
    40. // Message::sendMessage(ERROR, "Validation scheme is'n valid", "use some software like Liquid XML studio to validate scheme and fix errors", true);
    41. return;
    42. }
    43.  
    44. ScanForObjects();
    45. ScanForLevels();
    46.  
    47. tmlFile.close();
    48. }
    49.  
    50. void
    51. XmlLoader::ScanForObjects()
    52. {
    53. ScanXMLForObjects("..\\Resources\\Objects.xml", &mMapsMap[GLOBAL]);
    54. ScanXMLForObjects("..\\Resources\\LogicalObjects.xml", &mMapsMap[LOGICAL]);
    55. ScanXMLForObjects("..\\Resources\\PhysicalObjects.xml", &mMapsMap[PHYSICAL]);
    56. ScanXMLForObjects("..\\Resources\\GraphicalObjects.xml", &mMapsMap[GRAPHICAL]);
    57. }
    58.  
    59. void
    60. XmlLoader::ScanForLevels()
    61. {
    62. QDir levelsDir("..\\Resources\\Levels");
    63. QStringList entryList = levelsDir.entryList(QDir::Files);
    64.  
    65. for (QStringList::iterator i = entryList.begin(); i != entryList.end(); i++)
    66. {
    67. QString filename = *i;
    68. filename = "..\\Resources\\Levels\\" + filename;
    69.  
    70. QFile file(filename);
    71. if (!file.open(QIODevice::ReadOnly))
    72. return;
    73.  
    74. QDomDocument levelDocument("Level");
    75. if (!levelDocument.setContent(&file))
    76. {
    77. file.close();
    78. return;
    79. }
    80. file.close();
    81.  
    82. QDomElement levelElement = levelDocument.documentElement().firstChildElement("Level");
    83. mLevelsMap[levelElement.attribute("name").toStdString()] = levelElement;
    84. }
    85.  
    86. return;
    87. }
    88.  
    89. void
    90. XmlLoader::handleMessage(QtMsgType type,
    91. const QString& description,
    92. const QUrl& identifier,
    93. const QSourceLocation& sourceLocation)
    94. {
    95. // code to this method was added just to have place for breakpoint
    96. QString path = identifier.path();
    97. return;
    98. }
    99.  
    100. XmlLoader::getAvialableLevelsList()
    101. {
    102. QStringList levelsList;
    103. for (elementsMap::iterator i = mLevelsMap.begin(); i != mLevelsMap.end(); i++)
    104. {
    105. levelsList << tr(i->first.c_str());
    106. }
    107. return levelsList;
    108. }
    109.  
    110. XmlLoader::getLevelElement(std::string name)
    111. {
    112. elementsMap::iterator i = mLevelsMap.find(name);
    113. if (i == mLevelsMap.end())
    114. {
    115. return NULL;
    116. }
    117. else
    118. {
    119. return &(i->second);
    120. }
    121. }
    122.  
    123. XmlLoader::getObjectElement(dataType type, std::string name)
    124. {
    125. elementsMap::iterator i = mMapsMap[type].find(name);
    126. if (i == mMapsMap[type].end())
    127. {
    128. return NULL;
    129. }
    130. else
    131. {
    132. return &(i->second);
    133. }
    134. }
    135.  
    136. void
    137. XmlLoader::ScanXMLForObjects(QString filename, elementsMap* mapToFill)
    138. {
    139. QXmlSchemaValidator objectsTmlValidator(*mTmlSchema);
    140. objectsTmlValidator.setMessageHandler(this);
    141. bool a = objectsTmlValidator.schema().isValid();
    142.  
    143. QDomDocument objectsDocument("Objects");
    144. QFile objectsFile(filename);
    145. if (!objectsFile.open(QIODevice::ReadWrite))
    146. {
    147. return;
    148. }
    149.  
    150. QString* errorString = new QString();
    151. if (!objectsDocument.setContent(&objectsFile, errorString))
    152. {
    153. objectsFile.close();
    154. return;
    155. }
    156.  
    157. // HERE is my error. isValid always become false
    158. bool isValid = objectsTmlValidator.validate(&objectsFile, QUrl::fromLocalFile(objectsFile.fileName()));
    159. objectsFile.close();
    160.  
    161. QDomElement currentObjectElement =
    162. objectsDocument.documentElement().firstChildElement().firstChildElement("Object");
    163. while (!currentObjectElement.isNull())
    164. {
    165. (*mapToFill)[currentObjectElement.attribute("name").toStdString()] = currentObjectElement;
    166. currentObjectElement = currentObjectElement.nextSiblingElement("Object");
    167. }
    168. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: XML validation error

    It is not a minimal compilable example.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. XML Validation
    By NoRulez in forum Qt Programming
    Replies: 4
    Last Post: 15th September 2010, 13:07
  2. cross validation
    By mickey in forum General Discussion
    Replies: 0
    Last Post: 14th September 2009, 17:39
  3. MessageBox Validation
    By fortyhideout12 in forum Newbie
    Replies: 10
    Last Post: 2nd September 2009, 17:14
  4. Validation
    By Tavit in forum Qt Programming
    Replies: 2
    Last Post: 21st March 2009, 07:03
  5. XML DTC validation problem
    By shailesh in forum Qt Programming
    Replies: 1
    Last Post: 16th March 2006, 13:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.