Using Simplexml to Create an Xml Object from Scratch

Using SimpleXML to create an XML object from scratch

Sure you can. Eg.

<?php
$newsXML = new SimpleXMLElement("<news></news>");
$newsXML->addAttribute('newsPagePrefix', 'value goes here');
$newsIntro = $newsXML->addChild('content');
$newsIntro->addAttribute('type', 'latest');
Header('Content-type: text/xml');
echo $newsXML->asXML();
?>

Output

<?xml version="1.0"?>
<news newsPagePrefix="value goes here">
<content type="latest"/>
</news>

Have fun.

Loop through an XML object with SimpleXML

SimpleXML doesn't have a getElementsByTagName() method (DOMDocument does).

In SimpleXML, the object (e.g $xml) is treated as the root element. So you can loop through the product items like so:

$xml = simplexml_load_string($xmlString);
foreach($xml->products->item as $item)
{
echo (string)$item->product_id;
echo (string)$item->model;
}

Example of building a devices associative array:

$devices = array();

$xml = simplexml_load_string($xmlString);
foreach($xml->products->item as $item)
{
$device = array();

foreach($item as $key => $value)
{
$device[(string)$key] = (string)$value;
}

$devices[] = $device;
}

print_r($devices);

Outputs:

Array
(
[0] => Array
(
[product_id] => 32417
[manufacturer] => Alcatel
[model] => Sparq 2
[deeplink] => http://www.mysite.com/sc_offer?gid=32417
[thumbnail_URL] => http://www.mysite.com/images/devices/thumbs/Alcatel-Sparq-II.jpg
[image_URL] => http://www.mysite.com/images/devices/Alcatel-Sparq-II.jpg
[price_not_working] => 0.00
[price_poor] => 0.00
[price_fair] => 20.00
[price_good] => 25.00
[price_perfect] => 25.00
[price_new] => 25.00
[battery_new] => 1.00
[battery_perfect] => 1.00
[battery_good] => 1.00
[battery_fair] => 1.00
[battery_poor] => 0.00
[charger_new] => 1.00
[charger_perfect] => 1.00
[charger_good] => 1.00
[charger_fair] => 1.00
[charger_poor] => 0.00
[packaging_new] => 1.00
[packaging_perfect] => 1.00
[packaging_good] => 1.00
[packaging_fair] => 1.00
[packaging_poor] => 0.00
)

[1] => Array
(
[product_id] => 31303
[manufacturer] => Apple
[model] => iPhone 3G 8gb
[deeplink] => http://www.mysite.com/sc_offer?gid=31303
[thumbnail_URL] => http://www.mysite.com/images/devices/thumbs/iPhone 8 3G.jpg
[image_URL] => http://www.mysite.com/images/devices/iPhone 8 3G.jpg
[price_not_working] => 0.00
[price_poor] => 0.00
[price_fair] => 7.00
[price_good] => 2.00
[price_perfect] => 2.00
[price_new] => 2.00
[battery_new] => 1.00
[battery_perfect] => 1.00
[battery_good] => 1.00
[battery_fair] => 1.00
[battery_poor] => 0.00
[charger_new] => 1.00
[charger_perfect] => 1.00
[charger_good] => 1.00
[charger_fair] => 1.00
[charger_poor] => 0.00
[packaging_new] => 1.00
[packaging_perfect] => 1.00
[packaging_good] => 1.00
[packaging_fair] => 1.00
[packaging_poor] => 0.00
)

)

Insert XML structure as a child of another XML Element using PHP

You can't with SimpleXML, but if you really need manipulate your DOM or create it from scratch consider DOMDocument.

$xmlObject   = new \DOMDocument();
$xml = $xmlObject->createElement('root');
$xml_a = $xmlObject->createElement('parent');
$xml_b = $xmlObject->createElement('child');
$xml_b->setAttribute('attribute','value');
$xml_b->appendChild(new \DOMElement('item', 'itemValue'));
$xml_a->appendChild($xml_b);
$xml->appendChild($xml_a);
$xmlObject->appendChild($xml);
echo $xmlObject->saveXML();

Using SimpleXMLElement when loading from file

simplexml_load_file Interprets an XML file into an xml object thus $xml is itself a xml object, to access it`s elements just use $xml object properties no need to create the new object $elm

simplexml_load_file returns an object of class SimpleXMLElement with properties containing the data held within the XML document, or FALSE on failure.

http://php.net/manual/en/function.simplexml-load-file.php



Related Topics



Leave a reply



Submit