简体   繁体   English

如何在javascript中显示xml?

[英]how to display xml in javascript?

As the question says, it just escaped my memory how to display xml in javascript, I want to display some source xml within an div on a page, that sits next to the processed result of the xml in another div. 正如问题所说,它只是逃避了我的记忆如何在javascript中显示xml,我想在页面上的div中显示一些源xml,它位于另一个div中xml的处理结果旁边。

Can't remember if there was an equivalent to javascript's escape to convert entities on the client 不记得是否有相当于javascript的转义来转换客户端上的实体

Note : the xml files are served as is from the server, so I need a client side solution 注意 :xml文件是从服务器端提供的,因此我需要一个客户端解决方案

Note : the main problem is XML doesn't render correctly in most browsers, all the brackets and attributes disappear, leaving you with text that doesn't look like xml 注意 :主要问题是XML在大多数浏览器中无法正确呈现,所有括号和属性都消失了,留下的文本看起来不像xml

Fetch your XML using Ajax and simply put the result in a textarea . 使用Ajax获取XML并简单地将结果放在textarea Style the textarea any way you want. 以任何您想要的方式设置textarea样式。

Here's a function for you: 这是给你的功能:

<script type="text/javascript">
<!--
    function xml_to_string(xml_node)
    {
        if (xml_node.xml)
            return xml_node.xml;
        else if (XMLSerializer)
        {
            var xml_serializer = new XMLSerializer();
            return xml_serializer.serializeToString(xml_node);
        }
        else
        {
            alert("ERROR: Extremely old browser");
            return "";
        }
    }

    // display in alert box
    alert(xml_to_string(my_xml));

    // display in an XHTML element
    document.getElementById("my-element").innerHTML = xml_to_string(my_xml);
-->
</script>

If you want the browser to render your XML as XML , it must be in it's own document, like an iframe , or a frame . 如果您希望浏览器将 XML 呈现XML ,则它必须位于自己的文档中,如iframeframe DIV won't do the job! DIV不会做这个工作!

Besides, in the HTTP header of the request that serves the iframe you should send Content-Type: text/xml , so the Browser can understand that this content is an XML . 此外,在为iframe提供服务的请求的HTTP头中,您应该发送Content-Type: text/xml ,因此浏览器可以理解该内容是XML

But, if you truely need to see it in a DIV you will have to build yourself an XML rendering function XML2HTML . 但是,如果你真的需要在DIV中看到它,你将不得不自己构建一个XML呈现函数XML2HTML You can use the HTML PRE tag for that, if your XML string is already formatted (line breaks and tabs). 如果您的XML字符串已经格式化(换行符和制表符),则可以使用HTML PRE标记。 In that case you will have to replace < to &gt; 在这种情况下,您将不得不替换< to &gt; in the XML string. XML字符串中。

Conclusion: The browser can't render XML and HTML in the same document. 结论: The browser无法在同一文档中呈现XMLHTML If you need it, you just have to workaround it. 如果您需要它,您只需要解决它。

When you don't want to use a whole JavaScript framework just for this... 如果您不想仅仅为此使用整个JavaScript框架......

function insertLiteral(literalString, targetElement)
{
    var textNode = document.createTextNode(literalString);
    targetElement.appendChild(textNode)
    return textNode;
}

// test it (for the example, I assume #targetDiv is there but empty)
var xmlString = "<this><is_some><xml with='attributes' /></is_some></this>";
var targetElement = document.getElementById("targetDiv");
var xmlTextNode = insertLiteral(xmlString, targetElement);

#targetDiv could be styled using CSS: #targetDiv可以使用CSS设置样式:

#targetDiv {
  font-family: fixed;
  white-space: pre;
}

The problem is that you have to escape the characters that the HTML parser will interpret as markup: <, >, &, and in some cases " and ' 问题是你必须转义HTML解析器将解释为标记的字符:<,>,&,在某些情况下“和”

Core JavaScript doesn't provide this for you, but many of the add-on libraries do. 核心JavaScript不会为您提供此功能,但许多附加库都可以。

For example, Prototype has: http://www.prototypejs.org/api/string/escapeHTML 例如,Prototype有: http//www.prototypejs.org/api/string/escapeHTML

This is the easiest way I found to display XML as Text on the same page for copy and paste: 这是我发现在同一页面上将XML显示为Text以进行复制和粘贴的最简单方法:

var xml = document.getElementById('svg_element').innerHTML;
document.getElementById('svg_pre').innerText = xml; 

One technique would be to use two iframe elements with the src attribute set to the corresponding xml file available from the server (assuming it is exposed through a virtual directory): 一种技术是使用两个iframe元素,并将src属性设置为服务器提供的相应xml文件(假设它通过虚拟目录公开):

<iframe src="/myapp/Document1.xml"><iframe src="/myapp/Document2.xml">

Alternatively, you could potentially use an AJAX-type request to retrieve the xml documents and then embed them in the HTML document dynamically using something like: 或者,您可以使用AJAX类型的请求来检索xml文档,然后使用以下内容将它们动态地嵌入到HTML文档中:

getElementById("myDiv").innerText = ajax.response;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM