qt项目代码到xml文件的转换
qt project code to xml file transformation
我想用c++或java将一些qt项目文件转换为xml例如,代码进行这种转换:
TextInput {
id: textInput2
x: 247
y: 161
width: 80
height: 20
}
拥有:
< TextInput >
< id> textInput2< /id>
< x> 247< /x>
< y> 161< /y>
< width> 80< /width>
< height> 20 < /height>
< /TextInput >
你对此有什么想法吗?我必须应用什么技术才能将qt转换为xml?
edit:我尝试了SAXXMLPARSER,但代码不知道如何读取。
感谢
当然已经有一个lib使它成为可能,但我不知道,所以,如果你想用代码来做,你可以试着把它读成纯文本,并使用BufferedReader和几个循环手动进行翻译。
试试这个:
BufferedReader qtIn = new BufferedReader(new FileReader("example.qt")); //I don't know if you can read it as plain text straight.
String tag
String metaTag
String lineIn
String lineOut
BufferedWriter writer = new BufferedWriter(new FileWriter("example.xml"));
//here you should use writer to write down the heading of the xml file.
while ((lineIn = qtIn.readLine()) != null) { // while loop begins here. lineIn is the string where reader stores current line read.
if (lineIn.charAt(lineIn.length() - 1) == "{"){ //if line's last character is an opening brace ({)
metaTag = lineIn.subString(0, lineIn.length() - 1).trim(); //we store it in string metaTag
lineOut = "<"+metaTag+">n"; //and write metaTag as opening XML tag
writer.write (lineOut,0,lineOut.length());
}else if (lineIn.trim() == "}"){ //else, if it's a closing brace (})
lineOut = "</"+metaTag+">n"; //we write metaTag as closing XML tag
writer.write (lineOut,0,lineOut.length());
}else{ // if it's not an opening or closing brace
String[] element = lineIn.split(":"); //we split the line in element name and element data using the colon as splitter. don't forget to use trim method on both parts.
tag = element[0].trim(); //this is optional, you can replace it by using element[0].trim() instead in the next line, I added it just to make it clearer
lineOut = "<" + tag + ">" + element[1].trim() + "</" + tag +">n" // here, we take two element parts and write them as XML tag and plain text.
writer.write (lineOut,0,lineOut.length());
}
} // end while
//here you should write the footing of the file, if there's any.
writer.close(); // don't forget to close writer
我想我没有错过任何东西。别忘了关闭你的文件。此外,我要放弃qt&xml文件可能会有所不同,并且它并没有复杂的子节点。如果您事先知道qt文件的结构,它会容易得多,并且您可以使用DOM解析器来编写XML。
相关文章:
- 将生成文件转换为 CMakeLists.txt等效项
- 如何将 UTF-8 文本从文件转换为某个可以迭代的容器,并检查每个符号是否为C++字母数字?
- 如何使用 CreateProcess 调用 tshark 将捕获文件转换为 txt
- 使用emscripten将c++文件转换为wasm时出错
- 将Qt qml文件转换为位图图像
- (对于C++)读取类型从文件转换为 2D 数组
- 使用向量将字符串从C 中的文件转换
- 编写一个 C++ 程序以将文本文件转换为 HTML 文件
- 如何在 C++ 中将矩阵市场文件转换为矩阵
- 是否可以将信息从XML文件转换为CPP
- 将 ASCII 文件转换为 C++ 的二进制格式
- 将生成文件转换为CMakeLists;无法加载共享库
- FlatBuffers:未支撑的工会向量错误将JSON文件转换为二进制文件
- 如何使用 qt 将 Linux 文本文件转换为 Windows 文本文件
- 如何将可执行文件转换为 C++ 文件以达到代码峰值
- 将windows软件中的未知数据库文件转换为MySqli数据库
- 如何将 .npy 文件转换为 .binaryproto
- 使用ICEBuilder将ICE文件转换为C++
- 将文件转换为字符串数组C++
- 在编译时将文本文件转换为字符串