数组
代码如下 | 复制代码 |
$arr[] = array( 'name' => $row['name'], 'sex' => $row['sex'], 'age' => $row['age'] ); |
生成php
代码如下 | 复制代码 |
$doc = new DOMDocument('1.0', 'utf-8'); // 声明版本和编码 $doc->formatOutput = true; $doc->appendChild($r); $b = $doc->createElement("data"); $name->appendChild($doc->createTextNode($dat['name'])); $b->appendChild($name); $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); } |
这样就生成了一个xml文件了
解析xml文件也是非常的简单了
代码如下 | 复制代码 |
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( 'name' => $name, 'sex' => $sex, 'age' => $age ); } $i++; } } //print_r($arr); |