Bonjour,
Voilà, je rencontre un petit problème formatage avec simpleXML .
J'ai créé une fonction qui me permet de créer un xml
<?php
$itemsXml = new SimpleXMLElement("<items></items>");
// Add child element the SimpleXML element
$itemXml = $itemsXml->addChild('item');
// Add attribute to the SimpleXML element
$itemXml->addAttribute('id', 1);
$itemXml->addAttribute('type', 'simple');
$mobileXml = $itemXml->addChild('Mobiles');
// Suppose we have the following array of data
$items = array(array('name'=>'nokia','weight'=>'2'),
array('name'=>'samsung','weight'=>'5'),
array('name'=>'sony','weight'=>'3'));
foreach ($items as $item)
{
// XML PART
// Please look carefully on the variable names. They are important for using addChild method.
$brandXml = $mobileXml->addChild('Brand');
$nameXml = $brandXml->addChild('Name',$item['name']);
$weightXml = $brandXml->addChild('Weight',$item['weight']);
}
// FOR PROPER FORMATTING XML
// Create a new DOMDocument object
$doc = new DOMDocument('1.0');
// add spaces, new lines and make the XML more readable format
$doc->formatOutput = true;
// Get a DOMElement object from a SimpleXMLElement object
$domnode = dom_import_simplexml($itemsXml);
$domnode->preserveWhiteSpace = false;
// Import node into current document
$domnode = $doc->importNode($domnode, true);
// Add new child at the end of the children
$domnode = $doc->appendChild($domnode);
// Dump the internal XML tree back into a string
$saveXml = $doc->saveXML();
// SET HEADER TO OUTPUT DATA
echo $saveXml;
die();
?>
que j'appelle dans un autre script, que j'ai se résulta :
<?xml version="1.0"?>
<items>
<item id="1" type="simple">
<Mobiles>
<Brand>
<Name>nokia</Name>
<Weight>2</Weight>
</Brand>
<Brand>
<Name>samsung</Name>
<Weight>5</Weight>
</Brand>
<Brand>
<Name>sony</Name>
<Weight>3</Weight>
</Brand>
</Mobiles>
</item>
</items>
j'ai un décalage sur '<?xml version="1.0"?> '
Pouvez-vous m'aider à résoudre ce problème.
Merci d'avance.