Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 718
Qqwy Avatar asked Jan 27 '13 11:01

Qqwy


People also ask

How do I display XML content in HTML?

To view raw XML source, try to select "View Page Source" or "View Source" from the browser menu. Note: In Safari 5 (and earlier), only the element text will be displayed. To view the raw XML, you must right click the page and select "View Source".

How do I add an XML file to my website?

Create a folder in your website called "XMLFiles". Create a file in that folder called "Videos. xml" and place your XML in the file.


Video Answer


1 Answers

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) {
    }
    });
});
like image 136
Mike Sokolov Avatar answered Sep 24 '22 05:09

Mike Sokolov