The content of this article is about php generating XML, which has a certain reference value. Now I share it with everyone. Friends in need can refer to it
$doc = new DOMDocument('1.0', 'utf-8'); // 声明版本和编码 $doc->formatOutput = true; $r = $doc->createElement("root"); $doc->appendChild($r); $arr = array( array( 'name' => 'zhangsan', 'sex' => 'male', 'age' => 20, ), array( 'name' => 'lisi', 'sex' => 'female', 'age' => 25, ), ); foreach ($arr as $dat) { $b = $doc->createElement("data"); $name = $doc->createElement("name"); $name->appendChild($doc->createTextNode($dat['name'])); $b->appendChild($name); $sex = $doc->createElement("sex"); $sex->appendChild($doc->createTextNode($dat['sex'])); $b->appendChild($sex); $age = $doc->createElement("age"); $age->appendChild($doc->createTextNode($dat['age'])); $b->appendChild($age); $r->appendChild($b); }
Related recommendations:
PHP generates unique RequestID class
The above is the detailed content of php generate XML. For more information, please follow other related articles on the PHP Chinese website!