2015-01-29 2 views
0

foreach 루프에서 XML을 업데이트/수정하는 방법은 무엇입니까?PHP의 foreach 루프에서 XML 수정하기

foreach($xml as $code) 
{ 
    // Remove each item, so to keep the only one inside, and each time move forward by removing all other items 

    // if ($xml->node->code != $code) 
     // remove it 

} 

나는 내가 매번 동일하지 노드를 제거 할 방법을 모른다 : 나는 foreach 루프가 내 코드에서

<items> 
    <item> 
     <client>1</client> 
     <retailer>RET1</retailer> 
     <code>c553d8be7f3e88bda6a26b8e7eacd11a6001e0ba5f3c6e26ddbf4a08d9e277f3</code> 
     <id>1234567891011</id> 
    </item> 
    <item> 
     <client>1</client> 
     <retailer>RET1</retailer> 
     <code>9afb1b44a582d76159626d05aaf414983625530d83cbfa40a1359bf0afd9225b</code> 
     <id>1234567891011</id> 
    </item> 
    <item> 
     <client>1</client> 
     <retailer>RET1</retailer> 
     <code>90f5ff04e07b0701b6db3c6e44ed2f1636b64fb9eb78f9121dc684b8b8339741</code> 
     <id>1234567891011</id> 
    </item> 
</items> 

:

가 나는 XML과 같이가 코드 값, 어떤 제안?

+0

더 나은 아니라 어떻게 타의 추종을 불허하는 – Veerendra

+0

어떤 조언을 제거하는 것보다 일치 하나 새 XML을 만드는 모든 문제가 지속되면 알려주세요 DOMDOcument와 함께할까요? – aspirinemaga

답변

0

아래처럼 작업을 수행 할 수 있습니다

<?php 
$xml = simplexml_load_string($string);//$string contains your xml 
$item = $xml->item; 
$count_node = count($item); 
for($i=0;$i<$count_node;$i++) 
{ 
    echo $item[$i]->code;// access the tags like this and check your conditions 
} 
?> 

당신이

0

domdocument 및 removechild를 사용하여 노드를 제거하십시오.

$client = $items->getElementsByTagName('client')->item(0); 
$items->removeChild($client); 
+0

첫 줄에'$ client = $ items->'하고 있습니다 ... DOMDocument를'$ items'에 어떻게로드합니까? '$ items = DOMDocument :: loadXML ($ xml_string)'이 작동하지 않습니다. – aspirinemaga