java - qt项目代码到xml文件转换

标签 java c++ xml qt

我想用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?

编辑:我试过 SAX XML PARSER 但代码不知道如何阅读。

谢谢

最佳答案

肯定已经有一个库可以实现,但我不知道,所以,如果你想通过代码来完成,你可以尝试以纯文本形式阅读它,并使用 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。

关于java - qt项目代码到xml文件转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26859707/

相关文章:

java - 为什么在 JSON 负载请求正文中发送 http 请求元数据被认为是不好的做法,而最好使用请求 header ?

java - mvn 安装后检查损坏的 war 文件

python - 在 Cython 中包装自定义类型 C++ 指针

c++ - 使用C++11的begin()和end()函数通过参数确定数组维数

c++ - 从机器名称中查找机器 IP 地址

xml - 为什么创建 'new xml file' 选项不存在?

c++ - 在 ui 文件中隐藏 QTabWidget 中的单个选项卡 Pane ?

java - 在 Android 中重命名字符串

Java Swing : how to reflect changes made to xml file immediately to visual tree?

java - 图像轮廓点生成器?