2016-07-01 6 views
0

Ich habe die XML-Datei namens fonts.xml in meinem Dateisystem befindet.So aktualisieren Sie eine XML-Datei im Dateisystem bedingt - PHP

Ziel:

Ich möchte das Attribut aktualisieren <status> wo name ist „Aclonica“ aber ich weiß nicht, wie es bedingt zu tun.

XML:

<fonts> 
    <font> 
    <name>Aclonica</name> 
    <category>Aclonica</category> 
    <variants>100,bold</variants> 
    <status>active</status> 
    </font> 
    <font> 
    <name>Azeebe</name> 
    <category>Sans-serif</category> 
    <variants>100,bold,italic</variants> 
    <status>active</status> 
    </font> 
</fonts> 
+0

Sie diese Lösung für XML-Update beziehen können: http://stackoverflow.com/questions/20540592/replace-specific-node -value-in-xml-using-php –

+0

https://eval.in/598922 – splash58

+0

also hast du das Problem gelöst? – morels

Antwort

0

Sie benötigen DOMDocument Klasse. Auf diese Weise können Daten unter Verwendung von normalen if Bedingung wählen:

Lösung:

$a = $_POST['font']; // here value 'Aclonica' is assigned to $a 
$dom = new DOMDocument(); 

$dom->load('c:/xampp/htdocs/cms/public/fonts/font.xml'); 

foreach ($dom->documentElement->childNodes as $node) { 

    // print_r($node); // >> uncomment for debug purposes 

    if($node->nodeType == 1) { 
     $name = $node->getElementsByTagName('name')->Item(0); 

     if($name->nodeValue == $a) { // >> IMPORTANT: here is the condition you need 

     $OldJobId = $node->getElementsByTagName('status')->Item(0); 

     if($OldJobId->nodeValue == 'active') { 

      $newelement = $dom->createElement('status','inactive'); 

      $OldJobId->parentNode->replaceChild($newelement, $OldJobId); 
     }else{ 

      $newelement = $dom->createElement('status','active'); 
      $OldJobId->parentNode->replaceChild($newelement, $OldJobId); 
     } 
     } 
    } 
} 

$dom->save("c:/xampp/htdocs/cms/public/fonts/font.xml"); 
Verwandte Themen