On the moment I’m working with a project what gets data from an XML file and store it into a database. It work fine with SimpleXML but it’s all kind of hardcoded. If the XML format change I have to change the code. I feel that is must be possible to make this more dynamic. Like a template how to interpretate the XML file. Is there something in Yii that can help me with this?
Simplefied example:
Feed 1
<products>
<product>
<name>product one</name>
<price>12.50</price>
<properties>
<prop name="height"><value>100</value></prop>
<prop name="width"><value>200</value></prop>
<prop name="color"><value>green</value></prop>
</properties>
</product>
<product>
<name>product two</name>
<price>33.50</price>
<properties>
<prop name="height"><value>240</value></prop>
<prop name="width"><value>500</value></prop>
<prop name="color"><value>red</value></prop>
</properties>
</product>
</products>
I can approach them like:
$products = simplexml_load_file("feed.xml");
foreach ($products as $key => $value)
{
echo $value->name;
echo $value->properties->property[0]->attributes()->name;
echo $value->properties->property[0]->value;
}
But i got another feed who uses other tag names.
<productah>
<productio>
<name>product one</name>
<price>12.50</price>
<propertiras>
<propertira><name>heito</name><value>100</value></propertira>
<propertira><name>colura</name><value>Ruranjo</value></propertira>
</propertiras>
</productio>
</productah>
How can I make this dynamic? I guess I’m not the first one to ran into this issue, but again I don’t know the right keywords to search for a solution. Do you know an Yii extention or another solution for this?
Tnx!