/
Zamar_Terrier
/
TigorEngine
Обзор
Документация
Войти
/
Zamar_Terrier
/
TigorEngine
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
tests/xml_parser.c
191 строка
9 KB
Zamar_Terrier
Запрос на слияние 'feature-12032026' (
#9
) из feature-12032026 в master
08 июн 2026, 11:31
Верифицирован
08 июн 2026, 11:31
2200f38
Код
Авторство
О чём код?
#include "Tools/xml_parser.h" #include <stdio.h> const char *xml_string = \ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" "<map version=\"1.10\" tiledversion=\"1.11.2\" orientation=\"orthogonal\" renderorder=\"right-down\" width=\"13\" height=\"10\" tilewidth=\"16\" tileheight=\"16\" infinite=\"0\" nextlayerid=\"4\" nextobjectid=\"14\">" " <tileset firstgid=\"1\" source=\"Default_V.tsx\"/>" " <layer id=\"1\" name=\"World\" width=\"13\" height=\"10\">" " <data encoding=\"csv\"> Фвфвфцвф" " </data>" " </layer>" " <layer id=\"2\" name=\"Interier\" width=\"13\" height=\"10\">" " <data encoding=\"csv\">" " </data>" " </layer>" " <objectgroup id=\"3\" name=\"Слой объектов 1\">" " <object id=\"1\" name=\"Village\" type=\"Enter\" x=\"105.75\" y=\"38.75\" width=\"32\" height=\"9\"/>" " <object id=\"2\" type=\"StartPoint\" x=\"112\" y=\"80\">" " </object>" " <object id=\"3\" name=\"D13\" type=\"DialogPoint\" x=\"80.4377\" y=\"95.0504\">" " <properties>" " <property name=\"quest\" type=\"object\" value=\"4\"/>" " <property name=\"spawn\" type=\"int\" value=\"2\"/>" " </properties>" " </object>" " <object id=\"4\" name=\"D14\" type=\"Dialog\" x=\"-53.5622\" y=\"211.55\" width=\"86\" height=\"19\">" " <properties>" " <property name=\"start\" type=\"object\" value=\"5\"/>" " <property name=\"x\" type=\"float\" value=\"80.44\"/>" " <property name=\"y\" type=\"float\" value=\"95.05\"/>" " </properties>" " <text wrap=\"1\">Диалог 14</text>" " </object>" " <object id=\"5\" name=\"D14_M1\" type=\"Monolog\" x=\"-162.729\" y=\"270.384\" width=\"159.5\" height=\"82.5\">" " <properties>" " <property name=\"select_1\" type=\"object\" value=\"7\"/>" " <property name=\"select_2\" type=\"object\" value=\"11\"/>" " <property name=\"select_3\" type=\"object\" value=\"6\"/>" " </properties>" " <text wrap=\"1\">Как же надоели эти крысы! Уже не знаю как вывести.</text>" " </object>" " <object id=\"6\" name=\"D14_A1_V2\" type=\"Answer\" x=\"43.7707\" y=\"419.216\" width=\"40.3333\" height=\"19\">" " <text wrap=\"1\">Пока</text>" " </object>" " <object id=\"7\" name=\"D14_A1_V1\" type=\"Answer\" x=\"-233.063\" y=\"411.216\" width=\"51\" height=\"19\">" " <properties>" " <property name=\"need_quest_id\" type=\"int\" value=\"38\"/>" " <property name=\"select_1\" type=\"object\" value=\"8\"/>" " </properties>" " <text wrap=\"1\">Грибы</text>" " </object>" " <object id=\"8\" name=\"D14_M2\" type=\"Monolog\" x=\"-310.479\" y=\"470.634\" width=\"185.667\" height=\"147.333\">" " <properties>" " <property name=\"select_1\" type=\"object\" value=\"9\"/>" " <property name=\"select_2\" type=\"object\" value=\"6\"/>" " </properties>" " <text wrap=\"1\">Опять грибы? Я скоро сам превращусь в гриб с таким рационом. Ладно, спасибо что предупредил. Будет время морально подготовиться.</text>" " </object>" " </objectgroup>" "</map>"; extern void xml_parser_example(); int main(){ xml_node_t *root = xml_parse(&xml_string, NULL); if(root != NULL){ printf("\n--- Парсинг XML прошел успешно ---\n\n"); printf("Корневой элемент: %s\n", root->name); // Вывод атрибутов корневого элемента printf("\nАтрибуты корневого элемента:\n"); for(int i = 0; i < root->attr_count; i++) { printf(" %s = %s\n", root->attributes[i].name, root->attributes[i].value); } // Поиск и вывод информации о слоях xml_node_t *layers = root->children; printf("\nНайденные слои:\n"); while(layers) { if(strcmp(layers->name, "layer") == 0 || strcmp(layers->name, "objectgroup") == 0) { printf(" Слой: %s (id=%s, name=%s)\n", layers->name, xml_get_attribute(layers, "id"), xml_get_attribute(layers, "name")); // Подсчет объектов в слое if(strcmp(layers->name, "objectgroup") == 0) { xml_node_t *obj = layers->children; int obj_count = 0; while(obj) { if(strcmp(obj->name, "object") == 0) { obj_count++; } obj = obj->next; } printf(" Найдено объектов: %d\n", obj_count); } } layers = layers->next; } // Поиск конкретных объектов xml_node_t *village = xml_find_node(root, "object"); if(village && xml_get_attribute(village, "name") && strcmp(xml_get_attribute(village, "name"), "Village") == 0) { printf("\nНайден объект 'Village':\n"); printf(" x = %s, y = %s\n", xml_get_attribute(village, "x"), xml_get_attribute(village, "y")); printf(" width = %s, height = %s\n", xml_get_attribute(village, "width"), xml_get_attribute(village, "height")); } // Поиск объектов с определенными типами xml_node_t *current = root->children; printf("\nОбъекты с типами Dialog и Monolog:\n"); while(current) { if(strcmp(current->name, "objectgroup") == 0) { xml_node_t *obj = current->children; while(obj) { if(strcmp(obj->name, "object") == 0) { char *type = xml_get_attribute(obj, "type"); if(type && (strcmp(type, "Dialog") == 0 || strcmp(type, "Monolog") == 0)) { printf(" %s (id=%s): %s\n", type, xml_get_attribute(obj, "id"), xml_get_attribute(obj, "name")); // Поиск текста внутри объекта xml_node_t *text_node = xml_find_node(obj, "text"); if(text_node && text_node->value) { printf(" Текст: %s\n", text_node->value); } } } obj = obj->next; } } current = current->next; } // Поиск объектов с определенными свойствами printf("\nОбъекты с свойством 'need_quest_id':\n"); current = root->children; while(current) { if(strcmp(current->name, "objectgroup") == 0) { xml_node_t *obj = current->children; while(obj) { if(strcmp(obj->name, "object") == 0) { xml_node_t *properties = xml_find_node(obj, "properties"); if(properties) { xml_node_t *property = properties->children; while(property) { if(strcmp(property->name, "property") == 0) { if(xml_get_attribute(property, "name") && strcmp(xml_get_attribute(property, "name"), "need_quest_id") == 0) { printf(" Объект %s (id=%s) требует квест %s\n", xml_get_attribute(obj, "name"), xml_get_attribute(obj, "id"), xml_get_attribute(property, "value")); break; } } property = property->next; } } } obj = obj->next; } } current = current->next; } // Сериализация и вывод XML char *serialized = xml_serialize(root); if(serialized) { printf("\n--- Сериализованный XML ---\n\n"); printf("%s\n", serialized); free(serialized); } // Освобождение памяти xml_free_node(root); } else { printf("Ошибка парсинга XML строки\n"); return 1; } return 0; }