Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
etorth committed Jan 12, 2025
1 parent ec2ec45 commit e93ff5a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ ADD_SUBDIRECTORY(followuidmagiceditor)
ADD_SUBDIRECTORY(wtldecoder)
ADD_SUBDIRECTORY(bgmlistdecoder)
ADD_SUBDIRECTORY(soundlistdecoder)

ADD_SUBDIRECTORY(xmltran)
1 change: 1 addition & 0 deletions tools/xmltran/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ADD_SUBDIRECTORY(src)
12 changes: 12 additions & 0 deletions tools/xmltran/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
AUX_SOURCE_DIRECTORY(. XMLTRAN_SRC)
ADD_EXECUTABLE(xmltran ${XMLTRAN_SRC})
ADD_DEPENDENCIES(xmltran mir2x_3rds)

TARGET_INCLUDE_DIRECTORIES(xmltran PRIVATE ${MIR2X_COMMON_SOURCE_DIR})
TARGET_INCLUDE_DIRECTORIES(xmltran PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
TARGET_INCLUDE_DIRECTORIES(xmltran PRIVATE ${CMAKE_CURRENT_LIST_DIR})

TARGET_LINK_LIBRARIES(xmltran common)
TARGET_LINK_LIBRARIES(xmltran ${TINYXML2_LIBRARIES} )

INSTALL(TARGETS xmltran DESTINATION tools/xmltran)
70 changes: 70 additions & 0 deletions tools/xmltran/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// from: https://gist.github.com/Lee-swifter/d9cd651b093f0d32b65a2bce47b0ad91
#include <iostream>
#include "tinyxml2.h"

using namespace std;
using namespace tinyxml2;

void xmlTran(tinyxml2::XMLNode* node) {
if(node == nullptr)
return;

if(node->ToDeclaration()) {
auto declaration = dynamic_cast<tinyxml2::XMLDeclaration*>(node);
cout << "XML 声明,value=" << declaration->Value() << endl;
}
if(node->ToElement()) {
auto element = dynamic_cast<tinyxml2::XMLElement*>(node);
cout << "XML 元素,name=" << element->Name() << ", value=" << element->Value() << endl;
const tinyxml2::XMLAttribute* attribute = element->FirstAttribute();
while (attribute != nullptr) {
cout << "\t属性 " << attribute->Name() << "=" << attribute->Value() << endl;
attribute = attribute->Next();
}
}
if(node->ToText()) {
auto text = dynamic_cast<tinyxml2::XMLText*>(node);
cout << "XML 文本:" << text->Value() << endl;
}
if(node->ToComment()) {
auto comment = dynamic_cast<tinyxml2::XMLComment*>(node);
cout << "XML 注释:" << comment->Value() << endl;
}
if(node->ToUnknown()) {
auto unknown = dynamic_cast<tinyxml2::XMLUnknown*>(node);
cout << "XML 未知:" << unknown->Value() << endl;
}
if(node->ToDocument()) {
auto document = dynamic_cast<tinyxml2::XMLDocument*>(node);
cout << "XML 文档:" << document->ErrorName() << endl;
}

if(node->NoChildren()) {
return;
}

tinyxml2::XMLNode* child = node->FirstChild();
while(child != nullptr) {
xmlTran(child);
child = child->NextSibling();
}
}

int main(int argc, char *argv[])
{
if(argc == 2){
tinyxml2::XMLDocument xmlDoc;
tinyxml2::XMLError error = xmlDoc.LoadFile(argv[1]);
if(error != tinyxml2::XML_SUCCESS) {
std::cout << "Failed to read XML: " << xmlDoc.ErrorStr() << endl;
return EXIT_FAILURE;
}

xmlTran(&xmlDoc);
return EXIT_SUCCESS;
}
else{
std::cout << "Usage: xmltran <input.xml>" << std::endl;
return EXIT_FAILURE;
}
}

0 comments on commit e93ff5a

Please sign in to comment.