2013-04-22 43 views
0

我試圖刪除XML結構中特定的特定節點。我希望能夠刪除Employee元素的所有子元素,這些元素包含我從表單域指定的特定用戶名以及所有與其相關的子級。我一直使用SimpleXML來加載所有的數據,但我可以通過另一種方法重新加載數據。我想知道刪除所有孩子的最好方法是什麼,不管這個元素有多少層次。迄今爲止我所遇到的所有解決方案都只有一個層次。我想刪除用戶名爲UserToDelete的用戶。結構的一個例子如下。如何使用PHP刪除XML元素和所有它的多級子項?

<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,包括所有的孩子,包括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>";得到一個輸出。

+0

你有沒有測試是否' $ xml-> Employee'包含任何元素,並且$ username == $ employee-> Access_Info-> Username'條件是否觸發? – 2013-04-22 05:38:52

+0

好吧,如果我註釋掉'echo $ dom-> saveXML('employees.xml');'。它在下面的echo聲明中回顯出來。如果xml數據不清楚,則xml數據將存儲在employees.xml中。所以很明顯,從xml對象獲取數據,特別是考慮到它是通過SimpleXML加載的,並且我在別處使用了相同的結構。 – Elias 2013-04-22 05:42:48

+0

什麼類型的變量是'$ dom',它來自哪裏?你有錯誤報告激活,所以你看看是否有任何錯誤的'saveXML()'調用? (這可能是與實際XML無關的問題) – 2013-04-22 05:51:09

回答

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(); 
相關問題