PHP XML DOM 添加、修改和更新

我有一个名为“apps.xml”的 XML 文件,其结构如下:


<?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>

我希望能够使用在我的网站上运行的 PHP 脚本 ($_GET)、DOM 和 Xpath 添加、修改和更新(保存对文件的更改)上述 XML 文件。


添加新的 XML 条目(最后一行)后,文件输出将如下所示:


<?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>

修改/更新后(第一个条目修改/更新)将如下所示:


<?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>

并添加更多示例我正在努力实现的目标:


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 !');

}

任何帮助将不胜感激!


冉冉说
浏览 172回答 1
1回答

慕容森

几个小时后,我作为初学者设法做到了:$xmlFile = 'apps.xml';$xorig = simplexml_load_file($xmlFile) or die('0');&nbsp; &nbsp; $app = $xorig->addChild('app');&nbsp; &nbsp; $app->addAttribute('id', $xid);&nbsp; &nbsp; $app->addAttribute('valid', $xval);&nbsp; &nbsp; $app->addAttribute('company', $xcmp);&nbsp; &nbsp; $app->addAttribute('appname', $xanam);&nbsp; &nbsp; $app->addAttribute('user', $xusr);&nbsp; &nbsp; $app->addAttribute('description', $xdesc);&nbsp; &nbsp; $app->addAttribute('note', $xnote);

慕少森

几个小时后,我作为初学者设法做到了:$xmlFile = 'apps.xml';$xorig = simplexml_load_file($xmlFile) or die('0');&nbsp; &nbsp; $app = $xorig->addChild('app');&nbsp; &nbsp; $app->addAttribute('id', $xid);&nbsp; &nbsp; $app->addAttribute('valid', $xval);&nbsp; &nbsp; $app->addAttribute('company', $xcmp);&nbsp; &nbsp; $app->addAttribute('appname', $xanam);&nbsp; &nbsp; $app->addAttribute('user', $xusr);&nbsp; &nbsp; $app->addAttribute('description', $xdesc);&nbsp; &nbsp; $app->addAttribute('note', $xnote);
打开App,查看更多内容
随时随地看视频慕课网APP