Here is a simple form I am using to send XML data in admin_xml.php
<form name="review" action="admin_xml.php" method="post">
<textarea name="xml" cols="40" rows="10"></textarea>
<input class="submit" type="submit" value="Submit Request">
</form>
Here is the XML which I enter in order to retrieve the data from MySQL Database
<?xml version="1.0" encoding="UTF-8"?>
<GetOrdersIds>
<Credentials>
<Username>my_username</Username>
<Password>my_password</Password>
</Credentials>
<Criterions>
<OrderNumber></OrderNumber>
<StartDate>2009-01-01</StartDate>
<EndDate>2009-07-01</EndDate>
</Criterions>
</GetOrdersIds>
Here is the php code which extracts the tags from the xml:
<?php
$text_all = "<?xml version=\"1.0\"?>\r\n<GetOrdersIds version=\"1.0\">\r\n<Credentials>\r\n<Username>my_username</Username>\r\n<Password>my_password</Password>\r\n</Credentials>\r\n<Criterions>\r\n<OrderNumber></OrderNumber>\r\n<StartDate>2009-01-01</StartDate>\r\n<EndDate>2009-07-01</EndDate>\r\n</Criterions>\r\n</GetOrdersIds>";
$field = "Criterions";
$result = substr($text_all, strpos($text_all, "<".$field.">")+strlen("<".$field.">"), strpos($text_all, "</".$field.">")-strlen("<".$field.">")-strpos($text_all, "<".$field.">"));
?>
The result from the above php code is:
<OrderNumber></OrderNumber>
<StartDate>2009-01-01</StartDate>
<EndDate>2009-07-01</EndDate>
When the script is run, the php code goes through my mysql data, and extracts all orders between the Start Date and the End Date given above.
Is there a better way of improving the following php code, so that it performs the same function:
$result = substr($text_all, strpos($text_all, "<".$field.">")+strlen("<".$field.">"), strpos($text_all, "</".$field.">")-strlen("<".$field.">")-strpos($text_all, "<".$field.">"));
This is the code which searches through the xml data and retrieves all the tags.
UPDATE :
Can anyone list the $result code in SimpleXML?
Some temp vars will help and also make it easier to read.
Can be rewritten to.
or use SimpleXML
If you want the individual values use this.
if you want the element in XML then use asXML():