2013-04-22 3 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 

내가 contact_infoaccess_info 내의 요소를 포함한 모든 아이들을 포함하여 PHP를 사용하여 전체 직원을 삭제하려면 어떻게 질문의 포인트에 따라서. 얼마나 많은 레벨의 노드가 깊은 지 또는 다음과 같은 것을 할 수 있습니까?

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' 조건이 실행되는지 여부를 결정합니다. –

+0

글쎄,'echo $ dom-> saveXML ('employees.xml');'을 주석 처리하면됩니다. 아래의 echo 문을 출력합니다. xml 데이터가 명확하지 않은 경우 employees.xml에 저장됩니다. 그래서 분명히 xml 객체로부터 데이터를 얻고 있습니다. 특히 SimpleXML을 통해로드되고 다른 곳에서 같은 구조체를 사용한다는 점을 감안하면 더욱 그렇습니다. – Elias

+0

어떤 종류의 변수가'$ dom'입니까? 오류보고 기능이 활성화되어있어'saveXML()'호출에 문제가 발생했는지 알 수 있습니까? (이것은 실제 XML과 관련없는 문제 일 수 있습니다.) –

답변

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();