2013-05-07 43 views
9

如何將xml文件內容加載到div。使用jquery將xml文件內容加載到div

這裏是我的HTML中,我需要加載XML內容

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

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> 

和XML文件名是something.xml

我嘗試了幾種方法,但它沒」 t工作:(

我試過下面的方法,但沒有工作。

$('something.xml').find('name').each(function() { 
    $("#news-container").append($(this).text() + "<br />"); 
}); 
+0

我認爲這可以幫助你,'http://stackoverflow.com/questions/13534945/use-jquery-to-load-xml-file-edit-it-then-save-to-server-again-with -php' – dreamweiver 2013-05-07 10:53:49

回答

19

試試這個:

// 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 />"); 
     }); 
    } 
}); 

參考:1. jQuery.ajax() 2. parseXML()

+0

請檢查更新後的問題。我添加了要添加的XML數據。請問你可以試試這些數據嗎?謝謝 – Sowmya 2013-05-07 11:36:06

+0

在您的示例x​​ml中找不到'name'標籤。請檢查一次! – 2013-05-07 11:37:28

+0

name =「Myt」這是一個 – Sowmya 2013-05-07 11:40:10

3

您需要轉義HTML特殊字符。在將文本設置爲div之前使用此功能。

function htmlSpecialChars(unsafe) { 
    return unsafe 
    .replace(/&/g, "&amp;") 
    .replace(/</g, "&lt;") 
    .replace(/>/g, "&gt;") 
    .replace(/"/g, "&quot;"); 
} 
2

- 你必須調用XML文件中的第一

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

試試這個:

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 />"); 
} 
+0

請檢查更新的問題。我添加了要添加的XML數據。請問你可以試試這些數據嗎?謝謝 – Sowmya 2013-05-07 11:41:17

+0

「XML」中的'name'標籤在哪裏? – 2013-05-07 12:06:07

+0

檢查以上。 – 2013-05-07 12:32:40

2

如果你想讀像我這樣一個本地文件,你可以做到以下幾點:

<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> 

這需要用戶選擇文件,但它確實讓你的文件的內容。它使用HTML5 FileReader API