Solved

PHP converting XML to CSV, capturing atributes on unique field tags

Posted on 2016-08-25
4
15 Views
Last Modified: 2016-09-13
Hi Guys,

I need to convert an XML to CSV, I have a example of both attached and a script I was trying to adapt to the job. However it continues to cause fail to upload.

I wish to write multiple XML files into the same CSV file thereby building a log of all incoming jobs.
Here is an example of the XML:
"<?xml version='1.0' encoding='UTF-8'?>
<ValueDescription Type="metadatafields">
 <MF_1 LocalizedTagName="Customer Name" ValueIsRequired="Yes" Format="" Tooltip="Customer Name" Editor="inline" DisplayField="Yes" Type="string" ReadOnly="No" FormatEditor="inline" RememberLastValue="Yes" Subtype="inline">Novah</MF_1>
 <MF_2 LocalizedTagName="Contact Name" ValueIsRequired="Yes" Format="" Tooltip="Contact Name" Editor="inline" DisplayField="Yes" Type="string" ReadOnly="No" FormatEditor="inline" RememberLastValue="Yes" Subtype="inline">Damien</MF_2>

I need to capture each of the "MF_*" fields in this file in particular the LocalisedTagName attribute in each field, this will be set to the header of the CSV file while "MF_* " data is capture as a value for said LocalisedTagName.

IF someone can help point me in the right direction.

Thanks for your help
25-08-16.csv
demoWFconvert.php
Job455.xml
0
Comment
Question by:Alan Keane
  • 4
4 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41770622
Do you have any control over the format of the XML document?  It seems awkward to me and might benefit from more straightforward formatting.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points (awarded by participants)
ID: 41770661
This expert suggested creating a Gigs project.
Here's an example to help you get started.  Please see:
https://iconoun.com/demo/temp_alan_keane.php
<?php // demo/temp_alan_keane.php
/**
 * https://www.experts-exchange.com/questions/28965640/PHP-converting-XML-to-CSV-capturing-atributes-on-unique-field-tags.html
 */
error_reporting(E_ALL);
echo '<pre>';


// TEST DATA FROM THE POST AT E-E
$xml = <<<EOD
<?xml version='1.0' encoding='UTF-8'?>
<ValueDescription Type="metadatafields">
 <MF_1 LocalizedTagName="Customer Name" ValueIsRequired="Yes" Format="" Tooltip="Customer Name" Editor="inline" DisplayField="Yes" Type="string" ReadOnly="No" FormatEditor="inline" RememberLastValue="Yes" Subtype="inline">Novah</MF_1>
 <MF_2 LocalizedTagName="Contact Name" ValueIsRequired="Yes" Format="" Tooltip="Contact Name" Editor="inline" DisplayField="Yes" Type="string" ReadOnly="No" FormatEditor="inline" RememberLastValue="Yes" Subtype="inline">Damien</MF_2>
 <MF_20 LocalizedTagName="Customer Proof Required" ValueIsRequired="Yes" Tooltip="" Editor="inline" DisplayField="Yes" Type="bool" ReadOnly="No" RememberLastValue="Yes" Subtype="inline">Yes</MF_20>
 <MF_3 LocalizedTagName="Contact Email Address" ValueIsRequired="Yes" Format="" Dependencyvalue="Yes" Dependency="MF_20" Tooltip="Contact Email Address" Editor="inline" DisplayField="Yes" Type="string" ReadOnly="No" FormatEditor="inline" RememberLastValue="Yes" Subtype="inline" DependencyValueSubType="inline" DependencyCondition="Equals">thomas.com</MF_3>
 <MF_5 LocalizedTagName="Job Reference" ValueIsRequired="Yes" Format="" Tooltip="Job Reference" Editor="inline" DisplayField="Yes" Type="string" ReadOnly="No" FormatEditor="inline" RememberLastValue="Yes" Subtype="inline">41613</MF_5>
 <MF_13 LocalizedTagName="Customer PO Number" ValueIsRequired="Yes" Format="" Tooltip="Customer PO Number" Editor="inline" DisplayField="Yes" Type="string" ReadOnly="No" FormatEditor="inline" RememberLastValue="No" Subtype="inline">Damien</MF_13>
 <MF_17 LocalizedTagName="Job Type" ValueIsRequired="Yes" Tooltip="Job Type" Editor="inline" DisplayField="Yes" Type="enum:Van Branding;Truck Branding;Vinyl Print;PVC Banner;PVC Mesh;Roll UP &amp; Pull UP;Wall Graphic;Other;Signage;" ReadOnly="No" RememberLastValue="No" IgnoreDuplicates="No" Subtype="inline">PVC Banner</MF_17>
 <MF_4 LocalizedTagName="Required Delivery Date" ValueIsRequired="Yes" Tooltip="" Editor="inline" DisplayField="Yes" Type="date" ReadOnly="No" RememberLastValue="No" Subtype="inline">2016-08-23</MF_4>
 <MF_6 LocalizedTagName="Job Description" ValueIsRequired="Yes" Format="" Tooltip="Job Description" Editor="inline" DisplayField="Yes" Type="string" ReadOnly="No" FormatEditor="inline" RememberLastValue="No" Subtype="inline">Item_A</MF_6>
 <MF_7 LocalizedTagName="Quantity Required" ValueIsRequired="No" Format="" Tooltip="Quantity Required" Editor="inline" DisplayField="Yes" Type="string" ReadOnly="No" FormatEditor="inline" RememberLastValue="No" Subtype="inline">1</MF_7>
 <MF_14 LocalizedTagName="Job Trim Size Width (in cm)" ValueIsRequired="Yes" Format="" Tooltip="Job Trim Size Width (in cm)" Editor="inline" DisplayField="Yes" Type="string" ReadOnly="No" FormatEditor="inline" RememberLastValue="No" Subtype="inline">298.5</MF_14>
 <MF_15 LocalizedTagName="Job Trim Size Height (in cm)" ValueIsRequired="Yes" Format="" Tooltip="Job Trim Size Height (in cm)" Editor="inline" DisplayField="Yes" Type="string" ReadOnly="No" FormatEditor="inline" RememberLastValue="No" Subtype="inline">222</MF_15>
 <MF_16 LocalizedTagName="Sales Contact" ValueIsRequired="Yes" Tooltip="Sales Contact" Editor="inline" DisplayField="Yes" Type="enum:----- Select Sales Agent-----;;------------------;Sales 1;Sales 2;Sales 3" ReadOnly="No" RememberLastValue="Yes" IgnoreDuplicates="Yes" Subtype="inline">Sales 1</MF_16>
 <MF_18 LocalizedTagName="Job Value" ValueIsRequired="No" Format="" Tooltip="Job Value" Editor="inline" DisplayField="Yes" Type="string" ReadOnly="No" FormatEditor="inline" RememberLastValue="No" Subtype="inline">120</MF_18>
 <MF_11 LocalizedTagName="Choose Printer" ValueIsRequired="No" Tooltip="Choose Printer" Editor="inline" DisplayField="Yes" Type="enum:----PLEASE SELECT PRINTER---;Fresco;Jeti;Latex 3100;Mimaki;Roland;Tempo;Konsberg;Plotter" ReadOnly="No" RememberLastValue="No" IgnoreDuplicates="No" Subtype="inline">Jeti</MF_11>
 <MF_12 LocalizedTagName="Choose Media" ValueIsRequired="Yes" Tooltip="Choose Media" Editor="inline" DisplayField="Yes" Type="enum:----PLEASE SELECT MEDIA---;3M IJ180;Arlon 6000XRP;Arlon 4500 GT;Mactac 5929 PM;Ritrama Polymeric Hi-Grip;Arlon 8000;Mactac WW300;Ri-Jet 100 (04239) White Gloss Vinyl with blackout permanent adhesive;Ri-Jet 100 (05049) White Gloss Vinyl with permanent adhesive;Ri-Jet 100 (04236) White Gloss Vinyl with blackout removable adhesive;Ri-Jet 100 (02659) White Gloss Vinyl with removable adhesive ;Bsmart PVC 510g;Bsmart Mesh 270g ;------- RIGID MEDIA--------;Foamex 3mm;Foamex 5mm;Foamex 10mm;Foamex 19mm;Corriboard 3mm;Corriboard 5mm;Corriboard 10mm;Corriboard 19mm;Diabond;Display Board" ReadOnly="No" RememberLastValue="No" IgnoreDuplicates="No" Subtype="inline">Bsmart PVC 510g</MF_12>
 <MF_22 LocalizedTagName="Edges Wrapped Y/N" ValueIsRequired="No" Dependencyvalue="Foamex" Dependency="MF_12" Tooltip="Edges Wrapped Y/N" Editor="inline" DisplayField="Yes" Type="bool" ReadOnly="No" RememberLastValue="No" Subtype="inline" DependencyValueSubType="inline" DependencyCondition="Contains">No</MF_22>
 <MF_8 LocalizedTagName="Choose Laminate" ValueIsRequired="No" Tooltip="Choose Laminate" Editor="inline" DisplayField="Yes" Type="enum:----PLEASE SELECT LAMINATE---;3M 8548G;Arlon 3210;Arlon 3421;Polycover 999;Ri-2067 (03526)  Gloss laminate;Ri-2977 (03528)  Matt Laminate;None" ReadOnly="No" RememberLastValue="No" IgnoreDuplicates="No" Subtype="inline">None</MF_8>
 <MF_9 LocalizedTagName="Finishing Instructions" ValueIsRequired="No" Tooltip="Finishing Instructions" Editor="inline" DisplayField="Yes" Type="enum:TRIM TO SIZE;Mount OnSubstrate;Van Application;Wall Graphic;Truck Application;Hemm &amp; Eyelet " ReadOnly="No" RememberLastValue="No" IgnoreDuplicates="No" Subtype="inline">Hemm &amp; Eyelet</MF_9>
 <MF_10 LocalizedTagName="Choose Substrate" ValueIsRequired="Yes" Format="" Dependencyvalue="Mount OnSubstrate" Dependency="MF_9" Tooltip="Choose Substrate" Editor="inline" DisplayField="Yes" Type="string" ReadOnly="No" FormatEditor="inline" RememberLastValue="No" Subtype="inline" DependencyValueSubType="inline" DependencyCondition="Equals"/>
 <MF_19 LocalizedTagName="Fitting Required" ValueIsRequired="No" Tooltip="Fitting Required" Editor="inline" DisplayField="Yes" Type="bool" ReadOnly="No" RememberLastValue="No" Subtype="inline">No</MF_19>
 <MF_21 LocalizedTagName="Multi Item Job" ValueIsRequired="Yes" Tooltip="Multi Item Job" Editor="inline" DisplayField="Yes" Type="bool" ReadOnly="No" RememberLastValue="No" Subtype="inline">No</MF_21>
</ValueDescription>
EOD;

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// SHOW HOW TO FIND SOME DATA IN ELEMENTS OF THE OBJECT
$ele = $obj->MF_1;
$ltn = $ele->attributes()->LocalizedTagName;
$txt = (string)$ele;
echo PHP_EOL . "$ltn: <b>$txt</b>";

$ele = $obj->MF_3;
$ltn = $ele->attributes()->LocalizedTagName;
$txt = (string)$ele;
echo PHP_EOL . "$ltn: <b>$txt</b>";

$ele = $obj->MF_4;
$ltn = $ele->attributes()->LocalizedTagName;
$txt = (string)$ele;
echo PHP_EOL . "$ltn: <b>$txt</b>";

echo PHP_EOL;

// ITERATE OVER THE OBJECT TO SEE ALL THE NODES
foreach ($obj as $tag => $ele)
{
    print_r($tag);
    echo ': ';
    print_r($ele);
    echo PHP_EOL;
}

Open in new window

The general design for writing a single  row of a CSV file is to create an array and use fputcsv().  

We usually use a collection of "sub-arrays", where each "sub-array" represents a single row of the CSV file.  The collection is gathered into a main array.  We use an iterator like foreach() to access the main array, with each iteration getting us the next sub-array.  Each sub-array is fed to fputcsv().

If you're new to PHP this article can help you find good learning resources.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html

If you think you could use some hands-on help with this project, you might consider E-E Gigs.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points (awarded by participants)
ID: 41770679
And, this might help with the creation of the CSV
<?php // demo/temp_alan_keane.php
/**
 * https://www.experts-exchange.com/questions/28965640/PHP-converting-XML-to-CSV-capturing-atributes-on-unique-field-tags.html
 */
error_reporting(E_ALL);
echo '<pre>';

// WHERE TO PUT THE INFORMATION
$csv = 'temp_alan_keane.csv';

// TEST DATA FROM THE POST AT E-E
$url = 'https://filedb.experts-exchange.com/incoming/2016/08_w35/1113479/Job455.xml';
$xml = file_get_contents($url);

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// COLLECT THE HEADERS AND THE DATA SET
$headers = [];
$dataset = [];
foreach ($obj as $tag => $ele)
{
    $headers[] = (string)$ele->attributes()->LocalizedTagName;
    $dataset[] = (string)$ele;
}

// OPEN THE OUTPUT FILE
$fpw = fopen($csv, 'w');
if (!$fpw) trigger_error("Unable to open $csv", E_USER_ERROR);

// WRITE THE OUTPUT
if (!fputcsv($fpw, $headers)) trigger_error("Unable to write headers to $csv", E_USER_ERROR);
if (!fputcsv($fpw, $dataset)) trigger_error("Unable to write dataset to $csv", E_USER_ERROR);

// ALL DONE
fclose($fpw);
echo PHP_EOL . '<a href="' . $csv . '">See the CSV</a>';

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41795690
Tested and working code examples provided.  Author abandoned.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now