diff --git a/doc/en/scripting/filesystem.md b/doc/en/scripting/filesystem.md index 03976a7f8..12a190daa 100644 --- a/doc/en/scripting/filesystem.md +++ b/doc/en/scripting/filesystem.md @@ -101,8 +101,9 @@ xml.tostring(data: XML, multiline: bool = true) -> str xml.parse(code: str) -> XML -- Parses the VCD (VoxelCore Declaration) format used by the VCM format. --- root_tag - root element tag (the format does not require an explicit root) -xml.parse(code: str, root_tag: str = "root") -> XML +-- root_tag - a root element with the specified tag will be implicitly created, +-- containing all root elements +xml.parse_vcd(code: str, root_tag: str = "root") -> XML ``` ## *bjson* library diff --git a/doc/ru/scripting/filesystem.md b/doc/ru/scripting/filesystem.md index 6334b5e90..a3bee78a6 100644 --- a/doc/ru/scripting/filesystem.md +++ b/doc/ru/scripting/filesystem.md @@ -101,8 +101,9 @@ xml.tostring(data: XML, multiline: bool = true) -> str xml.parse(code: str) -> XML -- Парсит формат VCD (VoxelCore Declaration), используемый форматом VCM. --- root_tag - тег корневого элемента (формат не требует наличия явного) -xml.parse(code: str, root_tag: str = "root") -> XML +-- root_tag - будет неявно создан корневой элемент с указанным тегом, +-- содержащий все корневые элементы +xml.parse_vcd(code: str, root_tag: str | nil = nil) -> XML ``` ## Библиотека bjson diff --git a/src/logic/scripting/lua/libs/libxml.cpp b/src/logic/scripting/lua/libs/libxml.cpp index ff52b6321..70ccc36bb 100644 --- a/src/logic/scripting/lua/libs/libxml.cpp +++ b/src/logic/scripting/lua/libs/libxml.cpp @@ -82,9 +82,19 @@ static int l_parse_vcd(lua::State* L) { auto string = lua::require_string(L, 1); auto rootTag = lua::tostring(L, 2); auto document = xml::parse_vcm( - "[string]", string, rootTag ? rootTag : DEFAULT_ROOT_TAG + "[string]", string, rootTag ? rootTag : "" ); - return push_xml(L, *document->getRoot()); + const auto& root = *document->getRoot(); + if (rootTag != nullptr) { + return push_xml(L, root); + } + int rootElements = root.size(); + if (rootElements != 1) { + throw std::runtime_error( + "one root element expected, got " + std::to_string(rootElements) + ); + } + return push_xml(L, *root.getElements()[0]); } const luaL_Reg xmllib[] = {