简体   繁体   中英

How to embed an XML file in HTML?

I am creating a simple web application for a comic using HTML and JavaScript. Because people that are not very technical should be able to add new comics, I thought about using an XML file as a configuration file.

I thought about using JSON and decided against it, mainly because the way comma's are used(no comma between two items breaks it, but a comma at the end of the list also breaks it.).

However, my question is now: How can I embed this XML file? Do I use the <link rel= /> tag, or something else like the <embed src= /> tag? And how can I then read information from the XML nodes in JavaScript?

I would recommend loading a JavaScript library that makes this easy. jQuery is one. If you include jQuery in your page then you use it to load the document and get access to the browser's XML parsing capabilities fairly easily.

http://api.jquery.com/jQuery.parseXML/ shows a simple example of finding values in an xml document once it's loaded.

This site http://think2loud.com/224-reading-xml-with-jquery/ gives a good example of how to load XML from a remote site. The basic idea is to use AJAX: here's a tiny snippet for posterity that will load foo.xml after the html document has loaded (relies on jQuery):

$( function() {
    $.ajax({
        type: "GET",
    url: "foo.xml",
    dataType: "xml",
    success: myHandler(xml) {
    }
    });
});

Use xml tag. Example :

<html>
  <xml Id = msg>
      <message>
        <to> Visitors </to>
        <from> Author </from>
        <Subject> XML Code Islands </Subject>            
      </message>
  </xml>
</html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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