Convert Xml to php string [duplicate] - php

This question already has answers here:
XML Parser Error Start Tag Expected
(2 answers)
Closed 7 years ago.
I am using below code for converting xml response to php string but I am getting below error
"Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found....."
"Warning: simplexml_load_string(): example.com...."
"Warning: simplexml_load_string(): ^ in /home/example/public_html/example.php on line 9"
<?php
echo '<?xml version="1.0" encoding="utf-8" ?>';
$url= "example.com";
$xml = simplexml_load_string($url);
echo $xml->status;
?>

Well, simplexml_load_string expects an XML string to be passed as parameter, which is not the case of your code: the content of $url variable is not a valid XML string, thus, you get those errors.
You may want to load the $url as a file instead. Example:
$xml = simplexml_load_file($url);
Final code:
<?php
echo '<?xml version="1.0" encoding="utf-8" ?>';
$url ="example.com";
$xml = simplexml_load_file($url);
echo $xml->status;

Related

How to convert an XML response from curl to json [duplicate]

This question already has answers here:
Reference - How do I handle Namespaces (Tags and Attributes with a Colon in their Name) in SimpleXML?
(2 answers)
Closed 4 months ago.
$response = curl_exec($ch);
curl_close($ch);
dd($response);
It is of type response string.
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns3:GetProductListResponse xmlns:ns3="http://xxx1">
<result>
<status>success</status>
</result>
<products>
<product>
<currencyAmount>900.00</currencyAmount>
<currencyType>1</currencyType>
<displayPrice>900.00</displayPrice>
<isDomestic>false</isDomestic>
<id>557830715</id>
<price>900.00</price>
<productSellerCode>TSRT7777</productSellerCode>
<approvalStatus>6</approvalStatus>
...
To convert this data to xml I used simplexml_load_string()
$response = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($response);
dd($xml);
And the output is like this.
^ SimpleXMLElement {#435}
I'm trying to access the data in it and try this.
$status = (string)$xml->result->status;
dd($status);
Returns :
^ ""
I tried using simplexml_load_file() and got no results. My main goal is to get this data as json, but I can't do it because I can't read the values.Any help would be great. Thanks in advance.
After #Jacob Mulquin's suggestion I used:
if ($xml === false) {
dump("b");
foreach (libxml_get_errors() as $error) {
dump($error->message);
}
dd("a");
} else {
dd("c");
}
Returned : "c"
Your sample xml is not well formed, for various reasons, but assuming that the actual $response is a well formed xml string, the following should get you what you need:
#first you need to deal with namespaces
$xml->registerXPathNamespace("ns3", "http://xxx1");
#then use xpath to select your target element
$status = $xml->xpath('//ns3:GetProductListResponse//status')[0];
echo $status;
Output should be
success

Parsing Mixed XML and HTML with php

I am doing a curl request and getting the following back:
//....curl stuff....//
$result = curl_exec($curl);
curl_close ($curl);
print_R($result);
<html><body onload="if (parent.submitterLoaded)
parent.submitterLoaded();">{"AuthenticationType":0,
"DateDisplayFormat":1, "SystemURL":"https://rmm.server.com",
"Username”:”user”, "UserID":"12205_1", "Error":"", "Success":true,
"ClientID":1, "SessionGuid":"9eb91231b04-feca-4704-b445-
cc5b369581e3", "tag":"", "LastRequestDateTime":"636421428277379996"}
</body></html><?xml version="1.0" encoding="utf-8"?><soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body>
<LoginResponse xmlns="http://Iris.net" /></soap:Body></soap:Envelope>
I have tried xml_parser_create and
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
and i get a garbled mess in return.
Warning: simplexml_load_string(): Entity: line 1: parser error : XML
declaration allowed only at the start of the document in
/var/www/cron/billing/test.php on line 68
PHP Warning: simplexml_load_string(): b6-bd4dd8a0760b",
"LastRequestDateTime":"636421426011959977"}</body></html><?xml in
/var/www/cron/billing/test.php on line 68
PHP Warning: simplexml_load_string():
^ in /var/www/cron/billing/test.php on line 68
I can see what appears to be some json at the {"Keys" area of the response. How can i parse this correctly?
What other info do you need to help answer question?
The first warning indicate the parser doesn't like the second part <?xml version... So get rid of it:
$result = substr($result, 0, strpos($result, '<?xml version'));
Then to pull out the JSON string, use:
$jsonString = (string) simplexml_load_string($result)->body;
$array = json_decode($jsonString);
extract the json with DOMDocument, and parse it with json_decode
$domd=#DOMDocument::loadHTML($response);
$json_data=json_decode(trim($domd->getElementsByTagName("body")->item(0)->textContent));
now the stuff in the json can be accessed like $UserID=$json_data->UserID;, ... and the stuff in the HTML can be accessed in $domd, like $loginResponse=$domd->getElementsByTagName("LoginResponse")->item(0)->textContent; - didn't see anything useful in the html other than the json, though..

Issues loading XML file data into a PHP object

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?
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!

XML Parser Error Start Tag Expected

function retrieveProfile()
{
$url = "http://steamcommunity.com/profiles/{$this->steamID64}/?xml=1";
$profileData = simplexml_load_string($url);
if(!empty($profileData->error)) { return NULL; }
$this->friendlyName = (string) $profileData->steamID;
}
Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Start tag expected, '<' not found
Warning: simplexml_load_string() [function.simplexml-load-string]: http://steamcommunity.com/profiles/76561197991676121/?xml=1
Warning: simplexml_load_string() [function.simplexml-load-string]: ^
This is the XML file:
http://steamcommunity.com/profiles/76561197991676121/?xml=1
I got no ideea why it does not work and yes it have <?xml version="1.0" encoding="UTF-8" standalone="yes"?> header !
Tried $profileData = new SimpleXMLElement(file_get_contents($url)) too but I get the same result.
Why is this happening ? How can I solve it? I am searching for 3 hours and see only answers that won't help me .
Errors : http://img824.imageshack.us/img824/9464/errorszz.png
EDIT1 : simplexml_load_string was one of my mistakes but now I get even more errors with simplexml_load_file - Tells me that the document is empty... (Not true !)
You are loading the URL as your XML source. You should have:
$profileData = simplexml_load_file($url);
Url was changing from steamcommunity.com/profiles/76561197991676121/?xml=1 to http://steamcommunity.com/id/virusbogdan/?xml=1 .
Obviously the first link returns null so there was an error. Problem solved !

How to fix XML parsing error with PHP? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP library for parsing XML with a colons in tag names?
I have the xml shown below and I want to parse out the product title. When I use the php code below, I get "Parse error: syntax error, unexpected ':' in /home/content/c/a/s/cashme/html/buylooper/xml.php on line 5" because of the ":" located in the tag. How do I resolve this?
*update: I've got the answer to the first part, but am having trouble in how to parse out an attribute of an xml tag. The tag I am having trouble with is the "s:image" tag (link attribute) inside the "s:images" tag.
<?php
$url = 'xml-file.xml';
$xml = simplexml_load_file($url);
$title = $xml->entry[0]->s:product->s:title;
//print
echo '<br/>';
echo $title;
?>
<entry gd:kind="shopping#product">
<s:product>
<s:googleId>9400569674928563633</s:googleId>
<s:author>
<s:name>Amazon.com</s:name>
<s:accountId>2860562</s:accountId>
</s:author>
<s:creationTime>2010-08-19T05:50:21.000Z</s:creationTime>
<s:modificationTime>2012-01-26T23:54:26.000Z</s:modificationTime>
<s:country>US</s:country>
<s:language>en</s:language>
<s:title>Canon powershot s95 10 mp digital camera with 3.8x wide angle optical image stabilized zoom and 3.0-inch lcd</s:title>
<s:description>desc</s:description>
<s:link>http://www.amazon.com/Canon-PowerShot-S95-Stabilized-3-0-Inch/dp/B003ZSHNGS</s:link>
<s:brand>Canon</s:brand>
<s:condition>new</s:condition>
<s:gtin>00013803126556</s:gtin>
<s:gtins>
<s:gtin>00013803126556</s:gtin>
</s:gtins>
<s:inventories>
<s:inventory channel="online" availability="inStock">
<s:price shipping="0.0" currency="USD">340.41</s:price>
</s:inventory>
</s:inventories>
<s:images>
<s:image link="http://ecx.images-amazon.com/images/I/519z3AjKzHL._SL500_AA300_.jpg"/>
</s:images>
</s:product>
</entry>
Parse the namespaces first.
$namespaces = $xml->getNameSpaces(true);
$s = $xml->children($namespaces['s']);
echo (string)$s->product->title. "\n";
echo (string)$s->product->images->image->attributes()->link;
You need to get the correct namespace with . This is untested, but might do the trick:
$url = 'xml-file.xml';
$xml = simplexml_load_file($url);
$namespaces = $xml->entry->getNameSpaces(true);
// Get children of the correct namespace
$s = $xml->entry[0]->children($namespaces['s']);
$title = $s->product->title;
//print
echo '<br/>';
echo $title;

Categories