Home > Backend Development > PHP Tutorial > PHP method to convert array to XML_PHP tutorial

PHP method to convert array to XML_PHP tutorial

WBOY
Release: 2016-07-13 10:04:26
Original
860 people have browsed it

How to convert arrays to XML in php

This article mainly introduces the method of converting arrays to XML in php, and analyzes php's operation of arrays and XML format files with examples. The skills have certain reference value. Friends in need can refer to it

The example in this article describes the method of converting an array into XML in PHP. Share it with everyone for your reference. The details are as follows:

1. The php code is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

class A2Xml {

private $version = '1.0';

private $encoding = 'UTF-8';

private $root = 'root';

private $xml = null;

function __construct() {

$this->xml = new XmlWriter();

}

function toXml($data, $eIsArray=FALSE) {

if(!$eIsArray) {

$this->xml->openMemory();

$this->xml->startDocument($this->version, $this->encoding);

$this->xml->startElement($this->root);

}

foreach($data as $key => $value){

 

if(is_array($value)){

$this->xml->startElement($key);

$this->toXml($value, TRUE);

$this->xml->endElement();

continue;

}

$this->xml->writeElement($key, $value);

}

if(!$eIsArray) {

$this->xml->endElement();

return $this->xml->outputMemory(true);

}

}

}

$res = array(

'hello' => '11212',

'world' => '232323',

'array' => array(

'test' => 'test',

'b' => array('c'=>'c', 'd'=>'d')

),

'a' => 'haha'

);

$xml = new A2Xml();

echo $xml->toXml($res);

1 2

3

4php实现将数组转换为XML的方法    帮客之家

5

6 7

8

10 11 12 13 14
15 16
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
<🎜>class A2Xml {<🎜> <🎜>private $version = '1.0';<🎜> <🎜>private $encoding = 'UTF-8';<🎜> <🎜>private $root = 'root';<🎜> <🎜>private $xml = null;<🎜> <🎜>function __construct() {<🎜> <🎜>$this->xml = new XmlWriter(); } function toXml($data, $eIsArray=FALSE) { if(!$eIsArray) { $this->xml->openMemory(); $this->xml->startDocument($this->version, $this->encoding); $this->xml->startElement($this->root); } foreach($data as $key => $value){ if(is_array($value)){ $this->xml->startElement($key); $this->toXml($value, TRUE); $this->xml->endElement(); continue; } $this->xml->writeElement($key, $value); } if(!$eIsArray) { $this->xml->endElement(); return $this->xml->outputMemory(true); } } } $res = array( 'hello' => '11212', 'world' => '232323', 'array' => array( 'test' => 'test', 'b' => array('c'=>'c', 'd'=>'d') ), 'a' => 'haha' ); $xml = new A2Xml(); echo $xml->toXml($res);
2. The operation effect is shown in the figure below: I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/965529.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/965529.htmlTechArticleHow to convert arrays to XML in php. This article mainly introduces how to convert arrays to XML in php. , the example analyzes the skills of operating arrays and XML format files in PHP, with certain reference...
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