2013-04-22 81 views
1

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,並從調試,我可以看到,文件確會讀完全與所有標記的字符串,但它被設置爲王牌窗口後,他們被省略。

回答

1

XML中的XML片段......您可以以某種方式期望您的瀏覽器會解釋它們,除非正確轉義。試試這個:

txt = new WText("<bla>something</bla>", Wt::PlainText); 

這將在您的文本中轉義所有XML-ish字符。

重量的默認(XHTMLText)將嘗試解析您的輸入爲XML,如果它成功從XML發送它作爲XML瀏覽器之前過濾可能的XSS載體。如果它不能將文本解析爲XML,它將轉義XML-ish字符以避免具有自由分析器的瀏覽器無意中執行攻擊媒介。

第三個選項(XHTMLUnsafeText)繞過XSS過濾 - 危險,所以只有使用它時,你知道你的文字是安全的,並且不能由用戶直接或間接的影響。

+0

那麼,它的工作原理是讀取標籤,但XML通過一行來完成。 <?XML版本= 「1.0」?> \t 一些標籤內容 \t 所以它是接近的,但也不能令人信服。關於如何讓它讀取XML中的換行符的任何想法?另外,我注意到,如果我在記事本中打開該XML文件,它將在一行中打開,與Ace中顯示的方式相同。我做了一個副本,格式化爲記事本中的xml文件,使用enter爲換行符,tab爲間距。讀取該文件是成功的,看起來像XML。也許錯誤是在文件保存的方式?有任何想法嗎? – user2115945 2013-04-24 13:56:54

+0

好的,我明白了。我的XML文件保存爲UNIX格式,而不是Windows。這是什麼導致它被加載在一行上。我按照你的建議閱讀它,但在一行上閱讀它是一個單獨的問題,可以在這裏看到http://stackoverflow.com/questions/16201869/why-does-xml-look-differently-in-notepad - 和 - 記事本 – user2115945 2013-04-25 11:33:07

4

無論您是否將它放在WT頁面上,底線是一個JavaScript問題,因爲這是ACE編輯器的一個JavaScript工具。既然你沒有展示任何關於如何加載xml內容的事情,我只能推測你必須將xml文件的內容寫入頁面輸出源?如果你查看源代碼,你會看到標籤嗎?那麼如果是這樣,你就錯了。 xml文件需要通過javascript/ajax加載,因爲我將在下面演示一個完整的工作示例(編輯$ .ajax調用中的'url'到服務器上xml文件的位置),該文件顯示標籤和所有內容的xml文件。爲了簡化ajax請求代碼,添加了jQuery庫。請享用!

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<title>ACE in Action</title> 
<style type="text/css" media="screen"> 
    #editor { 
     position: absolute; 
     top: 0; 
     right: 0; 
     bottom: 0; 
     left: 0; 
    } 
</style> 
</head> 
<body> 

<div id="editor"></div> 

<script src="http://rawgithub.com/ajaxorg/ace-builds/master/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
<script> 
    var callback = function (data, status, xhr) { 
     //data will be the xml returned from the server 
     if (status == 'success') { 
      var editor = ace.edit("editor"); 
      //apparently, only modes supported are 'html', 'javascript' & 'text' 
      editor.getSession().setMode("ace/mode/html"); 
      editor.setValue(data); 
     } 
    }; 
    //using jQuery to fire off an ajax request to load the xml, 
    //using our callback as the success function 
    $.ajax(
     { 
      url : '/testing/cd_catalog.xml', 
      dataType : 'text', //explicitly requesting the xml as text, rather than an xml document 
      success : callback 
     } 
    ); 

</script> 
</body> 
</html> 

其實,我收回我說過的一些「必須通過JavaScript/AJAX加載」,因爲我現在知道你剛纔下面把內容插入到編輯器的div事先的ACE的例子。如果您希望使用html或xml內容來完成此操作,則標記將由瀏覽器進行評估,並且不會顯示出來,除非您複製編輯器div的innerHTML,然後實例化編輯器,然後將其值設置爲先前保存的innerHTML。例如:

<div id="editor"><?xml version="1.0" encoding="ISO-8859-1"> 
<books> 
<text>some text content</text> 
<book/> 
</books></div> 

<script src="http://rawgithub.com/ajaxorg/ace-builds/master/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script> 
<script> 
    var txt = document.getElementById('editor').innerHTML; 
    var editor = ace.edit("editor"); 
    //editor.setTheme("ace/theme/monokai"); 
    editor.getSession().setMode("ace/mode/html"); 
    editor.setValue(txt); 
</script> 
+0

這有點讓人難以理解,因爲我不是在JavaScript很流利,但我可以告訴你告訴我,該文件需要使用JavaScript在創建ACE窗口時加載。問題在於我使用ACE和WT的原因是我必須儘可能少地使用JavaScript。我在帖子中添加了更多信息,希望它能夠幫助顯示我實際正在做的事情。另一方面,在javascript中使用setValue會正確設置XML標記,但如果文件中存在換行符,則不會加載。 – user2115945 2013-04-24 14:19:35