2013-02-26 155 views
0

我有一個PHP函數創建一個DOMDocument XML文件從PHP傳輸的DOMDocument爲Javascript,我需要得到DOM文檔轉換爲JavaScript,我曾想過使用使用功能

PHP中的函數返回DOM文檔,這是PHP函數

function coursexml($cc, $type){ 
    $xmlfile = new DOMDocument(); 

    if (@$xmlfile->load("books.xml") === false || $cc == "" || $type == "") { 

     header('location:/assignment/errors/500'); 
     exit; 
    } 
    $string = ""; 

    $xpath = new DOMXPath($xmlfile); 
    $nodes = $xpath->query("/bookcollection/items/item[courses/course='$cc']"); 
    $x = 0; 
    foreach($nodes as $n) { 

     $id[$x] = $n->getAttribute("id"); 

     $titles = $n->getElementsByTagName("title"); 
     $title[$x] = $titles->item(0)->nodeValue; 
     $title[$x] = str_replace(" /", "", $title[$x]); 
     $title[$x] = str_replace(".", "", $title[$x]); 

     $isbns = $n->getElementsByTagName("isbn"); 
     $isbn[$x] = $isbns->item(0)->nodeValue; 

     $bcs = $n->getElementsByTagName("borrowedcount"); 
     $borrowedcount[$x] = $bcs->item(0)->nodeValue; 

     if ($string != "") $string = $string . ", "; 

     $string = $string . $x . "=>" . $borrowedcount[$x]; 
     $x++; 
    } 

    if ($x == 0) header('location:/assignment/errors/501'); 
    $my_array = eval("return array({$string});"); 
    asort($my_array); 

    $coursexml = new DOMDocument('1.0', 'utf-8'); 
    $coursexml->formatOutput = true; 
    $node = $coursexml->createElement('result'); 
    $coursexml->appendChild($node); 
    $root = $coursexml->getElementsByTagName("result"); 
    foreach ($root as $r) { 
     $node = $coursexml->createElement('course', "$cc"); 
     $r->appendChild($node); 
     $node = $coursexml->createElement('books'); 
     $r->appendChild($node); 
     $books = $coursexml->getElementsByTagName("books"); 
     foreach ($books as $b) { 
      foreach ($my_array as $counter => $bc) { 
       $bnode = $coursexml->createElement('book'); 
       $bnode = $b->appendChild($bnode); 
       $bnode->setAttribute('id', "$id[$counter]"); 
       $bnode->setAttribute('title', "$title[$counter]"); 
       $bnode->setAttribute('isbn', "$isbn[$counter]"); 
       $bnode->setAttribute('borrowedcount', "$borrowedcount[$counter]"); 
      } 
     } 
    } 
    return $coursexml; 
} 

所以我想要做的就是調用JavaScript中的函數,並返回DOM文檔。

回答

0

你可以簡單地把這個函數放到一個URL中(例如把它放在一個獨立的文件中,由你決定),然後通過AJAX從客戶端調用它。有關進行此類呼叫的詳細信息,請參閱How to make an AJAX call without jQuery?

編輯: 嘗試創建一個簡單的PHP文件,其中包含並調用您擁有的函數。從到目前爲止你所描述的東西,它可能會像

<?php 
    include("functions.php"); 
    print coursexml($cc, $type); 

假設此文件被稱爲xml.php,當你在http://mydomain.com/xml.php通過瀏覽器訪問它,你應該看到與JavaScript中的XML文檔(沒有至今)。

現在,在您的主文檔中,您將包含一段JavaScript,它將調用此URL來加載XML。一個例子是(假設你使用jQuery,一個簡單的JavaScript函數參考上面的鏈接):

$.ajax({ 
    url: "xml.php", 
    success: function(data){ 
     // Data will contain you XML and can be used in Javascript here 
    } 
}); 
+0

我一直在尋找在 $阿賈克斯({ URL: 「的test.html」, 方面:document.body的, 成功:函數(){$ (本).addClass( 「完成」);} }); 但我不知道如何格式化,調用我想要的功能,並處理返回。 – 2013-02-26 17:49:32

+0

首先你需要把上面的函數放到一個php文件中,比如xml.php,當瀏覽文件的時候(例如http://mydomain.com/xml.php)顯示XML。這取決於你如何設置你的應用程序。之後,使用上面引用的函數來調用該URL。 – hexblot 2013-02-26 17:53:03

+0

我在設置functions.php中有很多函數,它是否需要在它自己的PHP文件中? 對於輸出將像它跳出 $ xml = coursexml($ cc,$ type); $ string = htmlspecialchars($ xml-> saveXML()); 工作? – 2013-02-26 17:57:45

1

請嘗試以下

<?php include('coursexml.php'); ?> 
<script> 
    var xml = <?php $xml = coursexml("CC140", "xmlfile"); 
        echo json_encode($xml->saveXML()); ?>; 
    document.write("output" + xml); 
    var xmlDoc = (new DOMParser()).parseFromString(xml, 'text/xml'); 
</script> 
+0

在源代碼中顯示源代碼中的輸出時,它們顯示爲錯誤,因爲屏幕上沒有任何內容出現 – 2013-02-26 19:09:53

+0

@CraigWeston錯誤是什麼? – Musa 2013-02-26 19:11:21

+0

代碼中存在拼寫錯誤現在嘗試。 – Musa 2013-02-26 19:13:49