繁体   English   中英

使用函数将DOMDocument从PHP传输到Javascript

[英]Transferring DOMDocument from PHP to Javascript using function

我有一个PHP函数创建DOMDocument XML文件,我需要将DOMDocument转换为Javascript,我已经考虑过使用

PHP中的函数返回DOMDocument,这是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中调用函数,并返回DOMDocument。

尝试以下

<?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>

您可以简单地将此函数放入URL(例如,将其保存在独立文件中吗?该由您决定),然后通过AJAX从客户端调用它。 有关进行此类调用的详细信息,请参考如何在不使用jQuery的情况下进行AJAX调用?

编辑:尝试创建一个简单的PHP文件,其中包含并调用您具有的功能。 根据您到目前为止的描述,它可能看起来像

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

假定此文件名为xml.php,当您通过浏览器访问http://mydomain.com/xml.php时 ,应该会看到XML文档(到目前为止,与Javascript无关)。

现在,在您的主文档中,您将包含一段Javascript,它将调用此URL来加载XML。 一个示例是(假设您使用的是jQuery,有关上述链接的简单Javascript函数请参见参考资料):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM