douhuireng4407
2008-11-04 16:24 阅读 63

在SimpleXML for PHP中删除具有特定属性的子项

I have several identical elements with different attributes that I'm accessing with SimpleXML:

<data>
    <seg id="A1"/>
    <seg id="A5"/>
    <seg id="A12"/>
    <seg id="A29"/>
    <seg id="A30"/>
</data>

I need to remove a specific seg element, with an id of "A12", how can I do this? I've tried looping through the seg elements and unsetting the specific one, but this doesn't work, the elements remain.

foreach($doc->seg as $seg)
{
    if($seg['id'] == 'A12')
    {
        unset($seg);
    }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

17条回答 默认 最新

  • 已采纳
    dongtangu8615 dongtangu8615 2008-11-04 17:14

    While SimpleXML provides a way to remove XML nodes, its modification capabilities are somewhat limited. One other solution is to resort to using the DOM extension. dom_import_simplexml() will help you with converting your SimpleXMLElement into a DOMElement.

    Just some example code (tested with PHP 5.2.5):

    $data='<data>
        <seg id="A1"/>
        <seg id="A5"/>
        <seg id="A12"/>
        <seg id="A29"/>
        <seg id="A30"/>
    </data>';
    $doc=new SimpleXMLElement($data);
    foreach($doc->seg as $seg)
    {
        if($seg['id'] == 'A12') {
            $dom=dom_import_simplexml($seg);
            $dom->parentNode->removeChild($dom);
        }
    }
    echo $doc->asXml();
    

    outputs

    <?xml version="1.0"?>
    <data><seg id="A1"/><seg id="A5"/><seg id="A29"/><seg id="A30"/></data>
    

    By the way: selecting specific nodes is much more simple when you use XPath (SimpleXMLElement->xpath):

    $segs=$doc->xpath('//seq[@id="A12"]');
    if (count($segs)>=1) {
        $seg=$segs[0];
    }
    // same deletion procedure as above
    
    点赞 评论 复制链接分享
  • douzuo0002 douzuo0002 2008-12-06 03:58

    There is a way to remove a child element via SimpleXml. The code looks for a element, and does nothing. Otherwise it adds the element to a string. It then writes out the string to a file. Also note that the code saves a backup before overwriting the original file.

    $username = $_GET['delete_account'];
    echo "DELETING: ".$username;
    $xml = simplexml_load_file("users.xml");
    
    $str = "<?xml version=\"1.0\"?>
    <users>";
    foreach($xml->children() as $child){
      if($child->getName() == "user") {
          if($username == $child['name']) {
            continue;
        } else {
            $str = $str.$child->asXML();
        }
      }
    }
    $str = $str."
    </users>";
    echo $str;
    
    $xml->asXML("users_backup.xml");
    $myFile = "users.xml";
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, $str);
    fclose($fh);
    
    点赞 评论 复制链接分享
  • dt3696912 dt3696912 2008-12-27 06:04

    Just unset the node:

    $str = <<<STR
    <a>
      <b>
        <c>
        </c>
      </b>
    </a>
    STR;
    
    $xml = simplexml_load_string($str);
    unset($xml –> a –> b –> c); // this would remove node c
    echo $xml –> asXML(); // xml document string without node c
    

    This code was taken from How to delete / remove nodes in SimpleXML.

    点赞 评论 复制链接分享
  • dongshang5862 dongshang5862 2009-08-21 13:50

    Idea about helper functions is from one of the comments for DOM on php.net and idea about using unset is from kavoir.com. For me this solution finally worked:

    function Myunset($node)
    {
     unsetChildren($node);
     $parent = $node->parentNode;
     unset($node);
    }
    
    function unsetChildren($node)
    {
     while (isset($node->firstChild))
     {
     unsetChildren($node->firstChild);
     unset($node->firstChild);
     }
    }
    

    using it: $xml is SimpleXmlElement

    Myunset($xml->channel->item[$i]);
    

    The result is stored in $xml, so don’t worry about assigning it to any variable.

    点赞 评论 复制链接分享
  • dongshi1148 dongshi1148 2009-10-11 13:45

    Even though SimpleXML doesn't have a detailed way to remove elements, you can remove elements from SimpleXML by using PHP's unset(). The key to doing this is managing to target the desired element. At least one way to do the targeting is using the order of the elements. First find out the order number of the element you want to remove (for example with a loop), then remove the element:

    $target = false;
    $i = 0;
    foreach ($xml->seg as $s) {
      if ($s['id']=='A12') { $target = $i; break; }
      $i++;
    }
    if ($target !== false) {
      unset($xml->seg[$target]);
    }
    

    You can even remove multiple elements with this, by storing the order number of target items in an array. Just remember to do the removal in a reverse order (array_reverse($targets)), because removing an item naturally reduces the order number of the items that come after it.

    Admittedly, it's a bit of a hackaround, but it seems to work fine.

    点赞 评论 复制链接分享
  • dpd20130 dpd20130 2009-11-15 15:44

    For future reference, deleting nodes with SimpleXML can be a pain sometimes, especially if you don't know the exact structure of the document. That's why I have written SimpleDOM, a class that extends SimpleXMLElement to add a few convenience methods.

    For instance, deleteNodes() will delete all nodes matching a XPath expression. And if you want to delete all nodes with the attribute "id" equal to "A5", all you have to do is:

    // don't forget to include SimpleDOM.php
    include 'SimpleDOM.php';
    
    // use simpledom_load_string() instead of simplexml_load_string()
    $data = simpledom_load_string(
        '<data>
            <seg id="A1"/>
            <seg id="A5"/>
            <seg id="A12"/>
            <seg id="A29"/>
            <seg id="A30"/>
        </data>'
    );
    
    // and there the magic happens
    $data->deleteNodes('//seg[@id="A5"]');
    
    点赞 评论 复制链接分享
  • douzi8112 douzi8112 2009-11-25 09:04

    I believe Stefan's answer is right on. If you want to remove only one node (rather than all matching nodes), here is another example:

    //Load XML from file (or it could come from a POST, etc.)
    $xml = simplexml_load_file('fileName.xml');
    
    //Use XPath to find target node for removal
    $target = $xml->xpath("//seg[@id=$uniqueIdToDelete]");
    
    //If target does not exist (already deleted by someone/thing else), halt
    if(!$target)
    return; //Returns null
    
    //Import simpleXml reference into Dom & do removal (removal occurs in simpleXML object)
    $domRef = dom_import_simplexml($target[0]); //Select position 0 in XPath array
    $domRef->parentNode->removeChild($domRef);
    
    //Format XML to save indented tree rather than one line and save
    $dom = new DOMDocument('1.0');
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    $dom->loadXML($xml->asXML());
    $dom->save('fileName.xml');
    

    Note that sections Load XML... (first) and Format XML... (last) could be replaced with different code depending on where your XML data comes from and what you want to do with the output; it is the sections in between that find a node and remove it.

    In addition, the if statement is only there to ensure that the target node exists before trying to move it. You could choose different ways to handle or ignore this case.

    点赞 评论 复制链接分享
  • doutang1884 doutang1884 2010-03-13 02:36

    Your initial approach was right, but you forgot one little thing about foreach. It doesn't work on the original array/object, but creates a copy of each element as it iterates, so you did unset the copy. Use reference like this:

    foreach($doc->seg as &$seg) 
    {
        if($seg['id'] == 'A12')
        {
            unset($seg);
        }
    }
    
    点赞 评论 复制链接分享
  • douxie9471 douxie9471 2010-03-17 10:55

    A new idea: simple_xml works as a array.

    We can search for the indexes of the "array" we want to delete, and then, use the unset() function to delete this array indexes. My example:

    $pos=$this->xml->getXMLUser();
    $i=0; $array_pos=array();
    foreach($this->xml->doc->users->usr[$pos]->u_cfg_root->profiles->profile as $profile) {
        if($profile->p_timestamp=='0') { $array_pos[]=$i; }
        $i++;
    }
    //print_r($array_pos);
    for($i=0;$i<count($array_pos);$i++) {
        unset($this->xml->doc->users->usr[$pos]->u_cfg_root->profiles->profile[$array_pos[$i]]);
    }
    
    点赞 评论 复制链接分享
  • dongtiandexue123456 dongtiandexue123456 2010-08-26 17:57

    This work for me:

    $data = '<data>
    <seg id="A1"/>
    <seg id="A5"/>
    <seg id="A12"/>
    <seg id="A29"/>
    <seg id="A30"/></data>';
    
    $doc = new SimpleXMLElement($data);
    
    $segarr = $doc->seg;
    
    $count = count($segarr);
    
    $j = 0;
    
    for ($i = 0; $i < $count; $i++) {
    
        if ($segarr[$j]['id'] == 'A12') {
            unset($segarr[$j]);
            $j = $j - 1;
        }
        $j = $j + 1;
    }
    
    echo $doc->asXml();
    
    点赞 评论 复制链接分享
  • douhao5280 douhao5280 2010-09-10 19:13

    If you extend the base SimpleXMLElement class, you can use this method:

    class MyXML extends SimpleXMLElement {
    
        public function find($xpath) {
            $tmp = $this->xpath($xpath);
            return isset($tmp[0])? $tmp[0]: null;
        }
    
        public function remove() {
            $dom = dom_import_simplexml($this);
            return $dom->parentNode->removeChild($dom);
        }
    
    }
    
    // Example: removing the <bar> element with id = 1
    $foo = new MyXML('<foo><bar id="1"/><bar id="2"/></foo>');
    $foo->find('//bar[@id="1"]')->remove();
    print $foo->asXML(); // <foo><bar id="2"/></foo>
    
    点赞 评论 复制链接分享
  • duanbishai5271 duanbishai5271 2013-04-17 14:23

    Contrary to popular belief in the existing answers, each Simplexml element node can be removed from the document just by itself and unset(). The point in case is just that you need to understand how SimpleXML actually works.

    First locate the element you want to remove:

    list($element) = $doc->xpath('/*/seg[@id="A12"]');
    

    Then remove the element represented in $element you unset its self-reference:

    unset($element[0]);
    

    This works because the first element of any element is the element itself in Simplexml (self-reference). This has to do with its magic nature, numeric indices are representing the elements in any list (e.g. parent->children), and even the single child is such a list.

    Non-numeric string indices represent attributes (in array-access) or child-element(s) (in property-access).

    Therefore numeric indecies in property-access like:

    unset($element->{0});
    

    work as well.

    Naturally with that xpath example, it is rather straight forward (in PHP 5.4):

    unset($doc->xpath('/*/seg[@id="A12"]')[0][0]);
    

    The full example code (Demo):

    <?php
    /**
     * Remove a child with a specific attribute, in SimpleXML for PHP
     * @link http://stackoverflow.com/a/16062633/367456
     */
    
    $data=<<<DATA
    <data>
        <seg id="A1"/>
        <seg id="A5"/>
        <seg id="A12"/>
        <seg id="A29"/>
        <seg id="A30"/>
    </data>
    DATA;
    
    
    $doc = new SimpleXMLElement($data);
    
    unset($doc->xpath('seg[@id="A12"]')[0]->{0});
    
    $doc->asXml('php://output');
    

    Output:

    <?xml version="1.0"?>
    <data>
        <seg id="A1"/>
        <seg id="A5"/>
    
        <seg id="A29"/>
        <seg id="A30"/>
    </data>
    
    点赞 评论 复制链接分享
  • dongwo1234 dongwo1234 2013-05-22 12:29

    I was also strugling with this issue and the answer is way easier than those provided over here. you can just look for it using xpath and unset it it the following method:

    unset($XML->xpath("NODESNAME[@id='test']")[0]->{0});
    

    this code will look for a node named "NODESNAME" with the id attribute "test" and remove the first occurence.

    remember to save the xml using $XML->saveXML(...);

    点赞 评论 复制链接分享
  • dongzhang8475 dongzhang8475 2013-07-02 13:15

    Since I encountered the same fatal error as Gerry and I'm not familiar with DOM, I decided to do it like this:

    $item = $xml->xpath("//seg[@id='A12']");
    $page = $xml->xpath("/data");
    $id = "A12";
    
    if (  count($item)  &&  count($page) ) {
        $item = $item[0];
        $page = $page[0];
    
         // find the numerical index within ->children().
        $ch = $page->children();
        $ch_as_array = (array) $ch;
    
        if (  count($ch_as_array)  &&  isset($ch_as_array['seg'])  ) {
            $ch_as_array = $ch_as_array['seg'];
            $index_in_array = array_search($item, $ch_as_array);
            if (  ($index_in_array !== false)
              &&  ($index_in_array !== null)
              &&  isset($ch[$index_in_array])
              &&  ($ch[$index_in_array]['id'] == $id)  ) {
    
                 // delete it!
                unset($ch[$index_in_array]);
    
                echo "<pre>"; var_dump($xml); echo "</pre>";
            }
        }  // end of ( if xml object successfully converted to array )
    }  // end of ( valid item  AND  section )
    
    点赞 评论 复制链接分享
  • du4822 du4822 2016-01-23 23:33

    With FluidXML you can use XPath to select the elements to remove.

    $doc = fluidify($doc);
    
    $doc->remove('//*[@id="A12"]');
    

    https://github.com/servo-php/fluidxml


    The XPath //*[@id="A12"] means:

    • in any point of the document (//)
    • every node (*)
    • with the attribute id equal to A12 ([@id="A12"]).
    点赞 评论 复制链接分享
  • dtwxmn8741 dtwxmn8741 2016-03-09 09:07

    If you want to cut list of similar (not unique) child elements, for example items of RSS feed, you could use this code:

    for ( $i = 9999; $i > 10; $i--) {
        unset($xml->xpath('/rss/channel/item['. $i .']')[0]->{0});
    }
    

    It will cut tail of RSS to 10 elements. I tried to remove with

    for ( $i = 10; $i < 9999; $i ++ ) {
        unset($xml->xpath('/rss/channel/item[' . $i . ']')[0]->{0});
    }
    

    But it works somehow randomly and cuts only some of the elements.

    点赞 评论 复制链接分享
  • doujing5846 doujing5846 2016-08-29 09:25

    To remove/keep nodes with certain attribute value or falling into array of attribute values you can extend SimpleXMLElement class like this (most recent version in my GitHub Gist):

    class SimpleXMLElementExtended extends SimpleXMLElement
    {    
        /**
        * Removes or keeps nodes with given attributes
        *
        * @param string $attributeName
        * @param array $attributeValues
        * @param bool $keep TRUE keeps nodes and removes the rest, FALSE removes nodes and keeps the rest 
        * @return integer Number o affected nodes
        *
        * @example: $xml->o->filterAttribute('id', $products_ids); // Keeps only nodes with id attr in $products_ids
        * @see: http://stackoverflow.com/questions/17185959/simplexml-remove-nodes
        */
        public function filterAttribute($attributeName = '', $attributeValues = array(), $keepNodes = TRUE)
        {       
            $nodesToRemove = array();
    
            foreach($this as $node)
            {
                $attributeValue = (string)$node[$attributeName];
    
                if ($keepNodes)
                {
                    if (!in_array($attributeValue, $attributeValues)) $nodesToRemove[] = $node;
                }
                else
                { 
                    if (in_array($attributeValue, $attributeValues)) $nodesToRemove[] = $node;
                }
            }
    
            $result = count($nodesToRemove);
    
            foreach ($nodesToRemove as $node) {
                unset($node[0]);
            }
    
            return $result;
        }
    }
    

    Then having your $doc XML you can remove your <seg id="A12"/> node calling:

    $data='<data>
        <seg id="A1"/>
        <seg id="A5"/>
        <seg id="A12"/>
        <seg id="A29"/>
        <seg id="A30"/>
    </data>';
    
    $doc=new SimpleXMLElementExtended($data);
    $doc->seg->filterAttribute('id', ['A12'], FALSE);
    

    or remove multiple <seg /> nodes:

    $doc->seg->filterAttribute('id', ['A1', 'A12', 'A29'], FALSE);
    

    For keeping only <seg id="A5"/> and <seg id="A30"/> nodes and removing the rest:

    $doc->seg->filterAttribute('id', ['A5', 'A30'], TRUE);
    
    点赞 评论 复制链接分享

相关推荐