在我的网站上以HTML格式显示从其他网站生成的XML

时间:2015-12-08 12:50:57

标签: php jquery json xml html5

我在自己的网站上http://www.profesia.sk/partner/export.php?auth=b1e4689c28cac5331a535e467bed3f3f生成的XML的实现存在严重问题。我只想将position显示为h3content作为普通链接。它甚至可能吗?

我只能这样做:

<script>
    var xml = "<export><offer><offerdate>1.12.2015</offerdate><last_updated>2015-12-01 16:11:32</last_updated><position1><![CDATA[ Controller ]]></position1><location><![CDATA[ Ľudovíta Stárka 2150/16, Trenčín ]]></location><content><![CDATA[http://www.profesia.sk/partner/export.php?offer_id=2346292&auth=b1e4689c28cac5331a535e467bed3f3f&cdata=1]]></content><url><![CDATA[http://www.profesia.sk/praca/auto-kelly-slovakia/O2346292?statistics=626098_116&utm_campaign=offer&utm_medium=affiliate&utm_source=k_autokelly_1793]]></url><company><![CDATA[ AUTO KELLY SLOVAKIA, s.r.o. ]]></company><ico><![CDATA[ 36298026 ]]></ico></offer></export>",
        xmlDoc = $.parseXML( xml ),
        $xml = $( xmlDoc ),
        $position = $xml.find( "position" ),
        $content = $xml.find( "content" );

        $( "#position" ).append( $position.text() );
        $( "#content" ).append( $content.text() );
</script>

<h3 id="position"></h3>
<a id="content"/>

所以,我的主要问题是:如何动态地阅读该XML站点?

谢谢。

2 个答案:

答案 0 :(得分:1)

你可以查看控制台,你得到了什么样的参数,所以我们看到你从xml得到一个数组,你的任务的hier代码示例: 的 HTML

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">

    <script src=" https://code.jquery.com/jquery-1.11.3.min.js"></script>

  </head>

  <body>
    <h2>Your XML Links</h2>
    <div class="wrapper"></div>

  </body>

</html>

<强> JS

var xml = '<export> <list count="2"> <offer id="2347893"> <offerdate value="2015-12-01">1.12.2015</offerdate> <last_updated>2015-12-01 16:11:32</last_updated> <position> <![CDATA[ Controller ]]> </position> <location> <![CDATA[ Ľudovta Strka 2150/16, Trenčn ]]> </location> <content> <![CDATA[ http://www.profesia.sk/partner/export.php?offer_id=2347893&auth=b1e4689c28cac5331a535e467bed3f3f&cdata=1 ]]> </content> <url> <![CDATA[ http://www.profesia.sk/praca/auto-kelly-slovakia/O2347893?statistics=626098_116&utm_campaign=offer&utm_medium=affiliate&utm_source=k_autokelly_1793 ]]> </url> <company> <![CDATA[ AUTO KELLY SLOVAKIA, s.r.o. ]]> </company> <ico> <![CDATA[ 36298026 ]]> </ico> <ref_nr/> <jobtypes> <jobtype>1</jobtype> </jobtypes> <offercategories> <offercategory id="2"> <![CDATA[ Ekonomika, financie, čtovnctvo ]]> </offercategory> </offercategories> <offerpositions> <offerposition id="161"> <![CDATA[ Controller ]]> </offerposition> <offerposition id="24"> <![CDATA[ Finančn analytik ]]> </offerposition> </offerpositions> <customfields></customfields> <offerregions> <offerregion id="409"> <![CDATA[ Trenčn ]]> </offerregion> </offerregions> </offer> <offer id="2332074"> <offerdate value="2015-11-18">18.11.2015</offerdate> <last_updated>2015-11-30 11:30:55</last_updated> <position> <![CDATA[ Obchodn zstupca - Trnava ]]> </position> <location> <![CDATA[ Trnava ]]> </location> <content> <![CDATA[ http://www.profesia.sk/partner/export.php?offer_id=2332074&auth=b1e4689c28cac5331a535e467bed3f3f&cdata=1 ]]> </content> <url> <![CDATA[ http://www.profesia.sk/praca/auto-kelly-slovakia/O2332074?statistics=626098_116&utm_campaign=offer&utm_medium=affiliate&utm_source=k_autokelly_1793 ]]> </url> <company> <![CDATA[ AUTO KELLY SLOVAKIA, s.r.o. ]]> </company> <ico> <![CDATA[ 36298026 ]]> </ico> <ref_nr/> <jobtypes> <jobtype>8</jobtype> </jobtypes> <offercategories> <offercategory id="10"> <![CDATA[ Obchod ]]> </offercategory> </offercategories> <offerpositions> <offerposition id="54"> <![CDATA[ Obchodn referent ]]> </offerposition> <offerposition id="56"> <![CDATA[ Obchodn zstupca ]]> </offerposition> <offerposition id="568"> <![CDATA[ Odborn predajca ]]> </offerposition> </offerpositions> <customfields></customfields> <offerregions> <offerregion id="507"> <![CDATA[ Trnava ]]> </offerregion> </offerregions> </offer> </list> </export>';

var xmlDoc = $.parseXML( xml ),
  $xml     = $( xmlDoc ),
  offer    = $xml.find( "offer " ),
  position = $xml.find( "position" ),
  content  = $xml.find( "content" );

var template = '';

for(var i=0, len=offer.length; i<len; i++){ 
   template += '<a id="content'+i+'" href="'+content[i].textContent+'">'
   template += '<h3 id="position'+i+'">'+position[i].textContent+'</h3>';
   template += '</a>';
}

$('.wrapper').append(template);

如果您想在代码中更改某些内容以进行尝试,则代码位于http://plnkr.co/edit/vZEQImNkbkXJFwumGg8m?p=preview

答案 1 :(得分:0)

PHP代码可以如下所示

<?php
$xml = "<export><offer><offerdate>1.12.2015</offerdate><last_updated>2015-12-01 16:11:32</last_updated><position1><![CDATA[ Controller ]]></position1><location><![CDATA[ Ludovita Starka 2150/16, Trencin ]]></location><content><![CDATA[http://www.profesia.sk/partner/export.php?offer_id=2346292&auth=b1e4689c28cac5331a535e467bed3f3f&cdata=1]]></content><url><![CDATA[http://www.profesia.sk/praca/auto-kelly-slovakia/O2346292?statistics=626098_116&utm_campaign=offer&utm_medium=affiliate&utm_source=k_autokelly_1793]]></url><company><![CDATA[ AUTO KELLY SLOVAKIA, s.r.o. ]]></company><ico><![CDATA[ 36298026 ]]></ico></offer></export>";
$input = new SimpleXMLElement($xml);
echo '<h3 id="position">'.$input->offer->company.'</h3>';
echo '<a id="content" href="'.$input->offer->content.'">'.$input->offer->position1.'</a>';
?>