0

XML結果から属性を取得するためにiframeにドリルダウンする、この非常に厄介なものがあります。(私は、このオフサイト サーバーからデータを取得することに関連する他の何十億もの問題を回避するために、このようにしています。)

<?php

    $link = mysqli_connect(//ALL THE CONNECTS!);
    mysqli_select_db(//THE_DB, $link);

    $query = "SELECT * FROM jos_mls AS mls
                INNER JOIN jos_activeagents AS active ON mls.MSTLISTBRD = active.AGENTUID
                LIMIT 10;";

    $result = mysqli_query($query);

    $array = array(); 
    $index = 0;

    while($row = mysqli_fetch_array($result))
    {
        $array[$index] = $row;
        $index++;
    }

    foreach ($array as $key => $value) {
        $mls = $value[1];
        $street = $value[5].' '.$value[6];
        $city = $value[9];
        $state = $value[10];
        $zip = $value[11];
        $url = "http://eligibility.sc.egov.usda.gov/eligibility/eligibilityservice?eligibilityType=Property&requestString=<?xml version='1.0'?><Eligibility xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='/var/lib/tomcat5/webapps/eligibility/Eligibilitywsdl.xsd'><PropertyRequest StreetAddress1='".$street."' StreetAddress2='' StreetAddress3='' City='".$city."' State='".$state."' County='' Zip='".$zip."' Program='RBS'></PropertyRequest></Eligibility>";
        $frame = '<iframe class="frame" mls="'.$mls.'" style="width: 10px; height: 10px;" src="'.$url.'"></iframe>';
        echo $frame;
    }

    mysql_close($link);
?>
<div id="test"></div>
<script type="text/javascript">
    $(document).ready(function(){
        $('.frame').each(function(){
            var mls = $(this).attr('mls'),
                usda = $(this).contents().find('Property').attr('Eligibility');
            $('#test').append(mls+' '+usda+'<br/>');
        });
    });
</script>

iframe 内のデータは次のようになります...

<?xml version="1.0" encoding="UTF-8"?>
<Eligibility xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Adjusted AnnualIncome="" TotalDeduction="" AdjustedIncome="" ElderlyDeduction="" YoungDeduction="">
        </Adjusted>
        <Section502Guaranted MaximumAdjusted="" Eligible="">
        </Section502Guaranted>
        <Section502Direct MaximumAdjusted="" Eligible="">
        </Section502Direct>
        <Property Eligibility="InEligible" MapURL="http://rdgdwe.sc.egov.usda.gov/eligibilitymaps/index.jsp?app=RBSIELG&amp;ADDRESS=7865 ILLINOIS CASEYVILLE&amp;STATE=IL&amp;ZIP=62232" />
        <ErrorResponse EngineId="" HostName="" MaxSeverity="" LogFile="" Class="" Module="" Severity="" Time="">
                <Message Code="" Type="" Text="" />
        </ErrorResponse>
</Eligibility>

ノード Eligibilityからの属性が必要です...更新申し訳ありませんが、時期尚早に送信を押してください。私が今得た結果は、単に「未定義」です。Property$(this).contents().find('Property').attr('Eligibility')

4

1 に答える 1

1

JSを使用して正しい方法でiframeにアクセスし、XMLDOMを使用する必要があります。

var f = $('.frame')[0];
var Property = f.contentDocument.getElementsByTagName('Property')[0];
var Eligibility = Property.getAttribute('Eligibility');

またはそのようなもの。注意:これは、ドメイン、ポート、およびプロトコルが一致する場合にのみ可能です。(所有者ドキュメントとiframeドキュメントの。)

編集ああ、私は今、iframeのURLがあなたのウェブサイトのものとおそらく異なるだろうと思います。これよりはうまくいきません。Javascript(ブラウザ)はクロスサイトスクリプティングを許可しません。非常に優れたセキュリティ上の理由から。

PHPでXMLを読む必要があります。はるかに簡単です:

$xml = simplexml_load_file($url); // requires allow_url_fopen to be on
$Eligibility = (string)$xml->Property['Eligibility'];

(それをテストしませんでした。)

于 2013-03-20T19:46:25.243 に答える