Qqwy
Qqwy

Reputation: 5629

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?

Upvotes: 5

Views: 12879

Answers (2)

Mike Sokolov
Mike Sokolov

Reputation: 7044

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) {
    }
    });
});

Upvotes: 8

Iswanto San
Iswanto San

Reputation: 18569

Use xml tag. Example :

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

Upvotes: -1

Related Questions