John Davidson

php - Edit an XML within compressed tgz file without extracting it

0 comments
Message:


I have been trying to modify an XML document within an archived .tgz file by using the PharData class using php. I have no problem modifying the XML through DomDocument class, but I am having a hard time saving the whole process: I end up saving 2 different .phar documents alltogether, w/o modifying anything in the .tgz archive :


folder example image


Here is the code I have been using :


            $phar = new PharData($path . '/' . $idClient . '/' . $tempDate->format('ym') . '/' . $file);

echo $phar . "\n";

$dom = new DOMDocument();
$dom->load ('phar://' . $path . '/' . $idClient . '/' . $tempDate->format('ym') . '/' . $file . '/' . substr($file, 0, -4) . '.xml');

if ($dom === null) continue;
$ids = $dom->documentElement->getElementsByTagName('id');

foreach ($ids as $i_id) {
if ($i_id->nodeValue !== $id) continue;
echo $i_id->nodeValue . "\n";
echo $i_id->nodeName . "\n";
$sensor = $i_id->parentNode;
echo $sensor->nodeName . "\n";
foreach ($sensor->childNodes as $object) {
if ($object->nodeName !== 'type') continue;
echo $object->nodeName . "\n";
echo $object->nodeValue . "\n";
$object->nodeValue = $type;
echo $object->nodeValue . "\n" . "\n";
}
}
$dom->save('phar://' . $path . '/' . $idClient . '/' . $tempDate->format('ym') . '/' . $file . '.phar' . '/' . substr($file, 0, -4) . '.xml');


My Answer:
$phar = new PharData($path . '/' . $idClient . '/' . $tempDate . '.tgz');

// Extract the XML file from the archive
$xmlFile = $phar['example.xml']->getContent();

// Modify the XML using DomDocument
$dom = new DomDocument();
$dom->loadXML($xmlFile);
// Make your modifications here

// Save the modified XML back to the archive
$phar->addFromString('example.xml', $dom->saveXML());

// Close the PharData object
unset($phar);

This code should extract the XML file from the .tgz archive, modify it using DomDocument, and then save the modified XML back to the archive. Make sure to replace 'example.xml' with the actual name of the XML file you want to modify.

Rate this post

5 of 5 based on 9688 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us