简体   繁体   English

如何使用JavaScript显示XML

[英]How to Display XML With JavaScript

I have this XML: 我有这个XML:

  <school>
   <name>Big school</name>
   <located>somewhere</located>
   <age>163</age>
   <students>53354</students>
 </school>

I want to add new element to this XML and display it in a browser. 我想向该XML添加新元素并在浏览器中显示它。 I can use DOM methods to add new element but I do not know how to display it in a browser. 我可以使用DOM方法添加新元素,但是我不知道如何在浏览器中显示它。 I found this http://www.ehow.com/how_5941838_display-xml-javascript.html but I did not make it work. 我找到了这个http://www.ehow.com/how_5941838_display-xml-javascript.html,但是我没有使它起作用。

I want to see something like this in a browser: 我想在浏览器中看到以下内容:

<school>
   <name>Big school</name>
   <located>somewhere</located>
   <age>163</age>
   <students>53354</students>
   <teachers>18752</teachers>
 </school>

Can i do this in some simple way? 我可以用一些简单的方法吗?

You could just wrap it in a xmp element, which will preserve the formatting and display the markup (not interpret it as HTML). 您可以将其包装在xmp元素中,该元素将保留格式并显示标记(而不是将其解释为HTML)。

<xmp>
    <school>
        <name>Big school</name>
        <located>somewhere</located>
        <age>163</age>
        <students>53354</students>
        <teachers>18752</teachers>
    </school>
</xmp>

Note though that this tag is non standard and has been deprecated as the others noted, but it still works in all major browsers and there seems to be no decent alternative to it (without using JS or escaping the content). 请注意,尽管此标记是非标准的,并且已像其他标记所指出的那样被弃用,但它仍可在所有主要浏览器中使用,并且似乎没有替代方法(无需使用JS或转义内容)。

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

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