Skip to content

Commit

Permalink
Remove extra root element from XML documents
Browse files Browse the repository at this point in the history
See #842 and #1036.
  • Loading branch information
erengy committed Oct 12, 2024
1 parent 8df401a commit 061e392
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/media/anime_db_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,21 @@
*/

#include <QFile>
#include <QRegularExpression>
#include <QXmlStreamReader>

#include "anime_db.hpp"

namespace {

// This regex is used to remove the extra root element from v1's XML documents so that they
// can be read by `QXmlStreamReader` without an "Extra content at end of document" error.
// See #842 for more information.
static const QRegularExpression metaElementRegex{"<meta>.+</meta>",
QRegularExpression::DotMatchesEverythingOption};

} // namespace

namespace anime {

QList<Anime> readDatabase() {
Expand All @@ -30,7 +41,10 @@ QList<Anime> readDatabase() {

if (!file.open(QIODevice::ReadOnly)) return {};

QXmlStreamReader xml(&file);
QString str{file.readAll()};
str.remove(metaElementRegex);

QXmlStreamReader xml(str);

if (!xml.readNextStartElement()) return {};
if (xml.name() != u"database") return {};
Expand Down Expand Up @@ -115,7 +129,10 @@ QList<ListEntry> readListEntries() {

if (!file.open(QIODevice::ReadOnly)) return {};

QXmlStreamReader xml(&file);
QString str{file.readAll()};
str.remove(metaElementRegex);

QXmlStreamReader xml(str);

if (!xml.readNextStartElement()) return {};
if (xml.name() != u"library") return {};
Expand Down

0 comments on commit 061e392

Please sign in to comment.