Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php从xml中删除元素-非正统xml_Php_Iphone_Ios_Xml - Fatal编程技术网

使用php从xml中删除元素-非正统xml

使用php从xml中删除元素-非正统xml,php,iphone,ios,xml,Php,Iphone,Ios,Xml,我创建了一个解析xml文档的ios应用程序。如果用户登录,他们的信息将添加到xml文件中。我想能够删除一个用户,如果他们正在注销或取消登录。本质上,我需要弄清楚如何删除如下所示的xml对象(本例中为调酒师): <Bars> <Bar> <bar_id>0</bar_id> <Bartenders> <Bartender> <

我创建了一个解析xml文档的ios应用程序。如果用户登录,他们的信息将添加到xml文件中。我想能够删除一个用户,如果他们正在注销或取消登录。本质上,我需要弄清楚如何删除如下所示的xml对象(本例中为调酒师):

<Bars>
    <Bar>
        <bar_id>0</bar_id>
        <Bartenders>
            <Bartender>
                <imageURL>unique URL</imageURL>
                <shift>20:30</shift>
            </Bartender>
        </Bartenders>
    </Bar>
    <Bar>
        <bar_id>1</bar_id>
        <Bartenders>
               <Bartender>
                      <imageURL>aURL</imageURL>
                      <shift>a shift</shift>
                </Bartender>
                <Bartender>
                          <imageURL>aURL</imageURL>
                          <shift>a shift</shift>
                </Bartender>
        </Bartenders>
    </Bar>
有人告诉我,我可以用这种东西:

$bartenders->removeChild($bartenders[$newBar_ID]);
或者xpath,但我不确定如何仅使用唯一的imageXML就找到正确的调酒师路径。我知道我本应该更好地计划/设计这个,但我时间紧迫,必须这样做

对不起,我对php太差了。。。
谢谢你的帮助。

这里有一个非常好的脚本,效果非常好

function removeNode($xml, $path, $multi='one')
{
   $result = $xml->xpath($path);

      # for wrong $path
      if (!isset($result[0])) return false;

      switch ($multi) {
          case 'all':
                $errlevel = error_reporting(E_ALL & ~E_WARNING);
                foreach ($result as $r) unset ($r[0]);
                error_reporting($errlevel);
                return true;

            case 'child':
               unset($result[0][0]);
               return true;

            case 'one':
               if (count($result[0]->children())==0 && count($result)==1) {
                   unset($result[0][0]);
                   return true;
               }

            default:
                  return false;             
      }
}


请点击链接了解更多信息

xpath如何知道要去哪一组调酒师?
bar\u id
对于每个酒吧都是不同的,而且
imageURL
也如您所述是唯一的
function removeNode($xml, $path, $multi='one')
{
   $result = $xml->xpath($path);

      # for wrong $path
      if (!isset($result[0])) return false;

      switch ($multi) {
          case 'all':
                $errlevel = error_reporting(E_ALL & ~E_WARNING);
                foreach ($result as $r) unset ($r[0]);
                error_reporting($errlevel);
                return true;

            case 'child':
               unset($result[0][0]);
               return true;

            case 'one':
               if (count($result[0]->children())==0 && count($result)==1) {
                   unset($result[0][0]);
                   return true;
               }

            default:
                  return false;             
      }