qt项目代码到xml文件转换

时间:2014-11-11 07:27:41

标签: 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,但代码不知道如何阅读。

谢谢

1 个答案:

答案 0 :(得分:0)

肯定已经有了一个可以实现的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&amp;的结构概念分道扬.. xml文件可能会有所不同,并且它没有复杂的子节点。如果您事先知道qt文件的结构,那么它会更容易,您可以使用DOM解析器来编写XML。