php generate XML

不言
不言Original
2018-04-16 16:19:011419browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn