Reputation:
Trying the following PHP export to XML in order to create a remote phonebook for a Yealink SIP-T46G. Managed to get so far but currently exporting a an XML format that isn't valid to use on the phones. Looking throughout this site, I have found similar questions but have not managed to get this working.
<?php
include './data/datalogin.php';
$sql = mysqli_query($con,"SELECT contact_id, name, phone, cell FROM contact ORDER BY name ASC");
$xml = "<root_contact>";
while($r = mysqli_fetch_array($sql))
{
$xml .= "<contact>";
$xml .= $display_name=$r["name"];
$xml .= $office_number=$r["phone"];
$xml .= $mobile_number=$r["cell"];
$xml .= "</contact>";
}
$xml .= "</root_contact>";
$sxe = new SimpleXMLElement($xml);
$dom = new DOMDocument('1,0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($sxe->asXML());
echo $dom->saveXML();
$dom->save('phonebookt47.xml');
?>
Output currently is in the format, which is just adding the phone number immediately after the display instead of as the desired format further below.
<contact>SampleCompany01666666666</contact>
Need it to to export in the below format
<contact display_name="SampleCompanyName" office_number="01666666666" mobile_number="07945444444"/>
I don't think it is far away but any help is appreciated.
Upvotes: 2
Views: 8359
Reputation: 4843
You need to put the attributes in the content
tags like so:
<?php
include './data/datalogin.php';
$sql = mysqli_query($con,"SELECT contact_id, name, phone, cell FROM contact ORDER BY name ASC");
$xml = "<root_contact>";
while($r = mysqli_fetch_array($sql))
{
$xml .= '<contact display_name="'.$r["name"].'" office_number="'.$r["phone"].'" mobile_number="'.$r["cell"].'" />';
}
$xml .= "</root_contact>";
$sxe = new SimpleXMLElement($xml);
$dom = new DOMDocument('1,0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($sxe->asXML());
echo $dom->saveXML();
$dom->save('phonebookt47.xml');
?>
Upvotes: 3
Reputation: 1103
Since you are writing your XML as a concatenated string, you just need to make sure to include all of the static text that goes into it. It should look something like this:
$xml .= '<contact display_name="' . $r["name"] . '"office_number="' . $r["cell"] . '" mobile_number="' . $r["cell"] . '"/>';
}
EDIT: Just dump your string strait to a file as text. This eliminates the need for a device to support the SimpleXMLElement function.
PERFORMANCE NOTE: I compressed it into a single line on purpose because each time you use $xml .=, it will reload your entire xml string to append the next parameter. This can significantly slow you down if using large XMLs.
If this is still too slow, you may even want to create a cashing variable that stores several entries at a time before writing them to your xml string.
Upvotes: 0
Reputation: 974
You can use SimpleXMLElement
with addChild()
and addAttribute()
:
<?php
$data = [
['id' => 1, 'name' => 'Contact One', 'phone' => '555555501', 'cell' => '999999901'],
['id' => 2, 'name' => 'Contact Two', 'phone' => '555555502', 'cell' => '999999902'],
['id' => 3, 'name' => 'Contact Three', 'phone' => '555555503', 'cell' => '999999903'],
];
$xml = new SimpleXMLElement('<root_contact/>');
foreach($data as $r) {
$contact = $xml->addChild('contact');
$contact->addAttribute('display_name', $r["name"]);
$contact->addAttribute('office_number', $r["phone"]);
$contact->addAttribute('mobile_number', $r["cell"]);
}
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
header('Content-Type: text/xml');
echo $dom->saveXML();
//$dom->save('phonebookt47.xml');
Upvotes: 0