9

How to load the xml file content to a div.

Here is my HTML in which I need to load XML content

<div class="editorial" id="news-container">
</div>

XML Data

<contentprogramming>
    <category name = "My t" id = "1">
        <logo>http://123.png</logo>        
        <channel name="res" id= "1" type="ecommerce">            
            <logo>http://11.png</logo>           
            <items>              
                <item id="1">
                    <image>http://11.jpg</image>
                    <title>Memory Card MSMT2G</title>
                    <details>$6.99</details>
                    <linkurl>http://www.test.com/Sony</linkurl>
                </item>
                <item id="2">
                    <image>http://i2.jpg</image>
                    <title>Apple iPad </title>
                    <details>$579.95</details>
                    <linkurl>http://www.test.com/Apple</linkurl>
                </item> 
            </items>
        </channel>
    </category>
</contentprogramming>

And xml file name is something.xml

I tried several ways but it didn't work :(

I tried the below method but didn't work.

$('something.xml').find('name').each(function() { 
    $("#news-container").append($(this).text() + "<br />");
}); 
ndsmyter
  • 6,535
  • 3
  • 22
  • 37
Sowmya
  • 26,684
  • 21
  • 96
  • 136
  • I think this can help you,`http://stackoverflow.com/questions/13534945/use-jquery-to-load-xml-file-edit-it-then-save-to-server-again-with-php` – dreamweiver May 07 '13 at 10:53

5 Answers5

20

Try this:

// Load the xml file using ajax 
$.ajax({
    type: "GET",
    url: "something.xml",
    dataType: "xml",
    success: function (xml) {

        // Parse the xml file and get data
        var xmlDoc = $.parseXML(xml),
            $xml = $(xmlDoc);
        $xml.find('category[name="My t"] logo').each(function () {
            $("#news-container").append($(this).text() + "<br />");
        });
    }
});

Reference: 1. jQuery.ajax() 2. parseXML()

palaѕн
  • 72,112
  • 17
  • 116
  • 136
  • Please check the updated question. I have added XML data to be added. pls can u try with that data? Thanks – Sowmya May 07 '13 at 11:36
  • Can't find the `name` tag in your sample xml. Please check once! – palaѕн May 07 '13 at 11:37
  • name="Myt" This is the one – Sowmya May 07 '13 at 11:40
  • 1
    [As per this discussion](http://stackoverflow.com/questions/10147499/parsexml-not-working-with-valid-xml), the XML should already be parsed when retrieved from server, so you shouldn't need to call `$.parseXML(xml)` again in the success callback. – Josh Weston Apr 28 '17 at 15:46
  • How does it work for a dynamically generated XML document on the client side, e.g. when a page loads? And what happens when such an XML document has an associated XSLT? – Aleksey F. Nov 11 '20 at 16:10
3

You need to escape HTML special chars. Use this function before setting text into div.

function htmlSpecialChars(unsafe) {
    return unsafe
    .replace(/&/g, "&amp;")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;")
    .replace(/"/g, "&quot;");
}
dino.keco
  • 1,401
  • 1
  • 12
  • 18
2

-- you must call xml file first

    $.ajax({
       url: '/something.xml',
       type: "GET",
       dataType: "xml",
       success: function (result) {
            $(result).find('name').each(function () {
                $("#news-container").append($(this).text() + "<br />");
             }
           }
       });
Ahmed Alnahas
  • 263
  • 1
  • 4
2

If you have a local file you want to read like I do, you can do the following:

<input type="file" id="file" accept="text/xml">
<script type="text/javascript">
document.getElementById('file').addEventListener('change', function (evt) {
    if (!evt.target.files){
        return;
    }

    var file = evt.target.files ? evt.target.files[0] : null,
        reader = new FileReader();

    reader.onload = function (evt) {
        console.log(evt.target.result);
    };

    reader.readAsText(file);
});
</script>

This requires the user to select the file, but it does get you the contents of the file. It uses the HTML5 FileReader API.

jstaab
  • 3,449
  • 1
  • 27
  • 40
0

Try this:

var xml='<contentprogramming><category name = "My t" id = "1"><logo>http://123.png</logo><channel name="res" id= "1" type="ecommerce"><logo>http://11.png</logo> <items><item id="1"><image>http://11.jpg</image><title>Memory Card MSMT2G</title><details>$6.99</details><linkurl>http://www.test.com/Sony</linkurl></item><item id="2"><image>http://i2.jpg</image><title>Apple iPad </title><details>$579.95</details><linkurl>http://www.test.com/Apple</linkurl></item> </items></channel></category></contentprogramming>';
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$xml.find( "category[name='My t'] logo" ).each(function () {
    $("#news-container").append($(this).text() + "<br />");
}
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106