1
0
forked from BRT/arc
arc/automation_script/xml_reader.cpp
2025-04-25 15:42:51 +04:00

80 lines
2.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "xml_reader.h"
#include <QFile>
#include <QXmlStreamReader>
#include <QRegularExpression>
#include <QDebug>
#include <regex>
bool dumpAndPullUiXml(const std::string& localPath) {
int r1 = system("adb shell uiautomator dump /sdcard/view.xml");
int r2 = system(("adb pull /sdcard/view.xml " + localPath).c_str());
return r1 == 0 && r2 == 0;
}
std::vector<UiElement> parseUiDumpUsingQXml(const QString& filePath) {
std::vector<UiElement> uiElements;
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << "Не удалось открыть файл:" << filePath;
return uiElements;
}
QXmlStreamReader xmlReader(&file);
UiElement currentElement;
while (!xmlReader.atEnd()) {
xmlReader.readNext();
if (xmlReader.hasError()) {
qDebug() << "XML parsing error at line" << xmlReader.lineNumber() << ":"
<< xmlReader.errorString();
return {}; // Возвращаем пустой вектор в случае ошибки
}
if (xmlReader.isStartElement()) {
qDebug() << "Found element:" << xmlReader.name();
if (xmlReader.name() == "node") {
for (const QXmlStreamAttribute &attr : xmlReader.attributes()) {
if (attr.name() == "bounds") {
// Извлекаем координаты из bounds
QString bounds = attr.value().toString();
QRegularExpression regex(R"(\[([0-9]+),([0-9]+)\]\[([0-9]+),([0-9]+)\])");
QRegularExpressionMatch match = regex.match(bounds);
if (match.hasMatch()) {
// Извлекаем координаты из регулярного выражения
currentElement.x1 = match.captured(1).toInt();
currentElement.y1 = match.captured(2).toInt();
currentElement.x2 = match.captured(3).toInt();
currentElement.y2 = match.captured(4).toInt();
}
}
if (attr.name() == "text") {
currentElement.text = attr.value().toString();
}
if (attr.name() == "resource-id") {
currentElement.resourceId = attr.value().toString();
}
}
uiElements.push_back(currentElement);
}
}
// Пропускаем закрывающий тег </node>
if (xmlReader.isEndElement()) {
qDebug() << "End element:" << xmlReader.name();
}
}
if (xmlReader.hasError()) {
qWarning() << "Final XML parsing error:" << xmlReader.errorString();
}
return uiElements;
}