Take the 2-minute tour ×

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 == "") {

    $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];

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

    $coursexml = new DOMDocument('1.0', 'utf-8');
    $coursexml->formatOutput = true;
    $node = $coursexml->createElement('result');
    $root = $coursexml->getElementsByTagName("result");
    foreach ($root as $r) {
        $node = $coursexml->createElement('course', "$cc");
        $node = $coursexml->createElement('books');
        $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.

share|improve this question

2 Answers 2

Try the following

<?php include('coursexml.php'); ?> 
    var xml = <?php $xml = coursexml("CC140", "xmlfile"); 
                    echo json_encode($xml->saveXML()); ?>; 
    document.write("output" + xml);
    var xmlDoc = (new DOMParser()).parseFromString(xml, 'text/xml');
share|improve this answer
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
@CraigWeston what's the error? –  Musa Feb 26 '13 at 19:11
There was a typo in the code try it now. –  Musa Feb 26 '13 at 19:13
There seems to be a problem using xml as a XML type, this is the code i have now <script> var xml = <?php $xml = coursexml("CC140", "json"); echo json_encode($xml->saveXML()); ?>; books = xml.getElementsByTagName('book') document.write("<form name='books' action='course2.php' method='get'>"); document.write("<select name='book'>"); for (var i=0;i<books.length;i++) { id = xml.getElementsByTagName('book')[i].getAttribute("id"); document.write("<option value=" + id + ">Book</option>"); } document.write("</select>"); </script> –  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

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

    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) :

   url: "xml.php",
   success: function(data){
      // Data will contain you XML and can be used in Javascript here
share|improve this answer
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 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. –  hexblot 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 <?php print coursexml($cc, $type); or any similar variant. –  hexblot 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

Your Answer


