-1

I have an XML file named "apps.xml" and structured like this:

<?xml version="1.0" encoding="utf-8"?>

<applications>
    <app id="b461ae4a" valid="0" company="Lemon Inc." appname="None" user="admin" description="Test" note="None" />
    <app id="1c94395b" valid="0" company="Lemon Inc." appname="None" user="admin" description="Test" note="None" />
</applications>

and I wish to be able to Add, Modify and Update (Save changes to the file) the above mentioned XML file using PHP script ($_GET), DOM and Xpath running on my website.

after Adding a new XML entry (last line), the file output would look like this:

<?xml version="1.0" encoding="utf-8"?>

<applications>
    <app id="b461ae4a" valid="0" company="Lemon Inc." appname="None" user="admin" description="Test" note="None" />
    <app id="1c94395b" valid="0" company="Lemon Inc." appname="None" user="admin" description="Test" note="None" />
    <app id="d65k274p" valid="0" company="Lemon Inc." appname="None" user="admin" description="Test" note="None" />
</applications>

and after Modification/Update (first entry modified/updated) would look like this:

<?xml version="1.0" encoding="utf-8"?>

<applications>
    <app id="b461ae4a" valid="1" company="Orange Inc." appname="None" user="tommy" description="Test" note="None" />
    <app id="1c94395b" valid="0" company="Lemon Inc." appname="None" user="admin" description="Test" note="None" />
    <app id="d65k274p" valid="0" company="Lemon Inc." appname="None" user="admin" description="Test" note="None" />
</applications>

and to add more example what I'm trying to achieve:

http://www.myadress.com/apps.php?append=1&id=d65k274p&valid=1&company=Orange%20Inc&appname=None&user=admin&description=Test&note=None

// Append argument would decide to add a whole new entry set to 1 or if it is on 0 then to be modified the whole entry by given "id".

$xappend = $_GET["append"];

$xid = $_GET["id"];
$xvalid = $_GET["valid"];
$xcompany = $_GET["company"];
$xappname = $_GET["appname"];
$xuser = $_GET["user"];
$xdescription = $_GET["description"];
$xnote = $_GET["note"];

if (file_exists('apps.xml')) {
    $xorig = simplexml_load_file('apps.xml');
} else {
    exit('Failed to open apps.xml !');
}

Any help would be highly appreciated!

beic
  • 127
  • 1
  • 2
  • 16
  • Search for simplexml or domdocument. Anyway your question is too broad. Try to do something first and then come back when you have some issue. – Felippe Duarte Apr 14 '20 at 20:15
  • @FelippeDuarte Yes, I searched for the DOMDocument, the issue that I can't do so much, because I'm not so good in PHP, but I already did it in ASP, because I using it all the time for years, but unfortunately my hosting does not support ASP only PHP. – beic Apr 14 '20 at 20:51
  • Please edit your question for an example of a change you want to make and the desired output after the change. – Jack Fleeting Apr 15 '20 at 00:47
  • @JackFleeting I will do it right now, thank you! – beic Apr 15 '20 at 09:55

2 Answers2

0

Try it this way and see if it works on your actual xml:

<?php
$original = <<<SNIPPET
<applications>
    <app id="b461ae4a" valid="0" company="Lemon Inc." appname="None" user="admin" description="Test" note="None" />
    <app id="1c94395b" valid="0" company="Lemon Inc." appname="None" user="admin" description="Test" note="None" />
</applications>
SNIPPET;

$xorig = simplexml_load_string($original);

$ins_pnt = $xorig->xpath('/applications');
$target = $xorig->xpath('/applications/app[1]');
$new_node = clone $target[0];
$new_node['id'] = "d65k274p";

// the function is lifted from https://stackoverflow.com/a/4778964/9448090
function node_append(SimpleXMLElement $to, SimpleXMLElement $from) {
    $toDom = dom_import_simplexml($to);
    $fromDom = dom_import_simplexml($from);
    $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
}

node_append($ins_pnt[0], $new_node); 

echo $xorig->asXML();

Output:

<?xml version="1.0" encoding="UTF-8"?>
<applications>
   <app id="b461ae4a" valid="0" company="Lemon Inc." appname="None" user="admin" description="Test" note="None" />
   <app id="1c94395b" valid="0" company="Lemon Inc." appname="None" user="admin" description="Test" note="None" />
   <app id="d65k274p" valid="0" company="Lemon Inc." appname="None" user="admin" description="Test" note="None" />
</applications>
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • Tried, but not getting any result except blank php screen. Added also $xorig = simplexml_load_file('apps.xml'); – beic Apr 15 '20 at 19:33
  • @beic - Try it [here](https://paiza.io/projects/e/kwW9TVWnckB0Sc2kl1mlBw). – Jack Fleeting Apr 15 '20 at 19:53
  • I extended my question above I hope you can understand my needs now better. Thank you! – beic Apr 15 '20 at 19:58
  • @beic My answer was in response to your original question; you are basically asking a new question now. You probably should post the edit as a new question. – Jack Fleeting Apr 15 '20 at 21:02
  • From the start I wanted like posted right now, but I didn't know how to express my self in a right way! Can you please maybe edit your answer? You could see in my original question this particular line what was referring to my extended question "PHP script ($_GET), DOM and Xpath running on my website". – beic Apr 15 '20 at 21:22
0

After a long hours I managed to do it like this as a beginner:

$xmlFile = 'apps.xml';

$xorig = simplexml_load_file($xmlFile) or die('0');

    $app = $xorig->addChild('app');
    $app->addAttribute('id', $xid);
    $app->addAttribute('valid', $xval);
    $app->addAttribute('company', $xcmp);
    $app->addAttribute('appname', $xanam);
    $app->addAttribute('user', $xusr);
    $app->addAttribute('description', $xdesc);
    $app->addAttribute('note', $xnote);
beic
  • 127
  • 1
  • 2
  • 16