xml to php conversion - php

I'm trying to print the response from the xml result.
<?xml version="1.0"?>
<response op="sendsmsmsg" status="400" message="Customer with mobile number 6193030168 is not opted in" version="1.0"/>
SimpleXMLElement Object
(
[#attributes] => Array
(
[op] => sendsmsmsg
[status] => 400
[message] => Customer with mobile number 6193030168 is not opted in
[version] => 1.0
)
)
how to echo the op value, status, message to this result into php

Seems like the XML is malformed. This one works. TESTED
<?php
$string = <<<XML
<?xml version='1.0' standalone='yes'?>
<response2>
<op>sendsmsmsg</op>
<status>400</status>
<message>Customer with mobile number 6193030168 is not opted in</message>
</response2>
XML;
$xml = new SimpleXMLElement($string);
echo $xml->op."<br>".$xml->status."<br>".$xml->message;
?>
OUTPUT
sendsmsmsg
400
Customer with mobile number 6193030168 is not opted in

i have tested it with your xml :
//data.xml has
<?xml version="1.0"?>
<response op="sendsmsmsg" status="400" message="Customer with mobile number 6193030168 is not opted in" version="1.0"/>
<?php
$xml = simplexml_load_file('data.xml');
print($xml['op']);
?>

Related

Parsing very simple XML with PHP

Very simple request (I think) that I have had no luck with.
Here is the contents of the xml file:
<?xml version="1.0" encoding="utf-8"?>
<status USER_ID="xxxxx">OK</status>
Current php:
$xml=simplexml_load_file($file) or die("Error: Cannot create object");
print_r($xml);
Outputs:
SimpleXMLElement Object ( [#attributes] => Array ( [USER_ID] => xxxxx ) [0] => OK )
And now I'm stuck
How can I get the value of USER_ID and that the status was "OK" into my php script.
Thanks.
Try this one below
echo "Display the user id: " . $xml['USER_ID'];
echo "Display the status: " . $xml[0];
Hope this will help you.
If you don't like SimpleXml (like me), you can also use the XMLReader Class like:
$XMLReader = new XMLReader;
$XMLReader->XML(file_get_contents($file)); //you can use $XMLReader->open('file://'.$file); too
//move to first node
$XMLReader->read();
//get an attribute
echo "USER_ID:".$XMLReader->getAttribute('USER_ID')."\n";
//get the contents of the tag as a string
echo "Status:" .$XMLReader->readString();
Output:
USER_ID:xxxxx
Status:OK
Sandbox

Parse xml from Ruby script within php failing with StartTag: invalid element name </Envelope>

I have a Ruby script which does a query to a .NET ASP server and gets the result as a string of XML;
<?xml version="1.0" encoding="utf-8"?>
<Envelope>
<Body>
<QueryServicesResponse>
<QueryServicesResult>
<Date>2016-01-01</Date>
<serviceList>
<service>
<uuid>10264b70-87ee-11e6-ae22-56b6b6499611</uuid>
<flight>EZY0000</flight>
<originName>London Heathrow</originName>
<originShort>LHR</originShort>
<destinationName>London Stansted</destinationName>
<destinationShort>STN</destinationShort>
<scheduledDeparture>2016-01-01T14:00:00</scheduledDeparture>
<scheduledArrival>2016-01-01T14:30:00</scheduledArrival>
</service>
</serviceList>
</QueryServicesResult>
</QueryServicesResponse>
</Body>
</Envelope>
This is the section of the ruby scrip which deals with the returned body;
# Post the request
resp, data = http.post(path, data, headers)
# Output the results
doc = Nokogiri::XML(resp.body)
doc.remove_namespaces!
puts doc
The ruby script is called via a php file with the following code;
<?php
$xml = exec("ruby test.rb EZY0000",($results));
$xmlparse = simplexml_load_string($xml);
echo $xmlparse;
?>
But php Throws the following errors when trying to parse the result;
PHP Warning: simplexml_load_string(): Entity: line 1: parser error :
StartTag: invalid element name
PHP Warning: simplexml_load_string(): </Envelope>
I'm trying to parse the xml into a SimpleXMLElement Object I've been trying all sorts of things over the past few days but am stuck or blind to the problem now. I've tried htmlspecialchars but that didn't help either.
The only thing I can think of is this has something to do with the string coming from the ruby script even though it appears to be, and validates as proper xml.
If I take the xml above and use the following php code then everything works as expected and I get the desired result;
<?php
$string = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<Envelope>
<Body>
<QueryServicesResponse>
<QueryServicesResult>
<Date>2016-01-01</Date>
<serviceList>
<service>
<uuid>10264b70-87ee-11e6-ae22-56b6b6499611</uuid>
<flight>EZY0000</flight>
<originName>London Heathrow</originName>
<originShort>LHR</originShort>
<destinationName>London Stansted</destinationName>
<destinationShort>STN</destinationShort>
<scheduledDeparture>2016-01-01T14:00:00</scheduledDeparture>
<scheduledArrival>2016-01-01T14:30:00</scheduledArrival>
</service>
</serviceList>
</QueryServicesResult>
</QueryServicesResponse>
</Body>
</Envelope>
XML;
$xml = simplexml_load_string($string);
print_r($xml);
?>
Which gives me;
SimpleXMLElement Object
(
[Body] => SimpleXMLElement Object
(
[QueryServicesResponse] => SimpleXMLElement Object
(
[QueryServicesResult] => SimpleXMLElement Object
(
[Date] => 2016-01-01
[serviceList] => SimpleXMLElement Object
(
[service] => SimpleXMLElement Object
(
[uuid] => 10264b70-87ee-11e6-ae22-56b6b6499611
[flight] => EZY0000
[originName] => London Heathrow
[originShort] => LHR
[destinationName] => London Stansted
[destinationShort] => STN
[scheduledDeparture] => 2016-01-01T14:00:00
[scheduledArrival] => 2016-01-01T14:30:00
)
)
)
)
)
)
So how can I get the xml from my ruby script into a valid object which I can manipulate in php? Someome offline said I should try and do it all in Rails - but I'm not ready for anything like that much of a challenge at the moment.
So with a hint from #slowjack2k I re-looked at the Ruby file which generates the response.
doc = Nokogiri::XML(resp.body) Which I changed to become doc = Nokogiri::HTML(resp.body) and low and behold it now works and returns a valid xml object in php as expected.

Reading a XML File in PHP

I am importing a XML file that has an amount field <amount>$10.00</amount> but when it is read in using code I got from your other posts, the value is returned as .00.
Using:
$xml = simplexml_load_file("testInput.xml");
print_r($xml);
Result:
[amount] => .00
I can't find anywhere why this is failing... Unless it has to do with the $ or period in the value field but I can't find anything about reserved characters.
I tried to duplicate your results and couldn't...
I created a file called xml_test.php:
<?php
$xml = simplexml_load_file('test_input.xml');
print_r($xml);
?>
Then built the XML (test_input.xml):
<?xml version="1.0" encoding="UTF-8"?>
<tests>
<test>
<amount>$10.00</amount>
</test>
</tests>
And this was my result in the browser:
SimpleXMLElement Object ( [test] => SimpleXMLElement Object ( [amount] => $10.00 ) )
Is there anything else going on or am I missing something? Maybe you can paste in your XML...

Updating a xml file with php simplexml tool

I have a very simple xml content videolar.xml. Flash player takes these elements and plays them. I want to change the <title> and <youtube> elements by php simplexml. I wrote a code video.php to change these elements. When I run video.php it prints like below. It looks like it works, but if I play the player it plays led zeppelin still, and does not play whitesnake. If I open video.xml by browser I see that xml file has not changed. Still led zeppelin. So how can I update flashplayer's playlist?
videolar.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playlist>
<track>
<title>led zeppelin - kashmir</title>
<youtube>sfR_HWMzgyc</youtube>
</track>
<track>
<title>pink floyd - eclipse</title>
<youtube>BUwUKyztI10</youtube>
</track>
</playlist>
video.php
<?php
$completeurl = $_SERVER['DOCUMENT_ROOT'].'/ytplayer/videolar.xml';
$xml = simplexml_load_file($completeurl);
$xml->track[0]->title = 'Whitesnake - Is this love';
$xml->track[0]->youtube = 'GOJk0HW_hJw';
print_r($xml);
?>
When I run this code above it prints:
(
[track] => Array
(
[0] => SimpleXMLElement Object
(
[title] => Whitesnake - Is this love
[youtube] => GOJk0HW_hJw
)
[1] => SimpleXMLElement Object
(
[title] => pink floyd - eclipse
[youtube] => BUwUKyztI10
)
)
)
You need to save modified xml back into file
<?php
$completeurl = $_SERVER['DOCUMENT_ROOT'].'/ytplayer/videolar.xml';
$xml = simplexml_load_file($completeurl);
$xml->track[0]->title = 'Whitesnake - Is this love';
$xml->track[0]->youtube = 'GOJk0HW_hJw';
$xml->asXML($completeurl)
?>

XPath and PHP troubleshooting

I get this error:
Notice: Trying to get property of non-object in
Applies to:
echo $result->Data;
And this output:
Array ()
Background Informations
A function returns a string which contains an XML file.
I want to get some data from two tags and deal with them on their own.
String Data
$data="
<SyncML xmlns='SYNCML:SYNCML1.0'>
<SyncHdr>
</SyncHdr>
<SyncBody>
<betameta>
WANT 1
</betameta>
<Add>
<Data>
WANT 2
</Data>
</Add>
</SyncBody>
</SyncML>";
In the above data, I want values "WANT 1" and "WANT 2"
Code so far
$xml = simplexml_load_string($data);
$result = $xml->xpath("/SyncML/SyncBody");
print_r($result);
echo $result->Data;
$xml->registerXPathNamespace('default','SYNCML:SYNCML1.0');
$result = $xml->xpath("/default:SyncML/default:SyncBody");
Remove the trailing slash.
The only solution I can find is the following:
<?php
$data= <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<SyncML>
<SyncHdr>
</SyncHdr>
<SyncBody>
<betameta>
WANT 1
</betameta>
<Add>
<Data>
WANT 2
</Data>
</Add>
</SyncBody>
</SyncML>
XML;
$xml = simplexml_load_string($data);
$result = $xml->xpath("/SyncML/SyncBody");
print_r($result);
echo $result;
is there anyway you can loose the xmlns?
This will output:
Array
(
[0] => SimpleXMLElement Object
(
[betameta] =>
WANT 1
[Add] => SimpleXMLElement Object
(
[Data] =>
WANT 2
)
)
)

Categories