search
Homephp教程php手册PHP中DOMDocument生成与解析XML格式数据

DOMDocument这个功能是我们php中自带的一个功能,下面我来介绍利用DOMDocument生成xml文档,并且也介绍DOMDocument解析xml文件的方法,有需要的朋友可参考.

数组实例代码如下:

<?php
$arr[] = array(
    &#39;name&#39; => $row[&#39;name&#39;],
    &#39;sex&#39; => $row[&#39;sex&#39;],
    &#39;age&#39; => $row[&#39;age&#39;]
);
?>

生成php,代码如下:

<?php
$doc = new DOMDocument(&#39;1.0&#39;, &#39;utf-8&#39;); // 声明版本和编码
$doc->formatOutput = true;
$r = $doc->createElement("root");
$doc->appendChild($r);
foreach ($arr as $dat) {
    $b = $doc->createElement("data");
    $name = $doc->createElement("name");
    $name->appendChild($doc->createTextNode($dat[&#39;name&#39;]));
    $b->appendChild($name);
    $sex = $doc->createElement("sex");
    $sex->appendChild($doc->createTextNode($dat[&#39;sex&#39;]));
    $b->appendChild($sex);
    $age = $doc->createElement("age");
    $age->appendChild($doc->createTextNode($dat[&#39;age&#39;]));
    $b->appendChild($age);
    $r->appendChild($b);
}
echo $doc->saveXML();
?>

这样就生成了一个xml文件了.

解析xml文件也是非常的简单了,代码如下:

<?php
header("Content-type:text/html; Charset=utf-8");
$url = "createXML.php";
$reader = new XMLReader(); //实例化XMLReader
$reader->open($url); //获取xml
$i = 1;
while ($reader->read()) {
    if ($reader->nodeType == XMLReader::TEXT) { //判断node类型
        $m = $i % 3;
        if ($m == 1) $name = $reader->value; //读取node值
        if ($m == 2) $sex = $reader->value;
        if ($m == 0) {
            $age = $reader->value;
            $arr[] = array(
                &#39;name&#39; => $name,
                &#39;sex&#39; => $sex,
                &#39;age&#39; => $age
            );
        }
        $i++;
    }
}
//print_r($arr);
?>


文章地址:

转载随意^^请带上本文地址!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!