Problems with PHP/XML/XSL - php

I am trying to figure out what I can do to create a code that appends data in my XML file not rewrite the XML file continuously.
I need to be able to save all the form entries and as of right now every time the form is submitted. it creates a new XML file and erases the old one.
This may be really easy to fix or I am just really dumb but I have looked at DOM syntax and do not see what I could change to change the outcome.
// define configuration file name and path
$configFile = 'abook.xml';
// if form not yet submitted
// display form
if (!isset($_POST['submit'])) {
// set up array with default parameters
$data = array();
$data['name'] = null;
$data['email'] = null;
$data['caddress'] = null;
$data['city'] = null;
$data['state'] = null;
$data['zipcode'] = null;
$data['phone'] = null;
$data['pug'] = null;
$data['comment'] = null;
$data['subscribe'] = null;
// read current configuration values
// use them to pre-fill the form
if (file_exists($configFile)) {
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->load($configFile);
$address = $doc->getElementsByTagName('address');
foreach ($address->item(0)->childNodes as $node) {
$data[$node->nodeName] = $node->nodeValue;
}
}
In between is a PHP form and validation code and at the end I use XML tags again:
// generate new XML document
$doc = new DOMDocument();
// create and attach root element <configuration>
$root = $doc->createElement('addressbook');
$configuration = $doc->appendChild($root);
// create and attach <oven> element under <configuration>
$address = $doc->createElement('address');
$configuration->appendChild($address);
// write each configuration value to the file
foreach ($config as $key => $value) {
if (trim($value) != '') {
$elem = $doc->createElement($key);
$text = $doc->createTextNode($value);
$address->appendChild($elem);
$elem->appendChild($text);
}
}
// format XML output
// save XML file
$doc->formatOutput = true;
$doc->save($configFile) or die('ERROR: Cannot write configuration file');
echo 'Thank you for filling out an application.';
}
I am really new at this so I am sorry if my code is pretty messy.
The second part I am dealing with an XSL file which I have linked to my XML file but no matter what syntax I have used to transform, nothing works to save it in a table.
Again, I don't know if this could be caused by the way I have set up my PHP to write the XML.

Related

PHP 8.1.2 - Unusual behavior with DOMDocument when saving and formatting XML data

I am trying to output some configuration data to an XML file using DOMDocument and the code below using loadXML(), saveXML() and save(), however the XML file always has a structure like below (notice Configuration/, but not Configuration /Configuration), and I cannot get XML indentation to work using preserveWhiteSpace = false and formatOutput = true. Where is "Configuration/" coming from, and why no closing clause after Authentication_Key_2?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration/>
<FullProductName>Text</FullProductName>
<Copyright>Text</Copyright>
<Version>Text</Version>
<Website>Text</Website>
<Database_Password>NDE2NDYxNmQ0ODZmNmU2NTZiMzEzOTM4MzE=</Database_Password>
<Authentication_Key_1>MDY0ZmRhMDYwYzkxMjVkNzk1M2U4YzUx</Authentication_Key_1>
<Authentication_Key_2>ZjllYWFjYmQ5NTEyYmNiNWE4MmYwZj==</Authentication_Key_2>
The code I'm using follows below:
//Create the config data structure.
$new_config_data = new DOMDocument("1.0", "UTF-8");
$config_xml_element_name[0] = "Configuration";
$config_xml_element_name[1] = "FullProductName";
$config_xml_element_name[2] = "Copyright";
$config_xml_element_name[3] = "Version";
$config_xml_element_name[4] = "Website";
$config_xml_element_name[5] = "Database_Password";
$config_xml_element_name[6] = "Authentication_Key_1";
$config_xml_element_name[7] = "Authentication_Key_2";
$config_xml_element_instance[0] = $new_config_data->createElement($config_xml_element_name[0]);
$config_xml_element_instance[1] = $new_config_data->createElement($config_xml_element_name[1], $FullProductName);
$config_xml_element_instance[2] = $new_config_data->createElement($config_xml_element_name[2], $ThisProductSoftwareHouse);
$config_xml_element_instance[3] = $new_config_data->createElement($config_xml_element_name[3], $ThisProductVersion);
$config_xml_element_instance[4] = $new_config_data->createElement($config_xml_element_name[4], $ThisProductWebsite);
$config_xml_element_instance[5] = $new_config_data->createElement($config_xml_element_name[5], $_POST['postgresql-password']);
$config_xml_element_instance[6] = $new_config_data->createElement($config_xml_element_name[6], $_POST['secret-key1']);
$config_xml_element_instance[7] = $new_config_data->createElement($config_xml_element_name[7], $_POST['secret-key2']);
//Go through array of config file elements and append them to data structure
for ($config_xml_element_id = 0; $config_xml_element_id < count($config_xml_element_instance); $config_xml_element_id++) {
//If this is the Root element then append it to config data structure as Root
if ($config_xml_element_id == 0) {
$new_config_data->appendChild($config_xml_element_instance[0]);
//Otherwise append it to config data structure as Child element of Root element
} else {
$config_xml_element_instance[0] = $new_config_data->appendChild($config_xml_element_instance[$config_xml_element_id]);
}
}
$Config_create_file_path = $_SERVER['DOCUMENT_ROOT'] .$ParentProductRootPath .$ParentProductSubPath .$ThisProductTopPath ."/Application/Config/";
$Config_create_file = ucfirst($ThisProductFilePrependName) ."_config.xml";
$Config_create_file_string = $Config_create_file_path .$Config_create_file;
//If the Config directory does not exist then create it
if (!file_exists($Config_create_file_path)) {
mkdir($Config_create_file_path, 0777, true);
}
//Save compiled config data structure to XML file
$ConfigDataXml = $new_config_data->saveXML();
$new_config_data->preserveWhiteSpace = false;
$new_config_data->formatOutput = true;
$new_config_data->loadXML($ConfigDataXml);
$new_config_data->saveXML();
$config_write = $new_config_data->save($Config_create_file_string);
You are adding all of the nodes into the root of the document, so there is no hierarchy and there is no content for the Configuration node (as it's empty it shows `'.
I think the line which is the problem is where you add from element id after 0...
$config_xml_element_instance[0] = $new_config_data->appendChild($config_xml_element_instance[$config_xml_element_id]);
Is adding the new element onto the root of the new document. To add it to the configuration element, add it to $config_xml_element_instance[0]...
$config_xml_element_instance[0]->appendChild($config_xml_element_instance[$config_xml_element_id]);

PHP: Updating XML from PHP and Saving

I'm trying to update 1 particular node in an pre-existing xml file from php. The problem i'm having is that it doesn't seem to be saving to the xml file. Not sure why and any help here will be appreciated!
<?php
$itemNumber = $_GET["itemNumberField"];
$xmlFile = "items.xml";
if(file_exists($xmlFile))
{
// $doc = new DOMDocument('1.0');
// $doc->load($xmlFile);
$doc = DOMDocument::load($xmlFile);
$item = $doc->getElementsByTagName("item");
foreach($item as $node)
{
$itemNumberNode = $node->getElementsByTagName("itemNumber");
$itemNumberNode = $itemNumberNode->item(0)->nodeValue;
$qtyNode = $node->getElementsByTagName("quantity");
$qtyNode = $qtyNode->item(0)->nodeValue;
if ($itemNumberNode == $itemNumber)
{
$qtyNode++;
echo $qtyNode;
}
}
}
else
{
echo "file doesn't exist! <br/>";
}
$doc->save($xmlFile);
?>
Edit: Just to clarify, the adding to the node seems to be fine.
Solution: Turns out the reason it didn't save was because node of the adding. i had to either directly update it or assign an updated value to it.
$qtyNode = $node->getElementsByTagName("quantity");
$qtyNode = $qtyNode->item(0);
...
$qtyNode->nodeValue++;
I've just coded something similar, and found out that $doc->saveXML() just builds up the outgoing xml string. I added a file_put_contents for writing to the xml file and worked fine.
file_put_contents($xmlFile, $doc->saveXML());
You COPY the scalar value (string) into a variable. Then you change the variable.
$qtyNode = $qtyNode->item(0)->nodeValue;
if ($itemNumberNode == $itemNumber)
{
$qtyNode++;
echo $qtyNode;
}
$qtyNode is not a DOMNode object, but a string variable.
You will have to change the DOMNode::$nodeValue property directly or assign the variable to it.
$qtyNode = $qtyNode->item(0);
if ($itemNumberNode == $itemNumber)
{
$qtyNode->nodeValue++;
echo $qtyNode->nodeValue;
}
Chmod the folder in which you want to save xml file: set permissions to maxiamally 775.
If this not works try using Curl

how do i edit compressed gz xml file, compress and save it back, with php? [duplicate]

So, I have this code that searches for a particular node in my XML file, unsets an existing node and inserts a brand new child node with the correct data. Is there a way of getting this new data to save within the actual XML file with simpleXML? If not, is there another efficient method for doing this?
public function hint_insert() {
foreach($this->hints as $key => $value) {
$filename = $this->get_qid_filename($key);
echo "$key - $filename - $value[0]<br>";
//insert hint within right node using simplexml
$xml = simplexml_load_file($filename);
foreach ($xml->PrintQuestion as $PrintQuestion) {
unset($xml->PrintQuestion->content->multichoice->feedback->hint->Passage);
$xml->PrintQuestion->content->multichoice->feedback->hint->addChild('Passage', $value[0]);
echo("<pre>" . print_r($PrintQuestion) . "</pre>");
return;
}
}
}
Not sure I understand the issue. The asXML() method accepts an optional filename as param that will save the current structure as XML to a file. So once you have updated your XML with the hints, just save it back to file.
// Load XML with SimpleXml from string
$root = simplexml_load_string('<root><a>foo</a></root>');
// Modify a node
$root->a = 'bar';
// Saving the whole modified XML to a new filename
$root->asXml('updated.xml');
// Save only the modified node
$root->a->asXml('only-a.xml');
If you want to save the same, you can use dom_import_simplexml to convert to a DomElement and save:
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($simpleXml->asXML());
echo $dom->saveXML();

Drupal 7 - create node programatically, adding a youtube embed to a field

I'm trying to create nodes programatically. Using Media module with the youtube extension, I'd like to populate a field with youtube data. From what I've read so far, it's going to look something like this:
<?php
// $value in this case is the youtube ID.
$file = new stdClass();
$file->uid = 1;
$file->filename = $value;
$file->uri = 'youtube://v/' . $value;
$file->filemime = 'video/youtube';
$file->type = 'video';
$file->status = 1;
$youtube = file_save($file);
node->field_youtube[$node->language]['0']['fid'] = (array) $youtube->fid;
?>
I learned this by looking at the information in the $content variable in the bartik theme. However, this results in a "Bad file extension" error. I also tried putting the whole url in $file->uri and using file_get_mimetype on it, then it didn't throw an error but the video didn't work either. Does anyone know how to do this?
I found the answer. The function file_save only checks if a file id is already in the database. However, the youtube uri field did not allow duplicates. Therefore I stole this function from the file_example module. It checks if a file exists with that uri, if it does it loads the object.
function file_example_get_managed_file($uri) {
$fid = db_query('SELECT fid FROM {file_managed} WHERE uri = :uri', array(':uri' => $uri))->fetchField();
if (!empty($fid)) {
$file_object = file_load($fid);
return $file_object;
}
return FALSE;
}
So in the end I simply put an if statement, like this:
$file_exists = wthm_get_managed_file('youtube://v/' . $value);
if (!$file_exists) {
$file_path = drupal_realpath('youtube://v/' . $value);
$file = new stdClass();
$file->uid = 1;
$file->filename = $value;
$file->uri = 'youtube://v/' . $value;
$file->filemime = file_get_mimetype($file_path);
$file->type = 'video';
$file->status = 1;
$file_exists = file_save($file);
}
$node->field_youtube[$node->language]['0'] = (array) $file_exists;
This solved most problems. I still get a message saying bad file extension, but it works anyway.
I got it working like this. I'm importing embed codes that need to be parsed and some are dupes, and i think this function file_uri_to_object($code, $use_existing = TRUE) allows you to reuse managed urls.
$r->video is an iframe embed code for youtube which gets parsed to the correct uri format
// include the media youtube handler.inc file to use the embed code parsing
$path = drupal_get_path('module','media_youtube').'/includes/MediaInternetYouTubeHandler.inc';
require_once($path);
$code = MediaInternetYouTubeHandler::parse($r->video);
$youtube = file_uri_to_object($code, $use_existing = TRUE);
$youtube->display = 1;
$youtube = file_save($youtube);
$node->field_video[$lang][0] = (array)$youtube;
node_save($node);
A nicer way:
module_load_include('inc', 'media_youtube', 'includes/MediaInternetYouTubeHandler.inc');
$obj = new MediaInternetYouTubeHandler($url);
$file = $obj->getFileObject();
$file->display = 1;
file_save($file);
$product->field_product_video[LANGUAGE_NONE][] = (array) $file;

PHP - Loop through files on server and run script for each file

I will try to explain as well as possible what I'm trying to do.
I have a folder on a server with about 100 xml files. These xml files are content pages with text and references to attachment filenames on the server that will be pushed to a wiki through an API.
It's all working fine 1 XML file at a time but I want to loop through each one and run my publish script on them.
I tried with opendir and readdir and although it doesn't error it only picks up the one file anyway.
Could someone give me an idea what I have to do. I'm very new to PHP, this is my first PHP project so my code is probably not very pretty!
Here's my code so far.
The functions that gets the XML content from the XML file:
<?php
function gettitle($file)
{
$xml = simplexml_load_file($file);
$xmltitle = $xml->xpath('//var[#name="HEADLINE"]/string');
return $xmltitle[0];
}
function getsummary($file)
{
$xml = simplexml_load_file($file);
$xmlsummary = $xml->xpath('//var[#name="summary"]/string');
return $xmlsummary[0];
}
function getsummarymore($file)
{
$xml = simplexml_load_file($file);
$xmlsummarymore = $xml->xpath('//var[#name="newslinetext"]/string');
return $xmlsummarymore[0];
}
function getattachments($file)
{
$xml = simplexml_load_file($file);
$xmlattachments = $xml->xpath('//var[#name="attachment"]/string');
return $xmlattachments[0];
}
?>
Here's the main publish script which pushes the content to the wiki:
<?php
// include required classes for the MindTouch API
include('../../deki/core/dream_plug.php');
include('../../deki/core/deki_result.php');
include('../../deki/core/deki_plug.php');
//Include the XML Variables
include('loadxmlfunctions.php');
//Path to the XML files on the server
$path = "/var/www/dekiwiki/skins/importscript/xmlfiles";
// Open the XML file folder
$dir_handle = #opendir($path) or die("Unable to open $path");
// Loop through the files
while ($xmlfile = readdir($dir_handle)) {
if($xmlfile == "." || $xmlfile == ".." || $xmlfile == "index.php" )
continue;
//Get XML content from the functions and put in the initial variables
$xmltitle = gettitle($xmlfile);
$xmlsummary = getsummary($xmlfile);
$xmlsummarymore = getsummarymore($xmlfile);
$xmlattachments = getattachments($xmlfile);
//Build the variables for the API from the XML content
//Create the page title - replace spaces with underscores
$pagetitle = str_replace(" ","_",$xmltitle);
//Create the page path variable
$pagepath = '%252f' . str_replace("'","%27",$pagetitle);
//Strip HTML from the $xmlsummary and xmlsummarymore
$summarystripped = strip_tags($xmlsummary . $xmlsummarymore, '<p><a>');
$pagecontent = $summarystripped;
//Split the attachments into an array
$attachments = explode("|", $xmlattachments);
//Create the variable with the filenames
$pagefilenames = '=' . $attachments;
$pagefilenamefull = $xmlattachments;
//Create the variable with the file URL - Replace the URL below to the correct one
$pagefileurl = 'http://domain/skins/importscript/xmlfiles/';
//authentication
$username = 'admin';
$password = 'password';
// connect via proxy
$Plug = new DreamPlug('http://domain/#api');
// setup the deki api location
$Plug = $Plug->At('deki');
//authenticate with the following details
$authResult = $Plug->At('users', 'authenticate')->WithCredentials($username, $password)->Get();
$authToken = $authResult['body'];
$Plug = $Plug->With('authtoken', $authToken);
// Upload the page content - http://developer.mindtouch.com/Deki/API_Reference/POST:pages//%7Bpageid%7D//contents
$Plug_page = $Plug->At('pages', '=Development%252f' . $pagetitle, 'contents')->SetHeader('Expect','')->Post($pagecontent);
// Upload the attachments - http://developer.mindtouch.com/MindTouch_Deki/API_Reference/PUT:pages//%7Bpageid%7D//files//%7Bfilename%7D
for($i = 0; $i < count($attachments); $i++){
$Plug_attachment = $Plug->At('pages', '=Development' . $pagepath, 'files', '=' . $attachments[$i])->SetHeader('Expect','')->Put($pagefileurl . $attachments[$i]);
}
}
//Close the XMl file folder
closedir($dir_handle);
?>
Thanks for any help!
To traverse a directory of XML files you can just do:
$files = glob("$path/*.xml");
foreach($files as $file)
{
$xml = simplexml_load_file($file);
$xmltitle = gettitle($xml);
$xmlsummary = getsummary($xml);
$xmlsummarymore = getsummarymore($xml);
$xmlattachments = getattachments($xml);
}
I also recommend you make a minor adjustment to your code so simplexml doesn't need to parse the same file four times to get the properties you need:
function gettitle($xml)
{
$xmltitle = $xml->xpath('//var[#name="HEADLINE"]/string');
return $xmltitle[0];
}
function getsummary($xml)
{
$xmlsummary = $xml->xpath('//var[#name="summary"]/string');
return $xmlsummary[0];
}
function getsummarymore($xml)
{
$xmlsummarymore = $xml->xpath('//var[#name="newslinetext"]/string');
return $xmlsummarymore[0];
}
function getattachments($xml)
{
$xmlattachments = $xml->xpath('//var[#name="attachment"]/string');
return $xmlattachments[0];
}
Try changing your while loop to and see if that helps out better:
while (false !== ($xmlfile = readdir($dir_handle)))
Let me know.
EDIT:
By using the old way, there could have been a directory name that could have evaluated to false and stopped the loop, the way I suggested is considered the right way to loop over a directory while using readdir taken from here

Categories