Конечно, есть уже 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
Я думаю, что я ничего не пропуская. Не забудьте закрыть файл. Кроме того, я расстаюсь с концепцией о том, что структура файла xt qt & может отличаться и у него нет сложных дочерних узлов. Если вы заранее знаете структуру qt-файла, это намного проще, и вы можете использовать DOM-парсер для записи XML.
Если вы хотите преобразовать qml-код в xml, pickq qqml lexer/parser и создать посетителя – nib
у вас есть ссылка на запуск? –
[источники QQmlJsAstVisitor] (https://qt.gitorious.org/qt/qtdeclarative/source/5a9f0131f8c0b7424a4d323b0f65237206be45ea:src/qml/parser/qqmljsastvisitor_p.h) [посетитель пример использования в QtCreator] (https: // кварты. gitorious.org/qt-creator/qt-creator/source/dfae73650553d86eb3477be36a32989a0fed89ad:tests/auto/qml/codemodel/check/tst_check.cpp#L130) – nib