I am trying to build a PHP script to parse a rather complex XML document and output CSV data for certain names spaces within the document - But my simple code just to get the basic construct decoded is not working. This is my first effort in this space hope someone here might see the error of my ways and help a little --
My Code looks like this right now -- Generating
L1 config L2 category L3 set L4 collection L5 set L5
<?php
$filexml='contacts_20211226.xml';
if (file_exists($filexml))
{
echo "File name: ", $filexml ;
echo "\n" ;
$xml = simplexml_load_file($filexml);
if ($xml === false) {
echo "Failed loading XML: ";
foreach(libxml_get_errors() as $error) {
echo $error->message;
echo "\n" ;
}
}
$f = fopen('test.csv', 'w');
createCsv($xml, $f);
fclose($f);
} else {
echo "No File name: ", $filexml ;
echo "\n" ;
}
function createCsv($xml,$f)
{
$l1Name = $xml->getName() ;
echo 'L1 ' . $l1Name ;
echo "\n" ;
foreach ($xml->children() as $second_gen) {
$l2Name = $second_gen->getName() ;
echo 'L1 ' . $l1Name ;
' L2 ' . $l2Name ;
echo "\n" ;
foreach ($second_gen->children() as $third_gen) {
$l3Name = $third_gen->getName() ;
echo 'L1 ' . $l1Name ;
' L2 ' . $l2Name ;
' L3 ' . $l3Name ;
echo "\n" ;
foreach ($third_gen->children() as $fourth_gen) {
$l4Name = $fourth_gen->getName() ;
echo 'L1 ' . $l1Name .
' L2 ' . $l2Name .
' L3 ' . $l3Name .
' L4 ' . $$l4name ;
echo "\n" ;
foreach ($fourth_gen->children() as $fifth_gen) {
$l5Name = $fifth_gen->getName() ;
echo 'L1 ' . $l1Name .
' L2 ' . $l2Name .
' L3 ' . $l3Name .
' L4 ' . $l4Name .
' L5 ' . $l5Name .
' L5 ' . $fifth_gen ;
echo "\n" ;
}
}
}
}
}
?>
But this will not output the that I expect --- For example --
I get this as output But never see the values in name=" " or the actual data elements.
L1 config L2 category L3 set L4 where as I would like to see
L1 config L2 catergory "PCRContacts" L3 set "PCRContacts" alias "Talk-around" L4 field .......... L5 collection ....
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
L1 = <config>
L2 = <category name="PCRContacts">
L3 = <set name="PCRContacts" alias="Talk-around">
L4 = <field name="ContactName">Talk-around</field>
L5 = <collection name="MDCCalls" />
L5 = <collection name="QuikCallIICalls" />
L5 = <collection name="DigitalCalls">
L6 = <set name="DigitalCalls" index="0" key="GRPCALL">:q!
L7 = <field name="DU_CALLALIAS">Talk-around</field>
<field name="DU_CALLLSTID">10000</field>
<field name="DU_ROUTETYPE" Name="Regular">REGULAR</field>
<field name="DU_CALLPRCDTNEN">False</field>
<field name="DU_RINGTYPE" Name="No Style">NOSTYLE</field>
<field name="DU_TXTMSGALTTNTP" Name="Repetitive">REPETITIVE</field>
<field name="DU_CALLTYPE" Name="Group Call">GRPCALL</field>
<field name="DU_OVCMCALL">False</field>
<field name="DU_CALLTYPEPART2">0</field>
<field name="DU_UKPOTCFLG">False</field>
<field name="DU_RVRTPERS_Zone" Name="None">NONE</field>
<field name="DU_RVRTPERS" Name="Selected">SELECTED</field>
<field name="CallType">Digital Calls-Group Call</field>
<field name="PeudoCallId">10000</field>
L6 = </set>
L5 = </collection>
<collection name="CapacityPlusCalls" />
<collection name="PhoneCalls" />
<field name="Comments"></field>
// Object Properties
// Loop through the children of the <set name="PCRContacts"> node
foreach ($xml->category[0]->set->children() as $element) {
echo $element->getName() . '|' . $element['name'] . PHP_EOL;
}
// XPath
// Loop through all the <field> elements that are children of the <set name="DigitalCalls"> node
$fields = $xml->xpath("//config/category/set/collection/set[@name='DigitalCalls']/field");
foreach ($fields as $field) {
echo $field['name'] . '|' . $field . PHP_EOL;
}
https://www.php.net/manual/en/simplexml.examples-basic.php
now you can export the array to a csv
https://stackoverflow.com/questions/4249432/export-to-csv-via-php/13474770#13474770