UPDATE 1如何在ACE中加載XML?
這是我當前如何將文本加載到我的WT項目中。
wApp->require("ace.js");
//orignal XML, reads in incorrectly on one line
//std::string data = ReadFile("Q:\\settings.xml");
//XML after being formatted in notepad to look like xml, reads in correctly
//std::string data = ReadFile("Q:\\settings.txt");
//changed extension back to XML, edited in notepad++ to XML format, reads in correctly
std::string data = ReadFile("Q:\\settings_from_text.xml");
//test xml tag, reads in correctly
//std::string data = "<tag_1>some tag content</tag_1>";
//test xml tag with newline, reads in incorrectly on one line, doesnt read newline
//std::string data = "<tag_1>some tag content</tag_1>\n<tag_1>some tag content</tag_1>";
_ace_editor = new WText(data, Wt::PlainText);
//_ace_editor->setText(data);
_ace_editor->setInline(false);
// A WContainerWidget is rendered as a div
_ace_editor->resize(1000, 500);
std::string editor_ref = _ace_editor->jsRef(); // is a text string that will be the element when executed in JS
std::string command =
editor_ref + "._ace_editor = ace.edit(" + editor_ref + ");" +
editor_ref + "._ace_editor.setTheme(\"ace/theme/chrome\");" +
editor_ref + "._ace_editor.getSession().setMode(\"ace/mode/xml\");";// +
//editor_ref + "._ace_editor.setValue(\"" + data + "\");";
_ace_editor->doJavaScript(command);
而且,這裏是ReadFile函數
std::ifstream in(path, std::ios::in | std::ios::binary);
if(in)
{
std::string contents;
in.seekg(0, std::ios::end);
contents.resize(in.tellg());
in.seekg(0, std::ios::beg);
in.read(&contents[0], contents.size());
in.close();
return(contents);
}
throw(errno);
原帖
我試圖將一些XML文件加載到我嵌入在WT的ACE(http://ajaxorg.github.io/ace/#nav=about)編輯器(http://www.webtoolkit.eu/wt?wtd=rqBfShGlNupXgK3M1sWOxUk1Loz3BsW0)頁面。問題在於,無論出於何種原因,XML文件都會從加載中省略所有標籤。例如:作爲
some tag content
我需要整個XML文件就是標籤的不只是內容,其內容如下
<?xml version="1.0"?>
<settings>
<tag_1>some tag content</tag_1>
<tag_2/>
</settings>
的XML文件將被載入。
經過一番研究,我發現不同論壇上有不少其他人在問相同的事情,但我迄今爲止嘗試過的所有東西都沒有工作,這使我在這裏。
這包括將Ace模式設置爲XML,嘗試在將其設置爲ace窗口之前,將文本加載到不同的容器中,更改顏色方案並以不同方式解析文件。
我使用Visual Studio 2010,並從調試,我可以看到,文件確會讀完全與所有標記的字符串,但它被設置爲王牌窗口後,他們被省略。
那麼,它的工作原理是讀取標籤,但XML通過一行來完成。 <?XML版本= 「1.0」?> \t 一些標籤內容 \t 所以它是接近的,但也不能令人信服。關於如何讓它讀取XML中的換行符的任何想法?另外,我注意到,如果我在記事本中打開該XML文件,它將在一行中打開,與Ace中顯示的方式相同。我做了一個副本,格式化爲記事本中的xml文件,使用enter爲換行符,tab爲間距。讀取該文件是成功的,看起來像XML。也許錯誤是在文件保存的方式?有任何想法嗎? –
user2115945
2013-04-24 13:56:54
好的,我明白了。我的XML文件保存爲UNIX格式,而不是Windows。這是什麼導致它被加載在一行上。我按照你的建議閱讀它,但在一行上閱讀它是一個單獨的問題,可以在這裏看到http://stackoverflow.com/questions/16201869/why-does-xml-look-differently-in-notepad - 和 - 記事本 – user2115945 2013-04-25 11:33:07