简体   繁体   English

将XML内容提取为HTML

[英]Fetch XML Content into HTML

I am a newbie to HTML/JS, so I am hoping you experts can help me out here with a small question. 我是HTML / JS的新手,所以希望您的专家在这里有一个小问题可以帮助我。 I have html5 music player with playlist, contents of which are currently mentioned within the index.html file as follows: 我有带有播放列表的html5音乐播放器,其内容当前在index.html文件中提到如下:

<div id="divListing">
    <ol id="olContents">
<li rel="http://www.abc.mp3"><strong>Song1</strong><a target="_blank" href=" ">Buy Song</a><em>Artist1</em></li>
<li rel="http://www.abcd.mp3"><strong>Song2</strong><a target="_blank" href=" ">Buy Song< /a><em>Artist2</em></li>
<li rel="http://www.abcde.mp3"><strong>Song3</strong><a target="_blank" href=" ">Buy Song</a><em>Artist1</em></li>
<li rel="http://www.abcdef.mp3"><strong>Song4</strong><a target="_blank" href=" ">Buy Song</a><em>Artist2</em></li>
    </ol>
</div>

My goal is to get <li> items from a XML file so that I don't have to keep updating the html file every time but instead if I change the strings on my external(dropbox like) source.XML file being maintained in the same syntax as follows: 我的目标是从XML文件中获取<li>项,这样我就不必每次都更新HTML文件,而是如果我更改了外部(像dropbox这样的)source上的字符串。相同的语法如下:

<?xml version="1.0" encoding="utf-8"?>

<data>               
<li rel="http://www.abc.mp3"><strong>Song1</strong><a target="_blank" href=" ">Buy Song</a><em>Artist1</em></li>
<li rel="http://www.abcd.mp3"><strong>Song2</strong><a target="_blank" href=" ">Buy Song</a><em>Artist2</em></li>
<li rel="http://www.abcde.mp3"><strong>Song3</strong><a target="_blank" href=" ">Buy Song</a><em>Artist1</em></li>
<li rel="http://www.abcdef.mp3"><strong>Song4</strong><a target="_blank" href=" ">Buy Song</a><em>Artist2</em></li>

</data>

I am not sure how to associate or call the XML file so that html can read them as if they were being called directly in html. 我不确定如何关联或调用XML文件,以便html可以像读取直接在html中一样读取它们。

Any code help you can share will be greatly appreciated. 您可以共享的任何代码帮助将不胜感激。

Thanks! 谢谢!

Without spelling out all the code, I'd use AJAX to make a request for the XML file. 在不弄清楚所有代码的情况下,我将使用AJAX来请求XML文件。 Then once you receive that data, you can use javascript/jQuery to insert that HTML into your list. 然后,一旦收到该数据,就可以使用javascript / jQuery将HTML插入列表中。

Follow these steps to get your solution 请按照以下步骤获取解决方案

  1. Create a Web Service/ Web Api which will return Songs XML from server. 创建一个Web服务/ Web Api,它将从服务器返回Songs XML。

  2. Call that webservice using jQuery Ajax 使用jQuery Ajax调用该Web服务

  3. Read response and create li HTML elements dynamically as per XML response. 读取响应并根据XML响应动态创建li HTML元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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