XML thành chuỗi PHP

Nhiều khi cần lưu trữ dữ liệu dưới dạng XML vào cơ sở dữ liệu hoặc vào tệp để sử dụng sau. Để thực hiện yêu cầu này cần chuyển đổi dữ liệu sang XML và lưu tệp XML. Các hàm mở rộng SimpleXML cung cấp bộ công cụ để chuyển đổi XML thành một đối tượng. Các đối tượng đó xử lý các bộ chọn thuộc tính bình thường và các bộ lặp mảng. ví dụ 1.  

php




<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
8

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
9

 

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
0

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
1
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
2
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
5
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
0
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
1
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
2

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
5
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
1
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
7

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3____880

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3____882
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
83
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
85

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
86
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
5
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
89
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
90
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
5
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
92
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
5
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
94
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
95
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
96

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3____898

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3____901

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
03
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
83
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
06
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
07
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
08
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
09
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
10

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
86

_______886____913

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
86
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
82
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
83
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
17
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
83
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
09
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
20

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
21

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
21
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
23

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
21
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
2______909
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
07
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
31
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
07
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
33

_______921____898

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
21

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
86
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
38
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
39

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
21

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
21
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
42

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
21
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4______931
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
07
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
09
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
96

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
86
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
98

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3____898

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
56
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
58

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
98

 

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
00

________ 001 ________ 05 ________ 003 ________ 883

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
05
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
08
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
07
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
09
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
08
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
11
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4

 

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
14

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
16
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
08
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
03
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
83

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
21
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
08
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
23
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
26
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
08
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
28
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3____031
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
08
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
33

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
35

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
96

 

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
37

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
38
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
2____001
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
96

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
42

đầu ra

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
2

Vấn đề trên có thể được giải quyết bằng hàm array_walk_recursive(). Hàm này chuyển đổi mảng thành tài liệu xml trong đó các khóa của mảng được chuyển đổi thành giá trị và giá trị của mảng được chuyển đổi thành phần tử của xml. ví dụ 2.  

php




<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
8

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
9

 

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
45

________ 001 ________ 05 ________ 003 ________ 883

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
51
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
08
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
53
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
56
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
08
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
58
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3____062

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
64
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
08
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
03
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
83

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
86
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
69
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
08
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
71
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
3
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
35

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
96

 

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
76

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
1
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
5
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
89
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
90
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
95
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
96

 

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
83

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
84

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
85
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
83
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
01
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
03
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
83
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
1
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
4
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
93
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
33

 

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
95

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
96
<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
1____958

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >
42

đầu ra

<?xml version="1.0"? >
<root >
       <x> a </x >
       <y> b </y >
       <z> c </z >
</root >

Ghi chú. Nếu hệ thống tạo lỗi loại. PHP Lỗi nghiêm trọng. Lỗi chưa xử lý. Không tìm thấy lớp 'SimpleXMLElement' trong /home/6bc5567266b35ae3e76d84307e5bdc78. php. 24 thì chỉ cần cài đặt các gói php-xml, php-simplexml

Làm cách nào để chuyển đổi XML thành chuỗi trong PHP?

Nó chuyển đổi một chuỗi được mã hóa JSON thành một biến PHP. .
Bước 1. Tạo tệp XML (Tùy chọn). Tạo một tệp XML cần chuyển đổi thành mảng. .
Bước 2. Chuyển đổi tệp thành chuỗi. Tệp XML sẽ nhập vào PHP bằng cách sử dụng hàm file_get_contents() đọc toàn bộ tệp dưới dạng chuỗi và lưu vào một biến

Làm cách nào để đọc chuỗi XML trong PHP?

Hàm Simplexml_load_string() của PHP được sử dụng để đọc dữ liệu XML từ một chuỗi.

Làm cách nào để lấy dữ liệu từ tệp XML trong PHP?

Sử dụng hàm simplexml_load_file để tải tệp XML bên ngoài trong chương trình PHP của bạn và tạo một đối tượng. Sau đó, bạn có thể truy cập bất kỳ phần tử nào từ XML bởi đối tượng này như sau. $xmldata = simplexml_load_file("nhân viên

Làm cách nào để chuyển đổi XML thành chuỗi trực tuyến?

Nó giúp thay thế dữ liệu XML của bạn thành văn bản XML có thể được trình bày trong HTML. Công cụ này cho phép tải URL XML, tải XML và xâu chuỗi thành văn bản. Nhấp vào nút URL, Nhập URL và Gửi. Người dùng cũng có thể chuyển đổi Tệp XML thành chuỗi XML bằng cách tải tệp lên .