Passionnés d'informatique depuis 1990

SimpleXMLElement snippets

Par le 10 Mar 2021 dans Brouillons, projets, prototypes divers, Documentation, Php :: Scripts | 0 comments

Open from a file

$xml = simplexml_load_file(file.xml');

or
in file.php :

<?php
$xmlstr = <<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<lheo xmlns="https://www.of.moncompteformation.gouv.fr">
<offres></offres>
</lheo>
XML;
?>

then

include 'file.php';
$xml = new SimpleXMLElement($xmlstr);

To get a node :

$child = $xml->action->{'lieu-de-formation'}->coordonnees->{$field}

Notice {} when node syntax too complex or dynamic var used

Set node value

$new_action =  $xml->offres->formation[0]->action;
$new_action->{0}="777";

To get a child attribute

$xml->action['attr_name']

Select a node by his attribute

Ex : search node
With xpath
by specifying full path node

$foundNodes = $new_action->xpath(adresse/extras/extra[@info="'.$field.'"]');
$foundNode = $foundNodes[0];
$foundNode->{0} = $value;

Or relative path
Namsespace required here (xml interface format given by external url )

$namespaces = $xml->getNamespaces(true);
$namespace = $namespaces[0] ; // or
$namespace = "https://www.of.moncompteformation.gouv.fr";
$new_formation->registerXPathNamespace('c', $namespace);
$foundNodes = $new_formation->xpath('//c:extra[@info="'.$field.'"]');
$foundNode = $foundNodes[0];
$foundNode->{0} = $value;

Display xml

echo $new_offres->asXML();

Write/save in a file

$root->asXml('file.xml');

Append child

function sxml_append(SimpleXMLElement $target ,SimpleXMLElement $insert, $nextSibling=true )
{
$target_dom = dom_import_simplexml($target);
$insert_dom = $target_dom->ownerDocument->importNode(dom_import_simplexml($insert), true);
if ($nextSibling) {
$target_dom->parentNode->insertBefore($insert_dom, $target_dom->nextSibling);
} else {
$target_dom->appendChild($insert_dom);
}
}

$nextSibling = true, add child after last target node
$nextSibling = false, add child at end of node child

Exemples:

xml at start

<lheo>
<offres>
<formation>
<action>action 1</action>
<rooo>rooo</rooo>
</formation>
<formation>stuff</formation>
</offres>
</lheo>

sxml_append($xml->offres->formation[0]->action[0], $new_action, true);

outpout:

<lheo>
<offres>
<formation>
<action>action 1</action>
<action>action 2</action>
<rooo>rooo</rooo>
</formation>
<formation>stuff</formation>
</offres>
</lheo>

sxml_append($xml->offres->formation[0], $new_action, false);

outpout:

<lheo>
<offres>
<formation>
<action>action 1</action>
<rooo>rooo</rooo>
<action>action 2</action>
</formation>
<formation>stuff</formation>
</offres>
</lheo>

Dupplicate node

You can’t use 1 already node to append it again in same xml cause it‘ll only be modified instead of being dupplicated.
Then you have to create 1.

using a xml string (see Open from a file )

include 'file.php';
$xml = new SimpleXMLElement($xmlstr);

Or using an already existing node, converting it into a string to create a new SimpleXMLElement

//$new_action is a node
$new_action2 = new SimpleXMLElement($new_action->asXML());
$new_action2->{0}="value";
sxml_append($xml->offres->formation[0], $new_action2);

Delete node

Unset($xml->offres->formation[0]) ;

Delete nodes

$nbre = count($old_actions);
for($i=0;$i<$nbre;$i++)
	     {
	      unset($old_actions[0]);
	     }
Mots-clé: , ,