0

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.

Craig Weston
  • 141
  • 2
  • 4
  • 10

2 Answers2

1

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>
Musa
  • 96,336
  • 17
  • 118
  • 137
  • While in the source code shows the output in the source, their appears to be a error as nothing appears on the screen – Craig Weston Feb 26 '13 at 19:09
  • There seems to be a problem using xml as a XML type, this is the code i have now – Craig Weston Feb 26 '13 at 19:40
  • @CraigWeston xml is currently a string if you want it as an XML document you'll have to parse it, see update. – Musa Feb 26 '13 at 19:48
  • So far all working brilliantly, though a weird issue for (var i=0;i – Craig Weston Feb 26 '13 at 20:28
  • @CraigWeston why don't you create the form on the server side? – Musa Feb 26 '13 at 20:33
  • trying to build it up in javascript, the for loop is one i have used before with a XML doc (though from a XML doc that already exists, though i'm baffled about what its not entering the loop – Craig Weston Feb 26 '13 at 20:44
  • @CraigWeston you might want to ask that in another question, code in the comments just look like gibberish. – Musa Feb 26 '13 at 20:46
0

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
   }
 });
Community
  • 1
  • 1
Nick Andriopoulos
  • 10,313
  • 6
  • 32
  • 56
  • I had been looking at $.ajax({ url: "test.html", context: document.body, success: function(){ $(this).addClass("done"); } }); But i'm not sure how to format it, to call the function i want, and handle the return. – Craig Weston Feb 26 '13 at 17:49
  • first you need to put the above function into a php file, such as xml.php in a way that when the file is browsed ( eg http://mydomain.com/xml.php ) the XML is displayed. that depends on how you have setup your application. after that, use the above quoted function to call that URL. – Nick Andriopoulos Feb 26 '13 at 17:53
  • I have numerous functions in a set functions.php, does it need to be in its own PHP file? For the output would outting it like $xml = coursexml($cc, $type); $string = htmlspecialchars($xml->saveXML()); work? – Craig Weston Feb 26 '13 at 17:57
  • you can just have xml.php include the function.php file, and just do a function call to it, such as ` – Nick Andriopoulos Feb 26 '13 at 17:59
  • you've lost me there, how does that fit in with the Javascript? and how do i pass variables through to the function when being called by Javscript – Craig Weston Feb 26 '13 at 18:07
  • I've created this Which does return the XML, however now the Document.write doesn't work – Craig Weston Feb 26 '13 at 18:16
  • I'm afraid I didn't explain it well enough. I'll edit the response above, for formatting reasons. – Nick Andriopoulos Feb 26 '13 at 19:35