PHP XML Parse Issue

I’m new to using XML with PHP so have become stuck.

I’m converting a XML file into a CSV file for stock purposes and the XML is below which I need to create a row for in the CSV.

<product id="137470">
        <productName>Male x Male BSPP x BSPP</productName>
        <productCategory>Burnett and Hillman|Hydraulic Adaptors</productCategory>
        <productImageUrl>http://www.beaumanor.com/ShowProductImage.aspx?id=137470&amp;width=200</productImageUrl>
        <productDescription><![CDATA[<ul><li>Burnett & Hillman</li><li>Hydraulic Adaptor</li></ul>]]></productDescription>
        <variants>
            <variant partnumber="BUH-00001" price="1.2000" stock="20">
                <attributes>
                    <attribute name="BSPP Male (a)">1/8"</attribute>
                    <attribute name="BSPP Male (b)">1/8"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00002" price="1.3800" stock="50">
                <attributes>
                    <attribute name="BSPP Male (a)">1/8"</attribute>
                    <attribute name="BSPP Male (b)">1/4"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00003" price="1.6400" stock="1">
                <attributes>
                    <attribute name="BSPP Male (a)">1/8"</attribute>
                    <attribute name="BSPP Male (b)">3/8"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00004" price="2.2200" stock="5">
                <attributes>
                    <attribute name="BSPP Male (a)">1/8"</attribute>
                    <attribute name="BSPP Male (b)">1/2"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00005" price="1.0700" stock="200">
                <attributes>
                    <attribute name="BSPP Male (a)">1/4"</attribute>
                    <attribute name="BSPP Male (b)">1/4"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00006" price="1.2900" stock="98">
                <attributes>
                    <attribute name="BSPP Male (a)">1/4"</attribute>
                    <attribute name="BSPP Male (b)">3/8"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00007" price="1.7800" stock="50">
                <attributes>
                    <attribute name="BSPP Male (a)">1/4"</attribute>
                    <attribute name="BSPP Male (b)">1/2"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00008" price="3.3800" stock="5">
                <attributes>
                    <attribute name="BSPP Male (a)">1/4"</attribute>
                    <attribute name="BSPP Male (b)">5/8"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00014" price="1.2900" stock="200">
                <attributes>
                    <attribute name="BSPP Male (a)">3/8"</attribute>
                    <attribute name="BSPP Male (b)">3/8"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00015" price="1.5600" stock="98">
                <attributes>
                    <attribute name="BSPP Male (a)">3/8"</attribute>
                    <attribute name="BSPP Male (b)">1/2"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00016" price="2.4900" stock="5">
                <attributes>
                    <attribute name="BSPP Male (a)">3/8"</attribute>
                    <attribute name="BSPP Male (b)">5/8"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00017" price="3.2000" stock="10">
                <attributes>
                    <attribute name="BSPP Male (a)">3/8"</attribute>
                    <attribute name="BSPP Male (b)">3/4"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00021" price="1.5600" stock="100">
                <attributes>
                    <attribute name="BSPP Male (a)">1/2"</attribute>
                    <attribute name="BSPP Male (b)">1/2"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00022" price="2.3100" stock="10">
                <attributes>
                    <attribute name="BSPP Male (a)">1/2"</attribute>
                    <attribute name="BSPP Male (b)">5/8"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00023" price="2.6700" stock="50">
                <attributes>
                    <attribute name="BSPP Male (a)">1/2"</attribute>
                    <attribute name="BSPP Male (b)">3/4"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00024" price="5.2400" stock="18">
                <attributes>
                    <attribute name="BSPP Male (a)">1/2"</attribute>
                    <attribute name="BSPP Male (b)">1"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00028" price="2.3100" stock="10">
                <attributes>
                    <attribute name="BSPP Male (a)">5/8"</attribute>
                    <attribute name="BSPP Male (b)">5/8"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00029" price="3.0200" stock="20">
                <attributes>
                    <attribute name="BSPP Male (a)">5/8"</attribute>
                    <attribute name="BSPP Male (b)">3/4"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00032" price="2.6700" stock="75">
                <attributes>
                    <attribute name="BSPP Male (a)">3/4"</attribute>
                    <attribute name="BSPP Male (b)">3/4"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00033" price="4.6700" stock="20">
                <attributes>
                    <attribute name="BSPP Male (a)">3/4"</attribute>
                    <attribute name="BSPP Male (b)">1"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00034" price="17.4200" stock="10">
                <attributes>
                    <attribute name="BSPP Male (a)">3/4"</attribute>
                    <attribute name="BSPP Male (b)">1-1/4"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00037" price="4.4400" stock="50">
                <attributes>
                    <attribute name="BSPP Male (a)">1"</attribute>
                    <attribute name="BSPP Male (b)">1"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00038" price="10.4000" stock="9">
                <attributes>
                    <attribute name="BSPP Male (a)">1"</attribute>
                    <attribute name="BSPP Male (b)">1-1/4"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00039" price="14.9800" stock="5">
                <attributes>
                    <attribute name="BSPP Male (a)">1"</attribute>
                    <attribute name="BSPP Male (b)">1-1/2"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00042" price="9.6000" stock="20">
                <attributes>
                    <attribute name="BSPP Male (a)">1-1/4"</attribute>
                    <attribute name="BSPP Male (b)">1-1/4"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00043" price="13.9100" stock="20">
                <attributes>
                    <attribute name="BSPP Male (a)">1-1/4"</attribute>
                    <attribute name="BSPP Male (b)">1-1/2"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00044" price="23.5100" stock="5">
                <attributes>
                    <attribute name="BSPP Male (a)">1-1/4"</attribute>
                    <attribute name="BSPP Male (b)">2"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00045" price="13.1100" stock="20">
                <attributes>
                    <attribute name="BSPP Male (a)">1-1/2"</attribute>
                    <attribute name="BSPP Male (b)">1-1/2"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00047" price="21.2900" stock="10">
                <attributes>
                    <attribute name="BSPP Male (a)">1-1/2"</attribute>
                    <attribute name="BSPP Male (b)">2"</attribute>
                </attributes>
            </variant>
            <variant partnumber="BUH-00050" price="21.2900" stock="20">
                <attributes>
                    <attribute name="BSPP Male (a)">2"</attribute>
                    <attribute name="BSPP Male (b)">2"</attribute>
                </attributes>
            </variant>
        </variants>
    </product>

I use the below code to create a very simple CSV file from the above but I have two issues.



function convertXMLtoCSV() {
	$filexml= $_SERVER['DOCUMENT_ROOT'].'/beaux-stock/incoming/stock.xml';
	if (file_exists($filexml)) {
		$xml = simplexml_load_file($filexml);
	$f = fopen($_SERVER['DOCUMENT_ROOT'].'/beaux-stock/incoming/stock.csv', 'w');
	foreach ($xml->product as $product) {
		fputcsv($f, get_object_vars($product),',','"');
	}
	fclose($f);
	}	
}



  1. It doesn’t take the product id and add it to the CSV as a column.
  2. The Variants child elements are not added to the CSV.

Could someone point me in the right direction as I can’t seem to locate an material online about it or give me any pointers?

Thanks

Well currently you’re just looping through each product and putting the tag information into each cell in the CSV file. If you want information like the ID, and child tags, then you need to program that in yourself.

PHP: SimpleXMLElement::attributes - Manual
PHP: SimpleXMLElement::children - Manual