1 (edited by nickC 2018-04-29 11:33:53)

Topic: Juicebox won't read config.xml generated by PHP simpleXML [SOLVED]

Hi, I'm using the PHP simpleXML facility (http://php.net/manual/en/book.simplexml.php) to generate the config.xml dynamically from content I'm retrieving from a DB.

Everything works fine except for the fact that the XML that's generated by simpleXML self-closes the first juiceboxgallery node like this

<juiceboxgallery/> 

If I remove the trailing slash like so

<juiceboxgallery> 

then Juicebox is happy (although the XML becomes invalid then as there's no closing node).

I've tried removing the trailing '/' from the $xmlRoot variable in PHP using regex and trim but simpleXML just errors. So, my question is, is there any facility / configuration in Juicebox itself to handle the self-closing XML parent node?  If not, it looks like I'll have to do something nasty like do a search/replace on the generated config.xml file.

Thanks for any help.

Nick       

Here's an example output of my PHP:

<?xml version="1.0" encoding="UTF-8"?>
<juiceboxgallery/>
<image imageURL="../images/portraits/monkey2-monkey2.jpg" thumbURL="../images/portraits/thumbs/monkey2-monkey2.jpg" linkURL="#" linkTarget="_blank">
  <title> </title>
  <caption>sdfasd- ljsadf  </caption>
  <about>32423fwefas</about>
  <text>asfd3423 - lkj aslkj  </text>
  <copyrightYear>2234</copyrightYear>
  <locationCreated>asdf</locationCreated>
  <sourceOrganisation>asf</sourceOrganisation>
</image>

And, here's the code to generate it (NB, you'll see there're a few other nodes - not used by Juicebox (I'm using them to generate jsonLD within the page so pls ignore those - they're not the cause of the problem):

<?php
session_start();

$domtree = new DOMDocument('1.0', 'UTF-8');
// create a DOM document with UTF-8 encoding  

require_once "connect.php";

$sql_All = "SELECT Filenames.filename, Galleries.gallery, Captions.caption, TextContents.textContent, AboutContents.aboutContent, CopyrightDates.copyrightDate, Locations.location, SourceOrganisations.sourceOrganisation, SortOrder.sortOrderID FROM ((((((((
Filenames
INNER JOIN Galleries ON Filenames.filenameID_pk = Galleries.filenameID_fk)
INNER JOIN Captions ON Filenames.filenameID_pk = Captions.filenameID_fk)
INNER JOIN TextContents ON Filenames.filenameID_pk = TextContents.filenameID_fk)
INNER JOIN AboutContents ON Filenames.filenameID_pk = AboutContents.filenameID_fk)
INNER JOIN CopyrightDates ON Filenames.filenameID_pk = CopyrightDates.filenameID_fk)
INNER JOIN Locations ON Filenames.filenameID_pk = Locations.filenameID_fk)
INNER JOIN SourceOrganisations ON Filenames.filenameID_pk = SourceOrganisations.filenameID_fk)
INNER JOIN SortOrder ON Filenames.filenameID_pk = SortOrder.sortOrderID) ORDER BY SortOrder.sortOrder_pk";

// get gallery folder name from the session and create path for main folder
$pathMainImage = "../images/" . $_SESSION['nameOfFolder'] . "/";

//echo "this is the path: " . $pathMainImage;  

// get gallery folder name from the session and create path for the thumbnail folder
$pathThumbImage = "../images/" . $_SESSION['nameOfFolder'] . "/thumbs/";

//echo "this is the path: " . $pathThumbImage;  

// create the root element of the xml tree 
    $xmlRoot = $domtree->createElement("juiceboxgallery");

// append it to the document created 
    $xmlRoot = $domtree->appendChild($xmlRoot);

   foreach ($result=$conn->query($sql_All) as $row)
{

   // build image node with the various attributes needed for juicebox
    $image = $domtree->createElement("image");
    $image = $domtree->appendChild($image);
        $imageURL = $domtree->createAttribute("imageURL");
        $imageURL->value = $pathMainImage.$row['filename'];
        $image->appendChild($imageURL);
            $thumbURL = $domtree->createAttribute("thumbURL");
            $thumbURL->value = $pathThumbImage.$row['filename'];
            $image->appendChild($thumbURL);  
                $linkURL = $domtree->createAttribute("linkURL");
                $linkURL->value = "#";
                $image->appendChild($linkURL); 
                    $linkTarget = $domtree->createAttribute("linkTarget");
                    $linkTarget->value = "_blank";
                    $image->appendChild($linkTarget); 

    $image->appendChild($domtree->createElement('title',' '));
    $image->appendChild($domtree->createElement('caption', $row['caption']));
    $image->appendChild($domtree->createElement('about', $row['aboutContent']));
    $image->appendChild($domtree->createElement('text', $row['textContent']));
    $image->appendChild($domtree->createElement('copyrightYear', $row['copyrightDate']));
    $image->appendChild($domtree->createElement('locationCreated', $row['location']));    
    $image->appendChild($domtree->createElement('sourceOrganisation', $row['sourceOrganisation']));    
}

// retrieve the name of the gallery folder from the session to configure where the config.xml file gets saved and print / save the XML file

    $domtree->formatOutput = TRUE;
    print $domtree->saveXML();
    $domtree->save("../dropzone/" . $_SESSION['nameOfFolder'] .'/config.xml');

?>

Re: Juicebox won't read config.xml generated by PHP simpleXML [SOLVED]

I think all you might need to do is add the following line of code at the very end of (but still inside) your foreach loop:

$xmlRoot->appendChild($image);

This should create a valid XML file with correct opening and closing <juicbeboxgallery> tags and no need to perform any string manipulation on the resulting file.

Re: Juicebox won't read config.xml generated by PHP simpleXML [SOLVED]

Ah!!! Fantastic!!

Thanks Steven that sorted it!

I was going nuts looking at workarounds.

Thanks for your help and super rapid response (on a Sunday... !)

Nick

Re: Juicebox won't read config.xml generated by PHP simpleXML [SOLVED]

You're welcome!
I'm glad it worked. Thank you for letting me know.