2017-08-25 50 views
0

解析在我的控制,我讀DB數據。 (其中AlarmDeatils被存儲爲XML內容如:AlarmDeatils列包含XML是不是在PHP

<SiteAlarmDetails> 
    <AlertId>89637</AlertId> 
    <SiteCode>20157498</SiteCode> 
    <SiteName>newport</SiteName> 
    <TankNumber>4</TankNumber> 
    <DispenserNumbedr>3</DispenserNumbedr> 
    <HoseNumber>3</HoseNumber> 
    <GradeId>11</GradeId> 
    <GradeName>PULP98</GradeName> 
    <AlarmUTCDateTime>2015-10-08T12:00:00</AlarmUTCDateTime> 
    <AlarmClearedUTCDateTime>2015-10-08T22:00:00</AlarmClearedUTCDateTime> 
    <UTCTimeZoneName>GMT Standard Time</UTCTimeZoneName> 
    <AlarmVolume>0</AlarmVolume> 
    <AlarmLevel>0</AlarmLevel> 
    <TankCapacity>0</TankCapacity> 
    <TankCapacityPercent>0</TankCapacityPercent> 
    <TankOverfill>0</TankOverfill> 
    <TankUllage>0</TankUllage> 
    <ProductLoss>0</ProductLoss> 
    <HoursElapsed>10</HoursElapsed> 
    <WaterLevel>0</WaterLevel> 
    <AvgSalesPerDay>0</AvgSalesPerDay> 
    <DaysToStockOut>0</DaysToStockOut> 
    <InvalidDataCount>0</InvalidDataCount> 
    <ValidDataCount>0</ValidDataCount> 
    <ZeroVolumeCount>0</ZeroVolumeCount> 
    <ZeroProductLevelCount>0</ZeroProductLevelCount> 
    <ZeroTotaliserAmountCount>0</ZeroTotaliserAmountCount> 
</SiteAlarmDetails> 

,我讀了該行中我的控制器等;

$tableAlarm = \DB::table('Alarm') 
       ->where('Alarm.AlarmId', '=', $id)->first(); 

,當我使用

var_dump($tableAlarm); 

我得到

object(stdClass)#241 (10) { ["AlarmId"]=> string(6) "245039" ["MessageNotificationId"]=> string(6) "219078" ["CompanyId"]=> string(2) "19" ["CompanyCode"]=> string(7) "MCCOLLS" ["AlertTypeId"]=> string(2) "23" ["AlarmDetails"]=> string(1408) "979381320106510Eyemouth Service Station2017-07-23T21:26:499999-12-31T23:59:59.9999999GMT Standard Time00000005000000000001-01-01T00:00:000001-01-01T00:00:000001-01-01T00:00:000000001-01-01T00:00:00" ["AlertProcessStateId"]=> string(1) "2" ["UTCDateTimeInserted"]=> string(27) "2017-07-24 02:15:36.9300000" ["UTCDateTimeUpdated"]=> string(27) "2017-07-24 02:15:36.9300000" ["RowDataVersion"]=> string(16) "00000000117D854B" } 

我試着解析AlarmDeatils柱等;

$alertXml = simplexml_load_string($tableAlarm->AlarmDetails); 
echo $alertXml;//Nothing printed 

,但我沒有得到任何東西:(我試圖來處理XML等;。

foreach($alertXml->children() as $alerts) 
{ 
    print_r($alerts->AlertId);// **getting SimpleXMLElement Object()** 
    echo $alerts->AlertId;//**Nothing printed** 
} 
+2

'simplexml_load_string'返回一個你不能回顯的對象,試試'print_r'。[simplexml_load_string]( http://us2.php.net/manual/en/function.simplexml-load-string.php)在這個'simplexml_load_string'你必須加載一個字符串,而不是一個'object' –

+0

和預期的產出? –

+0

你能分享你'XML' –

回答

1

希望這將有助於試試這個簡單的一個

Try this code snippet here

<?php 

$xmlString=<<<XML 
<SiteAlarmDetails> 
    <AlertId>89637</AlertId> 
    <SiteCode>20157498</SiteCode> 
    <SiteName>newport</SiteName> 
    <TankNumber>4</TankNumber> 
    <DispenserNumbedr>3</DispenserNumbedr> 
    <HoseNumber>3</HoseNumber> 
    <GradeId>11</GradeId> 
    <GradeName>PULP98</GradeName> 
    <AlarmUTCDateTime>2015-10-08T12:00:00</AlarmUTCDateTime> 
    <AlarmClearedUTCDateTime>2015-10-08T22:00:00</AlarmClearedUTCDateTime> 
    <UTCTimeZoneName>GMT Standard Time</UTCTimeZoneName> 
    <AlarmVolume>0</AlarmVolume> 
    <AlarmLevel>0</AlarmLevel> 
    <TankCapacity>0</TankCapacity> 
    <TankCapacityPercent>0</TankCapacityPercent> 
    <TankOverfill>0</TankOverfill> 
    <TankUllage>0</TankUllage> 
    <ProductLoss>0</ProductLoss> 
    <HoursElapsed>10</HoursElapsed> 
    <WaterLevel>0</WaterLevel> 
    <AvgSalesPerDay>0</AvgSalesPerDay> 
    <DaysToStockOut>0</DaysToStockOut> 
    <InvalidDataCount>0</InvalidDataCount> 
    <ValidDataCount>0</ValidDataCount> 
    <ZeroVolumeCount>0</ZeroVolumeCount> 
    <ZeroProductLevelCount>0</ZeroProductLevelCount> 
    <ZeroTotaliserAmountCount>0</ZeroTotaliserAmountCount> 
</SiteAlarmDetails> 
XML; 

$xml=simplexml_load_string($xmlString); 
echo (string)$xml->AlertId;//type-casted to string 
echo PHP_EOL; 
echo (string)$xml->SiteCode; 
echo PHP_EOL; 
echo (string)$xml->SiteName;