0

次のようなxmlファイルがあります。

<?xml version="1.0"?>
<datas>
<books> 
<book>
<id>1</id>
<title>PHP Undercover</title>     
<author>Wiwit Siswoutomo</author>
</book>`enter code here`
<book>
<id>2</id>
<title>PHP Enterprise</title>     
<author>Wiwit Siswoutomo</author>
</book>
</books>
</datas>

そして今、私はこの方法で特別なタグを置き換えたいと思っています.xmlファイルを検索し、PHPエンタープライズがある場合はそれを見つけて「新しいタイトル」に置き換えます. 私は何をすべきか?TNX

4

2 に答える 2

0

このスクリプトはあなたを助けます!! これを試して :)

<?php
function replace_Special_Str($mystring)
{
    //Load file
    $filename="./myfile.xml";
    if (!file_exists($filename)) { echo "There is not a myfile.xml file in the directory."; exit;}
    $xml = simplexml_load_file($filename);

    //search and replace particular node by book title
    $node = $xml->xpath('/datas/books[title="' . $mystring. '"]');
    if(sizeof($node) > 0) 
    {                                                                              
        $node[0]->title = 'My Title';
    }
    $xml->asXML('./aucstatus.xml');
}
?>

またはこれを試してください、

<users>
     <name>John</name>
     <address>My Address</address>
     <zipcode>12345</zipcode>
     <city>My City</city>
     <phone>555 1234-4321</phone>
</users>

PHPファイル

fopen('users.xml');
while ($users->read()) {
     switch ($users->nodeType) {
          case (XMLReader::ELEMENT):
               if ($users->localName == "users") {
             $node = $reader->expand();
             $dom = new DomDocument();
             $n = $dom->importNode($node,true);
             $dom->appendChild($n);
             $simple_xml = simplexml_import_dom($n);
             $id = $simple_xml['id'];
             $name = $simple_xml->name;
             $address = $simple_xml->address;
                     // Custom code insert, update, whatever...
               }
     }
}
于 2013-05-27T06:46:35.560 に答える