1

I’m setting up a PHP site which will gather information from a Dell iDRAC. I want to use the returned information to create a PHP object. The information returned from the first part of the script looks like this.

<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://www.w3.org/2003/05/soap-envelope" xmlnwsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlnwsen="http://schemas.xmlsoap.org/ws/2004/09/enumeration" xmlnn1="http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_SystemView" xmlnxsi="http://www.w3.org/2001/XMLSchema-instance">
  <Header>
    <To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</To>
    <Action>http://schemas.xmlsoap.org/ws/2004/09/enumeration/PullResponse</Action>
    <RelatesTo>uuid:e5ef952f-fb48-1b48-8003-06c7395d1500</RelatesTo>
    <MessageID>uuid:36a3a786-fb4c-1b4c-8012-fc140555dbe0</MessageID>
  </Header>
  <Body>
    <PullResponse>
      <Items>
        <DCIM_SystemView>
          <AssetTag/>
          <BIOSReleaseDate>01/20/2014</BIOSReleaseDate>
          <BIOSVersionString>2.1.2</BIOSVersionString>
          <BaseBoardChassisSlot>NA</BaseBoardChassisSlot>
          <BatteryRollupStatus>1</BatteryRollupStatus>
          <BladeGeometry>255</BladeGeometry>
          <BoardPartNumber>03015MA01</BoardPartNumber>
          <BoardSerialNumber>CN7475128I0205</BoardSerialNumber>
          <CPLDVersion>1.0.0</CPLDVersion>
          <CPURollupStatus>1</CPURollupStatus>
          <ChassisModel/>
          <ChassisName>Main System Chassis</ChassisName>
          <ChassisServiceTag>5P5KMW1</ChassisServiceTag>
          <ChassisSystemHeight>5</ChassisSystemHeight>
          <DeviceDescription>System</DeviceDescription>
          <ExpressServiceCode>12404926945</ExpressServiceCode>
          <FQDD>System.Embedded.1</FQDD>
          <FanRollupStatus>1</FanRollupStatus>
          <HostName/>
          <InstanceID>System.Embedded.1</InstanceID>
          <LastSystemInventoryTime>20140608040932.000000+000</LastSystemInventoryTime>
          <LastUpdateTime>20140522204842.000000+000</LastUpdateTime>
          <LicensingRollupStatus>1</LicensingRollupStatus>
          <LifecycleControllerVersion>2.1.0</LifecycleControllerVersion>
          <Manufacturer>Dell Inc.</Manufacturer>
          <MaxCPUSockets>2</MaxCPUSockets>
          <MaxDIMMSlots>12</MaxDIMMSlots>
          <MaxPCIeSlots>6</MaxPCIeSlots>
          <MemoryOperationMode>OptimizerMode</MemoryOperationMode>
          <Model>PowerEdge T420</Model>
          <NodeID>5P5KMW1</NodeID>
          <PSRollupStatus>1</PSRollupStatus>
          <PlatformGUID>31574d4f-c0b5-4b80-3510-00504c4c4544</PlatformGUID>
          <PopulatedCPUSockets>2</PopulatedCPUSockets>
          <PopulatedDIMMSlots>4</PopulatedDIMMSlots>
          <PopulatedPCIeSlots>1</PopulatedPCIeSlots>
          <PowerCap>317</PowerCap>
          <PowerCapEnabledState>3</PowerCapEnabledState>
          <PowerState>2</PowerState>
          <PrimaryStatus>1</PrimaryStatus>
          <RollupStatus>1</RollupStatus>
          <ServiceTag>5P5KMW1</ServiceTag>
          <StorageRollupStatus>1</StorageRollupStatus>
          <SysMemErrorMethodology>6</SysMemErrorMethodology>
          <SysMemFailOverState>NotInUse</SysMemFailOverState>
          <SysMemLocation>3</SysMemLocation>
          <SysMemMaxCapacitySize>393216</SysMemMaxCapacitySize>
          <SysMemPrimaryStatus>1</SysMemPrimaryStatus>
          <SysMemTotalSize>16384</SysMemTotalSize>
          <SystemGeneration>12G Monolithic</SystemGeneration>
          <SystemID>1273</SystemID>
          <SystemRevision>0</SystemRevision>
          <TempRollupStatus>1</TempRollupStatus>
          <UUID>4c4c4544-0050-3510-804b-b5c04f4d5731</UUID>
          <VoltRollupStatus>1</VoltRollupStatus>
          <smbiosGUID>44454c4c-5000-1035-804b-b5c04f4d5731</smbiosGUID>
        </DCIM_SystemView>
      </Items>
      <EndOfSequence/>
    </PullResponse>
  </Body>
</Envelope>

When I try to use the simplexml_load_string function, it returns the following errors and does not process the data as a string.

PHP Warning: simplexml_load_string(): Entity: line 1: parser error : XML declaration allowed only at the start of the document in php shell code on line 1

PHP Warning: simplexml_load_string(): in php shell code on line 1

PHP Warning: simplexml_load_string(): ^ in php shell code on line 1

However, if I use the EXACT same XML and manually create the variable like this :

<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://www.w3.org/2003/05/soap-envelope" xmlnwsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlnwsen="http://schemas.xmlsoap.org/ws/2004/09/enumeration" xmlnn1="http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_SystemView" xmlnxsi="http://www.w3.org/2001/XMLSchema-instance">
  <Header>
    <To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</To>
    <Action>http://schemas.xmlsoap.org/ws/2004/09/enumeration/PullResponse</Action>
    <RelatesTo>uuid:641403a8-fb4a-1b4a-8003-06c7395d1500</RelatesTo>
    <MessageID>uuid:b4caac95-fb4d-1b4d-8051-fc140555dbe0</MessageID>
  </Header>
  <Body>
    <PullResponse>
      <Items>
        <DCIM_SystemView>
          <AssetTag/>
          <BIOSReleaseDate>01/20/2014</BIOSReleaseDate>
          <BIOSVersionString>2.1.2</BIOSVersionString>
          <BaseBoardChassisSlot>NA</BaseBoardChassisSlot>
          <BatteryRollupStatus>1</BatteryRollupStatus>
          <BladeGeometry>255</BladeGeometry>
          <BoardPartNumber>03015MA01</BoardPartNumber>
          <BoardSerialNumber>CN7475128I0205</BoardSerialNumber>
          <CPLDVersion>1.0.0</CPLDVersion>
          <CPURollupStatus>1</CPURollupStatus>
          <ChassisModel/>
          <ChassisName>Main System Chassis</ChassisName>
          <ChassisServiceTag>5P5KMW1</ChassisServiceTag>
          <ChassisSystemHeight>5</ChassisSystemHeight>
          <DeviceDescription>System</DeviceDescription>
          <ExpressServiceCode>12404926945</ExpressServiceCode>
          <FQDD>System.Embedded.1</FQDD>
          <FanRollupStatus>1</FanRollupStatus>
          <HostName/>
          <InstanceID>System.Embedded.1</InstanceID>
          <LastSystemInventoryTime>20140608040932.000000+000</LastSystemInventoryTime>
          <LastUpdateTime>20140522204842.000000+000</LastUpdateTime>
          <LicensingRollupStatus>1</LicensingRollupStatus>
          <LifecycleControllerVersion>2.1.0</LifecycleControllerVersion>
          <Manufacturer>Dell Inc.</Manufacturer>
          <MaxCPUSockets>2</MaxCPUSockets>
          <MaxDIMMSlots>12</MaxDIMMSlots>
          <MaxPCIeSlots>6</MaxPCIeSlots>
          <MemoryOperationMode>OptimizerMode</MemoryOperationMode>
          <Model>PowerEdge T420</Model>
          <NodeID>5P5KMW1</NodeID>
          <PSRollupStatus>1</PSRollupStatus>
          <PlatformGUID>31574d4f-c0b5-4b80-3510-00504c4c4544</PlatformGUID>
          <PopulatedCPUSockets>2</PopulatedCPUSockets>
          <PopulatedDIMMSlots>4</PopulatedDIMMSlots>
          <PopulatedPCIeSlots>1</PopulatedPCIeSlots>
          <PowerCap>317</PowerCap>
          <PowerCapEnabledState>3</PowerCapEnabledState>
          <PowerState>2</PowerState>
          <PrimaryStatus>1</PrimaryStatus>
          <RollupStatus>1</RollupStatus>
          <ServiceTag>5P5KMW1</ServiceTag>
          <StorageRollupStatus>1</StorageRollupStatus>
          <SysMemErrorMethodology>6</SysMemErrorMethodology>
          <SysMemFailOverState>NotInUse</SysMemFailOverState>
          <SysMemLocation>3</SysMemLocation>
          <SysMemMaxCapacitySize>393216</SysMemMaxCapacitySize>
          <SysMemPrimaryStatus>1</SysMemPrimaryStatus>
          <SysMemTotalSize>16384</SysMemTotalSize>
          <SystemGeneration>12G Monolithic</SystemGeneration>
          <SystemID>1273</SystemID>
          <SystemRevision>0</SystemRevision>
          <TempRollupStatus>1</TempRollupStatus>
          <UUID>4c4c4544-0050-3510-804b-b5c04f4d5731</UUID>
          <VoltRollupStatus>1</VoltRollupStatus>
          <smbiosGUID>44454c4c-5000-1035-804b-b5c04f4d5731</smbiosGUID>
        </DCIM_SystemView>
      </Items>
      <EndOfSequence/>
    </PullResponse>
  </Body>
</Envelope>
XML;

It works like a charm. So, my question is simple. How can I tell PHP to process the string the same way it processes the XML like it does if the XML identifier is used. I have tried to reprocess the string like this:

 $new_string = <<<XML
   $string
     XML;

But no go. Any other ideas?

2
  • i tried your first example on simple_xml_load_string(), and it works fine, and XML string interpretation <<<XML XML; also works. codepad.viper-7.com/ltKlNI Commented Jun 8, 2014 at 4:02
  • Hi there kevinabelita, if you create the variable with quotes (' ') instead of using < < < XML then it gives you the same results I'm getting. Also, the PHP version is 5.3.3. Commented Jun 8, 2014 at 4:36

1 Answer 1

1

Are you using file_get_contents to actually load the XML file into a string before using simplexml_load_string which—as the name states—loads XML from a string?

$xml_file = file_get_contents('test.xml');

$xml = simplexml_load_string($xml_file);

echo '<pre>';
print_r($xml);
echo '</pre>';

And the output is good when I use the XML from your post:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [xmlnwsa] => http://schemas.xmlsoap.org/ws/2004/08/addressing
            [xmlnwsen] => http://schemas.xmlsoap.org/ws/2004/09/enumeration
            [xmlnn1] => http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_SystemView
            [xmlnxsi] => http://www.w3.org/2001/XMLSchema-instance
        )

And so on…
And so on…
And so on…

But that said, I can recreate your error exactly if I add a space or line to the beginning of the XML file like this; note the one simple space before <?xml version="1.0" encoding="UTF-8"?>:

 <?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://www.w3.org/2003/05/soap-envelope" xmlnwsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlnwsen="http://schemas.xmlsoap.org/ws/2004/09/enumeration" xmlnn1="http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_SystemView" xmlnxsi="http://www.w3.org/2001/XMLSchema-instance">

And so on…
And so on…
And so on…

And here is my error:

Warning: simplexml_load_string(): Entity: line 1: parser error : XML declaration allowed only at the start of the document in /Applications/MAMP/htdocs/test.php on line 5

Warning: simplexml_load_string(): in /Applications/MAMP/htdocs/test.php on line 5

Warning: simplexml_load_string(): ^ in /Applications/MAMP/htdocs/test.php on line 5

So hey! I know your pain!

Anyway, the quick solution I tried is to use trim on the $xml_file to get rid of extraneous white space at the beginning & end of the of the file like this:

$xml_file = file_get_contents('test.xml');

$xml = simplexml_load_string(trim($xml_file));

echo '<pre>';
print_r($xml);
echo '</pre>';

And all works great!

Sign up to request clarification or add additional context in comments.

2 Comments

Hi there Jake, I'm using simplexml_load_string. I would very much like to not have to use load_file, as I want to pull the information directly from the variable created when I run the query which returns the information...
Dude, you are the bomb. I've been screwing with this thing for a few hours now and you got it in a few minutes. Thanks a ton. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.