如何使用PHP删除XML元素及其所有多层次子元素?

时间:2013-04-22 04:45:36

标签: php xml simplexml removechild

我正在尝试删除XML结构中特定的特定节点。我希望能够删除Employee元素的所有子元素,其中包含我从表单字段指定的特定用户名以及与之相关的所有子元素。我一直在使用SimpleXML加载所有数据,但我可以通过另一种方法重新加载数据。我想知道删除所有孩子的最佳方法是什么,无论元素的深度是多少。到目前为止,我遇到的所有解决方案只有一个层次。我想删除用户名为UserToDelete的用户。结构的一个例子如下。

<Employees>
  <Employee Status="Part-time">
     <First_Name>...</First_Name>
     <Last_Name>...</Last_Name>
     <Position>...</Position>
     <SSN>...</SSN>
     <Contact_Info>
         <Office_Phone>...</Office_Phone>
         <Email>...</Email>
         <Cell_Phone>...</Cell_Phone>
     </Contact_Info>
     <Access_Info level="admin">
         <Username>UserToDelete</Username>
         <Password>...</Password>
     </Access_Info>
     <Department>...</Department>
     <Date_Started>...</Date_Started>
     <Salary>...</Salary>
     <Years>5</Years>
     <Photo>...</Photo>
  </Employee>
  <Employee>  
     ....
  </Employee>
  ...
</Employees>

所以问题是如何使用PHP删除整个Employee,包括所有子项,包括contact_infoaccess_info中的元素。节点有多深,或者我可以做如下的事情是否重要?

foreach ($xml->Employee as $employee) {
    if($username == $employee->Access_Info->Username) {
        foreach ($family as $node) {
            $node->parentNode->removeChild($node);
        }
        echo $dom->saveXML('employees.xml');
        echo "<font color='red'>".$username." deleted. </font>";
    }

}

修改

我没有在echo "<font color='red'>".$username." deleted. </font>";上获得输出。

1 个答案:

答案 0 :(得分:1)

  

所以问题是如何删除整个员工

xml.xml:

<?xml version="1.0"?>
<Employees>

<Employee>
  <Username>Joe</Username>
  <Password>...</Password>
</Employee>


<Employee>
    <Access_Info level="admin">
      <Username>Jane</Username>
      <Password>...</Password>
    </Access_Info>
</Employee>


<Employee Status="Part-time">
     <First_Name>...</First_Name>
     <Last_Name>...</Last_Name>
     <Position>...</Position>
     <SSN>...</SSN>
     <Contact_Info>
         <Office_Phone>...</Office_Phone>
         <Email>...</Email>
         <Cell_Phone>...</Cell_Phone>
     </Contact_Info>
     <Access_Info level="admin">
       <NestedOneMoreTime>
         <Username>EmployeeToDelete</Username>
         <Password>...</Password>
       </NestedOneMoreTime>
     </Access_Info>
     <Department>...</Department>
     <Date_Started>...</Date_Started>
     <Salary>...</Salary>
     <Years>5</Years>
     <Photo>...</Photo>
</Employee>

</Employees>

PHP:

$xml = simplexml_load_file("xml.xml");

foreach ($xml->Employee as $employee)
{
    //Because the needed xpath feature is broken in SimpleXML, create
    //a new xml document containing only the current Employee:
    $employee_xml = simplexml_load_string($employee->asXML()); 

    //Get all Username elements in employee_xml(there
    //should only be one, which will be at position 0 in the returned array):
    $usernames_array = $employee_xml->xpath("//Username");

    if($usernames_array[0] == "UserToDelete")
    {
        unset($employee[0]); //The first element of an Element object
                             //is a reference to its DOM location
        break; //Important--you don't want to keep iterating over something
               //from which you've deleted elements.  If you need to delete
               //more than one element, save the references in an array
               //and unset() them after this loop has terminated.
    }
}

echo $xml->asXML();