如何在ACE中加载XML

How to load XML in ACE?

本文关键字:加载 XML ACE      更新时间:2023-10-16

UPDATE 1

这是我目前如何加载文本到我的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 (http://www.webtoolkit.eu/wt?wtd=rqBfShGlNupXgK3M1sWOxUk1Loz3BsW0)页面中的Ace (http://ajaxorg.github.io/ace/#nav=about)编辑器中。问题是,无论出于何种原因,XML文件的所有标记都从加载中省略了。示例:包含以下内容的XML文件

<?xml version="1.0"?>
<settings>
    <tag_1>some tag content</tag_1>
    <tag_2/>
</settings>

将被加载为

some tag content

我需要完整的XML文件,而不仅仅是标签的内容。

在做了一些研究之后,我发现在不同的论坛上有相当多的其他人问同样的事情,但到目前为止我所尝试的一切都没有起作用,这把我带到这里。

这包括将Ace模式设置为XML,在将其设置为Ace窗口之前尝试将文本加载到不同的容器中,更改配色方案,以及以不同的方式解析文件。

我使用的是visual studio 2010,从调试中我可以看到,该文件确实被完全读入一个带有所有标签的字符串,但在它被设置为Ace窗口后,它们被省略了。

无论你是否把它放在WT页面上,这都是一个javascript问题,因为这就是ACE编辑器,一个javascript工具。由于您没有显示任何有关如何加载xml内容的内容,因此我只能推测您一定是在将xml文件的内容写入页面输出源中。我敢打赌,如果你查看源代码,你看到标签了吗?如果是这样,那你就错了。xml文件需要通过javascript/ajax加载,我将在下面用一个完整的工作示例来演示(编辑$. js中的'url')。(对服务器上XML文件位置的Ajax调用),它显示标记和XML文件的所有内容。添加jQuery库只是为了简化ajax请求代码。享受吧!

<!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加载"的一些内容,因为我现在意识到您只是遵循ACE的示例,即事先将内容放入编辑器div中。如果你想这样做与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>

XML中的XML片段…您可以期望浏览器能够解释它们,除非正确转义。试试这个:

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

将转义文本中所有类似xml的字符。

Wt的默认值(XHTMLText)将尝试将您的输入解析为XML,如果成功,则在将其作为XML发送到浏览器之前从XML中过滤可能的XSS向量。如果它不能将文本解析为XML,它将转义XML字符,以避免具有自由解析器的浏览器无意中执行攻击向量。

第三个选项(XHTMLUnsafeText)绕过XSS过滤-危险的,所以只有当你知道你的文本是安全的,不能被用户直接或间接影响时才使用它。