Xml in PHP

Here’s the translation of the XML handling code from Go to PHP, formatted in Markdown suitable for Hugo:

<?php

// The SimpleXMLElement class provides a simple way to work with XML data in PHP.
// We'll use it to create and parse XML.

// Define a Plant class that will be mapped to XML.
class Plant {
    public $id;
    public $name;
    public $origin = [];

    public function __construct($id, $name) {
        $this->id = $id;
        $this->name = $name;
    }

    public function __toString() {
        return sprintf("Plant id=%d, name=%s, origin=%s",
            $this->id, $this->name, implode(", ", $this->origin));
    }
}

// Create a Plant object
$coffee = new Plant(27, "Coffee");
$coffee->origin = ["Ethiopia", "Brazil"];

// Create an XML element from the Plant object
$xml = new SimpleXMLElement('<plant/>');
$xml->addAttribute('id', $coffee->id);
$xml->addChild('name', $coffee->name);
foreach ($coffee->origin as $origin) {
    $xml->addChild('origin', $origin);
}

// Output the XML, using DOM to format it nicely
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
echo $dom->saveXML();

// Add a generic XML header to the output
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo $dom->saveXML();

// Parse the XML string back into a SimpleXMLElement
$parsed = new SimpleXMLElement($xml->asXML());
$p = new Plant((int)$parsed['id'], (string)$parsed->name);
$p->origin = (array)$parsed->origin;
echo $p . "\n";

// Create another Plant
$tomato = new Plant(81, "Tomato");
$tomato->origin = ["Mexico", "California"];

// Create a nested structure
$nesting = new SimpleXMLElement('<nesting/>');
$parent = $nesting->addChild('parent');
$child = $parent->addChild('child');
foreach ([$coffee, $tomato] as $plant) {
    $plantXml = $child->addChild('plant');
    $plantXml->addAttribute('id', $plant->id);
    $plantXml->addChild('name', $plant->name);
    foreach ($plant->origin as $origin) {
        $plantXml->addChild('origin', $origin);
    }
}

// Output the nested XML structure
$dom->loadXML($nesting->asXML());
echo $dom->saveXML();

This PHP code demonstrates XML handling, including creating XML from objects, parsing XML into objects, and working with nested XML structures. Here’s a breakdown of what the code does:

  1. We define a Plant class that represents our data structure.

  2. We create a Plant object and convert it to XML using SimpleXMLElement.

  3. We output the XML, using DOMDocument to format it nicely.

  4. We demonstrate how to add an XML header to the output.

  5. We parse the XML string back into a SimpleXMLElement and then into a Plant object.

  6. We create a nested XML structure with multiple plants.

  7. Finally, we output the nested XML structure.

Note that PHP’s SimpleXMLElement and DOMDocument classes provide functionality similar to Go’s encoding/xml package. The SimpleXMLElement class is particularly useful for creating and manipulating XML structures in an object-oriented way.

To run this PHP script, save it to a file (e.g., xml_handling.php) and execute it using the PHP command-line interpreter:

$ php xml_handling.php

This will output the XML representations and the parsed Plant object, demonstrating PHP’s XML handling capabilities.