Home > Backend Development > PHP Tutorial > php generate XML

php generate XML

不言
Release: 2023-03-24 06:38:01
Original
1400 people have browsed it

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);
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template