是否可以使用外部网站的XML?

时间:2011-12-05 05:52:10

标签: php javascript xml xslt

我已经研究了这个主题已经好几个月没有成功了。许多帖子都说这是可能的,但我还是能够让它发挥作用。

问题在于,我们正在使用以XML格式提供数据文件的资源。可以在以下网址找到它:http://www.idexonline.com/image_portal/Home/Graph/Base/IDEXOnlineDiamondIndex.xml

我想将此数据插入现有网页。

我尝试使用XSL或XML包含以及各种其他技术来访问此数据,但没有运气。

我目前在此网站上使用PHP。但是,我对方法持开放态度,它可以使用JavaScript或其他技术!帮助!

由于

4 个答案:

答案 0 :(得分:1)

如果网站没有阻止您,您可以获得如下数据。

 $xml = simplexml_load_file('http://www.idexonline.com/image_portal/Home/Graph/Base/IDEXOnlineDiamondIndex.xml');
 print_r($xml);

答案 1 :(得分:1)

这对我有用:

<?php

$file  = 'http://www.idexonline.com';
$file .= '/image_portal/Home/Graph/Base/IDEXOnlineDiamondIndex.xml';

$xml = file_get_contents($file);

$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml);
$domXPath = new DOMXPath($xmlDoc);

$xPath = '/root/index';
$index = $domXPath->evaluate("string($xPath)");

$xPath = '/root/change';
$change = $domXPath->evaluate("string($xPath)");
$change = number_format(round($change, 2), 2);

echo "The index is '$index' and the change is '$change'";

当时的输出是这样的:

  

索引为'139.04',更改为'0.00'

答案 2 :(得分:0)

我不确定这个问题的正确解决方案。但是,AFAIK从其他位置访问数据需要远程计算机上的跨域访问策略。 Wikipedia's take on this

答案 3 :(得分:0)

此XSLT转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pUrl" select=
 "'http://www.idexonline.com/image_portal/Home/Graph/Base/IDEXOnlineDiamondIndex.xml'"/>

 <xsl:template match="/">
     <textarea>
       <xsl:copy-of select="document($pUrl)"/>
     </textarea>
 </xsl:template>
</xsl:stylesheet>

应用于任何XML文档时,生成

<textarea>
   <root>
      <index>139.09</index>
      <change>-0.00036470</change>
   </root>
</textarea>
相关问题