使用 jquery/javascript 将 XML 转换为 HTML

问题描述 投票:0回答:1

我有一些

XML
,我需要在
div
中显示为文本。
我们可以将这个
XML
转换为如下格式吗?

<root>
<field>
  <label>Have you invested before</label>
  <value>No</value>
</field>
<field>
  <label>Are you looking to invest in the next 6 months</label>
  <value>Maybe</value>
</field>
<field>
  <label>What investments are you interested in</label>
  <value>Carbon Credits, Green Investments</value>
</field>
<field>
  <label>How much are you looking to invest</label>
  <value>£50,000 -  £100,000</value>
</field>
</root>

输出应如下所示:

您之前是否投资过:否
您是否打算在未来 6 个月内进行投资:也许
您对什么投资感兴趣:碳信用额、绿色投资
您打算投资多少:£50,000 - £100,000

这可以使用 Jquery/javascript 实现吗??

它应该像下面的 HTML 一样渲染。

<div class="how-to">
<div class="how-text">
    <h3>Your Requirements</h3>
        <ul class="requirements">         
<li><label class="field-l">Have you invested before:</label> <span>No</span></li>         
<li><label class="field-l">Are you looking to Invest in the next 6 months:
</label>      <span>Maybe</span></li>         
<li><label class="field-l">What Investments are you interested in:</label> 
<span>Carbon  Credits,Green Investments</span></li>
 <li><label class="field-l">How much are you looking to invest:</label>
  <span>£50,000 -  £100,000</span></li>
      <li class="link-pad"><a href="index.html" class="requirements">
     Change  your requirements</a></li>
    </ul>
<div class="clear"></div>
 </div>
  </div>
javascript jquery html xml
1个回答
27
投票

第 1 步:验证您的 xml

您的 xml 无效。您可以检查它是否有效,例如,在在线验证器中。有很多,我链接的这个只是一个例子。

对于这个答案,假设我们有一些如下的 xml:

<root>
  <field>
      <label>Have you invested before</label>
      <value>No</value>
  </field>
  <field>
      <label>Are you looking to invest in the next 6 months</label>
      <value>Maybe</value>
  </field>
  <field>
      <label>What investments are you interested in</label>
      <value>Carbon Credits, Green Investments</value>
  </field>
  <field>
      <label>How much are you looking to invest</label>
      <value>£50,000 -  £100,000</value>
  </field>
</root>

Step2:获取xml,也许通过ajax

我认为这是显而易见的,但无论如何我都会将其包含在这里。

$.get('/url_of_the_xml_resource')
  .done(function(data){
    // this function is executed if the request was sucessfull
  })
  .fail(function(){
    // this function is executed if the request fails
  })
;

第3步:解析xml

您可以使用 jQuery 的 $.parseXML 来解析它并将原始数据转换为 XML 文档。

$.get('/url_of_the_xml_resource')
  .done(function(data){
    // parse the xml
    data = $.parseXML(data);
    //
    // do anything you want with the parsed data
  })
  .fail(function(){
    alert('something went wrong!');
  })
;

第 4 步:使用数据

现在我们有一个 xml 文档可以使用。下面的 snipnet 假设我们的 HTML 布局中有一个定义列表,

<dl>
标签,并且应该在解析数据后执行,就像上一步一样。

// first we query the HTML document to get the list element
// and store it for later use
var list = $('dl');
// data is a xml document now, so we query it...
$(data)
  // and search for all <field> elements
  .find('field')
  // now we can play with each <field>
  .each(function(index, element){
    // as example we query & store the field
    var field = $(element)
    // get the values we want
    var label = field.find('label').text()
    var value = field.find('value').text()
    // and append some html in the <dl> element we stored previously
    list
      .append('<dt>'+label+': </dt>')
      .append('<dd>'+value+'</dd>')
    ;
  })
;

结论

jQuery 是你想要使用的。它的可链接性质使得遍历 DOM 就像切黄油一样。希望这个答案对你有用。

作为附加参考,请参阅 jsfiddle 上的 完整示例

© www.soinside.com 2019 - 2024. All rights reserved.