2013-02-15 35 views
-2

我需要我的确认表单中的平均值和精益解决方案。我从服务器XML响应中检索联系人电子邮件地址。也就是说如下:xml新增功能:将儿童数据导入到php变量中

<users> 
<User LoginName="test1" Owner="" Alias="" UserType="PAID" ClientType="OBM" 
Quota="10737418240" Timezone="GMT+08:00 (CST)" Language="en" DataFile="1" 
DataSize="1536" RetainFile="0" RetainSize="0" EnableMSSQL="Y" EnableMSExchange="Y" 
EnableOracle="Y" EnableLotusNotes="Y" EnableLotusDomino="Y" EnableMySQL="Y" 
EnableInFileDelta="Y" EnableShadowCopy="Y" EnableExchangeMailbox="N" 
ExchangeMailboxQuota="0" EnableNASClient="Y" EnableDeltaMerge="Y" EnableMsVm="N" 
MsVmQuota="0" EnableVMware="N" VMwareQuota="0" Bandwidth="0" Notes="" 
Status="ENABLE" RegistrationDate="1302687743242" SuspendPaidUser="N" 
SuspendPaidUserDate="20140503" LastBackupDate="1302699594652" EnableCDP="Y" 
EnableShadowProtectBareMetal="Y" EnableWinServer2008BareMetal="Y" 
Hostname="123.abc.com"> 
<Contact Name=""Email="[email protected]"/> 
</user> 
… 
</users> 

我就认为我有以下结果:

object(SimpleXMLElement)#7 (1) { [0]=> string(6) "Company" } 
object(SimpleXMLElement)#8 (1) { [0]=> string(26) "[email protected]" } 

现在我需要那些到变量。我似乎无法实现这一点。我的代码到这里:

$request = "http://$SERVER/obs/api/GetUser.do? 
    SysUser=$SYSUSER&SysPwd=$SYSPWD&LoginName=$logonname"; 
    // Execute the API Call and place the XML output in an Array variable 
    $response = simplexml_load_file($request); 
    // Retrieve the LoginName attribute from the Array 
    foreach($response->children() as $child) { 
     foreach($child->attributes() as $data) { 
      echo var_dump($data); 
     } 
    } 

任何新手帮助将appriciated。谢谢

弗兰克

+0

你显然已经得到了的SimpleXMLElement持有的电子邮件,那么究竟是什么问题是什么?如何从中获取电子邮件字符串? $ email =(string)$ emailElement; – Gordon 2013-02-15 14:24:49

+0

'echo var_dump'是多余的。 var_dump默认已经输出。 – 2013-02-15 14:30:59

回答

0

这里的读取姓名和电子邮件您的XML的一个简单的例子:

$response = simplexml_load_file($request); 

foreach($response->User as $user) 
{ 
    foreach($user->Contact as $contact) 
    { 
     $name = $contact->attributes()->Name; 
     $email = $contact->attributes()->Email; 

     echo $name; 
     echo $email; 
    } 
} 

Demo

+0

回显为空。我的回复包含: – 2013-02-15 15:20:54

+0

@FrankSchuurman在您的XML中检查框。您打开的用户标记是'User',但是关闭是'user'。您需要在PHP代码中使用真正的XML中的任何一种外壳 - 请相应地进行更改。除此之外,上面的代码应该可以工作(参见演示)。 – MrCode 2013-02-15 15:53:17