简体   繁体   中英

Transferring DOMDocument from PHP to Javascript using function

I have a PHP function creating a DOMDocument XML file, i need to get the DOMDocument into Javascript, i have thought about using

The function in PHP returns the DOMDocument, this is the PHP function

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;
}

So what i want to do is call the function in Javascript, and returns the DOMDocument.

Try the following

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

you can simply put this function to a URL ( eg have it in a standalone file? that's up to you ), and call it from the client side via AJAX. For details on doing such a call, please reference How to make an AJAX call without jQuery? .

Edit: try to create a simple PHP file that includes and calls the function you have. From what you've described so far, it will probably look like

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

assuming this file is called xml.php , when you access it via your browser in http://mydomain.com/xml.php you should see the XML document (nothing related to Javascript so far).

Now, in your main document, you include a piece of Javascript that will call upon this URL to load the XML. An example would be (assuming you are using jQuery, for a simple Javascript function reference the above link) :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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