PHP implements the method of generating and parsing xml based on SimpleXML

墨辰丷
Release: 2023-03-26 20:48:02
Original
1734 people have browsed it

This article mainly introduces the method of generating and parsing xml based on SimpleXML in PHP. It analyzes the specific operation skills of using PHP to generate and parse xml format data using SimpleXML in the form of a complete example. Friends who need it can refer to it

Xml will not be explained much. PHP also provides methods for operating XML. PHP can operate XML in many ways, such as domdocment, simplexml, xmlwriter, etc. The simplest one should be simplexml. This time, let’s talk about simplexml. Read and parse xml files or strings

1. Generate xml strings and files

<?php
 header("Content-type: text/html; charset=utf-8");
 $xml=new SimpleXMLElement(&#39;<?xml version="1.0" encoding="utf-8"?><UsersInfo />&#39;);
 $item=$xml->addchild("item");
 $item->addchild("name","冯绍峰");
 $item->addchild("age","30");
 $item2=$xml->addchild("item");
 $item2->addchild("name","潘玮柏");
 $item2->addchild("age","29");
 $item2->addAttribute("id","02");
 header("Content-type: text/xml");
 echo $xml->asXml();
 $xml->asXml("student.xml");
?>
Copy after login

The most important methods for generating xml are addchild, addAttribute, and asXml. If you just generate an xml file, you don’t need the header. The following is the display result of the browser

Isn’t it very simple?

2. simplexml parses xml files or strings

<?php
 header("Content-type: text/html; charset=utf-8");
 $xml=simplexml_load_file("UserInfo.xml");
 //通过children取得根节点下面的子项
 for($i=0;$i<count($xml->children());$i++){
   foreach ($xml->children()[$i] as $key => $value ) {
    echo "$key:$value"."<br/>";
   }
 }
?>
Copy after login

The above method is suitable for parsing xml files. If it is an xml string, just change simplexml_load_file to simplexml_load_string. Children is used to obtain the root node or child node. The obtained node is an array necessary for direct traversal. Just add filtering conditions. The following is the result of analysis

By the way, post my xml file

<?xml version="1.0" encoding="UTF-8"?>
<UsersInfo>
 <item>
   <name>潘玮柏</name>
   <address>上海市浦东新区</address>
   <song>快乐崇拜</song>
 </item>
 <item>
   <name>蔡依林</name>
   <address>上海市徐汇区</address>
   <song>独占神话</song>
 </item>
</UsersInfo>
Copy after login

Related recommendations:

PHP xml parsing and generationSimpleXMLUsage introduction

php SimpleXML() function usage detailed explanation

php uses simplexml to parse xml

The above is the detailed content of PHP implements the method of generating and parsing xml based on SimpleXML. 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