qt项目代码到xml文件的转换

qt project code to xml file transformation

本文关键字:文件 转换 xml 项目 代码 qt      更新时间:2023-10-16

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