drjtua5953 2014-04-30 13:22
浏览 41
已采纳

如何将XML标记的值获取到HTML页面中

I need to display the local weather in a PowerPoint slideshow (which updates itself regularely without human interaction). I have in mind to create a local html file that loads the data from the web and displays that in lateron in the ppt.

This is how the XML looks like (I load that from http://api.openweathermap.org/data/2.5/weather?q=Basel&mode=xml&units=metric)

<?xml version="1.0" encoding="UTF-8"?>
<current>
  <city id="2661604" name="Basel">
    <coord lon="7.57" lat="47.56"/>
    <country>CH</country>
    <sun rise="2014-04-30T04:13:21" set="2014-04-30T18:40:25"/>
  </city>
  <temperature value="12" min="12" max="12" unit="celsius"/>
  <humidity value="76" unit="%"/>
  <pressure value="1012" unit="hPa"/>
  <wind>
    <speed value="4.1" name="Gentle Breeze"/>
    <direction value="280" code="W" name="West"/>
  </wind>
  <clouds value="75" name="broken clouds"/>
  <precipitation value="1" mode="rain" unit="3h"/>
  <weather number="803" value="broken clouds" icon="04d"/>
  <lastupdate value="2014-04-30T11:00:00"/>
</current>

How do I now get the values from these tags into my html file?

I need it to be something like this:

  • Temperature: 12°C (I need the 12 from <temperature value="">
  • Weather icon from URL: http://openweathermap.org/img/w/04d.png (the url is static exept the 04d comes from the XML)
  • Sun rise: 6.13 am (<sun rise=""> +2 in summertime and +1 in wintertime)
  • Sun set: 8.40 pm (same as above)
  • last update: xxx

There is also a JSON version of the API (http://api.openweathermap.org/data/2.5/weather?q=Basel&mode=json&units=metric) and I would take whichever I get to work the fastest.

Also I would run it on our local windows XP machine (so no php available) but it it would only work with php I could get that to work easily.

Thanks for any help.

  • 写回答

1条回答 默认 最新

  • dpq755012465 2014-04-30 14:11
    关注

    You can use javascript/ jQuery. Add <div id="xml-data"></div> and code bellow on your html page. Hope I help.

    <script>
    jQuery(document).ready(function(){
        jQuery.ajax({
                type: "GET", 
                url: "http://api.openweathermap.org/data/2.5/weather?q=Basel&mode=xml&units=metric", 
                dataType: "xml", 
                success: function(xml) { 
    
                jQuery(xml).find('current').each(
                        function()
                        {
                          var city_name = jQuery(this).find('city').attr('name'),
                          country = jQuery(this).find('country').text(),
                          temperature = jQuery(this).find('temperature').attr('value');
    
                         jQuery('<div class="items"></div>').html('<h2>'
                          +city_name+'</h2><p>'
                          +country+'</p><p>'
                          +temperature+'</p>').appendTo('#xml-data');
    
                        });                 
                }
        });
    });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点