user3824329
user3824329

Reputation: 95

Get specific results from XML file

Hello I am trying to extract some data out of an XML file. But it is not working properly with my solution.

The XML file:

<?xml version='1.0' encoding='UTF-8'?>
<m4n xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="datafeeds/ext/m4n-datafeeds.xsd">
<dataHeader>
<exportType>stream</exportType>
<exportId>2000433</exportId>
<rows>40434</rows>
<lastChecked>2014-07-02 10:12:24.41</lastChecked>
<lastUpdated>2014-07-02 10:13:36.09</lastUpdated>
<parserLocale>nl_NL</parserLocale>
<streamCurrency>EUR</streamCurrency>
<name>VALUE</name>
<description><![CDATA[]]></description>
</dataHeader>

<data>
<record>
    <recordHash><![CDATA[-2147395708]]></recordHash>
    <column name="url"><![CDATA[VALUE]]></column>
    <column name="title"><![CDATA[VALUE]]></column>
    <column name="description"><![CDATA[VALUE]]></column>
    <column name="offerid"><![CDATA[VALUE]]></column>
    <column name="image"><![CDATA[VALUE]]></column>
    <column name="price"><![CDATA[VALUE]]></column>
    <column name="category"><![CDATA[VALUE]]></column>
    <column name="subcategory"><![CDATA[VALUE]]></column>
    <column name="stock" />
    <column name="timetoship"><![CDATA[VALUE]]></column>
    <column name="ean"><![CDATA[VALUE]]></column>
    <column name="price_shipping"><![CDATA[VALUE]]></column>
    <column name="price_old" />
    <column name="vendor"><![CDATA[VALUE]]></column>
    <column name="category_path" />
    <column name="description2"><![CDATA[VALUE]]></column>
    <column name="largeimage"><![CDATA[VALUE]]></column>
    <column name="author"><![CDATA[VALUE]]></column>
    <column name="column0"><![CDATA[VALUE]]></column>
    <column name="column1"><![CDATA[VALUE]]></column>
    <column name="column2"><![CDATA[VALUE]]></column>
    <column name="column3"><![CDATA[VALUE]]></column>
    <column name="column4"><![CDATA[VALUE]]></column>
    <column name="column5"><![CDATA[VALUE]]></column>
    <column name="column6"><![CDATA[VALUE]]></column>
    <column name="column7"><![CDATA[VALUE]]></column>
    <column name="column8"><![CDATA[VALUE]]></column>
    <column name="column9"><![CDATA[VALUE]]></column>
    <column name="sku"><![CDATA[VALUE]]></column>
    <column name="size"><![CDATA[VALUE]]></column>
    <column name="height"><![CDATA[VALUE]]></column>
</record>
</data></m4n>

What I want to extract is the following: url = VALUE title = VALUE and so on, so basicly i want the name of the column and the value of the column together.

I tried the following methods:

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'url');
$data = curl_exec($ch);
curl_close($ch);

$xml = new SimpleXMLElement($data);

foreach($xml->data as $data)
{
    foreach($data->record as $record)
    {
        foreach($record->column as $column)
        {
            foreach($column->attributes() as $key => $value)
            {
                echo $key." - ".$value."<br/>";
            }
        }
    }
}

Could someone please take a look

Upvotes: 1

Views: 160

Answers (1)

Mihai Iorga
Mihai Iorga

Reputation: 39704

Ok, you need to extract the CDATA value like this:

foreach($xml->data as $data)
{
    foreach($data->record as $record)
    {
        foreach($record->column as $column)
        {
          echo $column->attributes()->{0}." - ".(string)$column."<br/>"; // fetch first attribute and the CDATA value

        }
    }
}

Convert $column to string.

CodePAD

Upvotes: 1

Related Questions