PHP Array to XML

297 views Asked by At

I have created a multi-dimensional array using fgetcsv from a CSV file.

Using both DOMDocument and SimpleXML I am trying to create a XML file of the CSV document.

The array and XML variables are being passed to a function within the same class file. The XML document is being created without any issues, but no value is passing from the array into the XML. It does work it I use a static value opposed to passing a value from the array, also if I print_r the array the structure and values are all correct.

I have tried 'htmlspecialcharacters' and 'encode_UTF8' before passing the value into the XML.

An example of the code is below, product is the multi-dimensional array.

public function array_to_xml($product, &$xml) 
{

    foreach($product as $row)
    {       
        $element = $xml->createElement("Product");
        $xml->appendChild($element); 

        $element = $xml->createElement("ID", ($row[38]));
        $xml->appendChild($element); 
     }
 }

The problem is obviously with the array but I can't find the answer. Any help would be gratefully appreciated.

The output currently looks like (with not value in the ID element). Once it is working Product will have about 20 child elements.

<?xml version="1.0"?>
    <ProductList/>
        <Product>
            <ID/>
        </Product>
    </ProductList>

Example of $row when printed to screen:

Array ( [0] => [1] => [2] => 6/10/2016 [3] => [4] => [5] => 7.35 [6] => N [7] => N [8] => N [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 80 [15] => 0 [16] => 80 [17] => 0 [18] => 80 [19] => N [20] => N [21] => N [22] => N [23] => 236.50 [24] => 0.00 [25] => 4.86 [26] => AFG Home Loans - Alpha [27] => 100% Offset Lo Doc Fixed [28] => 100% Offset Lo Doc 4 Year Fixed Owner Occupied [29] => 250.00 [30] => [31] => 7.35 [32] => 0.00 [33] => 4.9 [34] => N [35] => 325.00 [36] => 48 [37] => 4.52 [38] => 1-1MX78TF [39] => N [40] => [41] => [42] => N [43] => N [44] => [45] => Y [46] => 0.00 [47] => 10,000.00 [48] => 2,000,000.00 [49] => Y [50] => 30 [51] => [52] => [53] => Y [54] => 0.00 ) 
1

There are 1 answers

1
Derek On

A couple things stand out. First, you have a syntax error on this line:

$element = $xml->createElement("ID", ($row[38])); (note the errant parentheses around $row[38]. The createElement method takes a String for its second parameter.

Second, you're not adding the ID to the product, but to the root XML. Fixing that, your code should look closer to this.

public function array_to_xml($product, &$xml) 
{
    foreach ($product as $row)
    {       
        $product= $xml->createElement("Product");
        $id = $xml->createElement("ID", $row[38]);

        $product->appendChild($id); 
        $xml->appendChild($product); 
     }
 }

If you need it as an attribute as @Barmar commented, you'd use the DOMElement->setAttribute() method, and it would look like:

public function array_to_xml($product, &$xml) 
{
    foreach ($product as $row)
    {       
        $product= $xml->createElement("Product");
        $product->setAttribute('ID', $row[38]); 

        $xml->appendChild($product); 
     }
 }