Detailed explanation of XML operation based on PHP

  • 2020-06-07 04:09:27
  • OfStack

< ?php

$xml = simplexml_load_file (' example. xml '); // Create the SimpleXML object
var_dump ($xml); / / output XML
? >

< ?php
$xml = simplexml_load_file (' example. xml '); // Read the XML file
foreach($xml- > depart as $a) // Loop reads every depart tag in the XML data
{
echo "$a- > name < BR > "; // Output the name attribute
}
? >

< ?php
$xml = simplexml_load_file (' example. xml '); // Read the XML file
echo $xml- > depart- > name [0]. // Output node
? >

< ?php
$xml = simplexml_load_file('example.xml');
foreach ($xml- > depart- > children() as $depart) // Read the child tags under the depart tag in a loop
{
var_dump ($depart); // Output XML data for the tag
}
? >

< ?php
$xml = simplexml_load_file (' example. xml '); // Read XML file
$result = $xml- > xpath ('/departs depart/employees employee/name '); // Define nodes
var_dump ($result); // Output node
? >

< ?php
$xml = simplexml_load_file (' example. xml '); / / read XML
$xml- > depart- > name [0] = "Human Resource"; // Modify nodes
? >

< ?php
$xml = simplexml_load_file (' example. xml '); // Read XML data
echo $xml- > asXML (); // Standardize XML data
? >

< ?php
$xml = simplexml_load_file (' example. xml '); // Read XML data
$newxml = $xml- > asXML (); // Standardize XML data
$fp = fopen (" newxml xml ", "w"); // Open the file to write XML data
fwrite ($fp, $newxml); // Write XML data
fclose ($fp); // Close the file
? >


Related articles: