views:

171

answers:

5

Hello

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?

+1  A: 

Have a look at the simplexml module.

Matthew Vines
+1  A: 

Use an XML parser like SimpleXML to extract that data:

$doc = simplexml_load_string($_POST['xml']);
$OrderNumber = $doc->Criterions->OrderNumber;
$StartDate = $doc->Criterions->StartDate;
$EndDate = $doc->Criterions->EndDate;
Gumbo
Can you list the code change of $result in SimpleXML.
Ibn Saeed
@Gumbo, i tried the above code. Its working but it only giving me the value of the elements.(2009-01-01)I need the complete element along with its value. i.e (<StartDate>2009-01-01</StartDate>)
Ibn Saeed
+3  A: 

Some temp vars will help and also make it easier to read.

$result = substr($text_all, strpos($text_all, "<".$field.">")+strlen("<".$field.">"), strpos($text_all, "</".$field.">")-strlen("<".$field.">")-strpos($text_all, "<".$field.">"));

Can be rewritten to.

$tagLen = strlen('<'.$field.'>');
$openTag = strpos($text_all, '<'.$field.'>');
$closeTag = strpos($text_all, '</'.$field.'>', $openTag);

$result = substr($text_all, $openTag + $tagLen, $closeTag - $tagLen - $openTag);

or use SimpleXML

$doc = simplexml_load_string($text_all);
echo $doc->Criterions->asXML();

If you want the individual values use this.

$doc = simplexml_load_string($text_all);

echo $doc->Criterions->OrderNumber;
echo $doc->Criterions->StartDate;
echo $doc->Criterions->EndDate;

if you want the element in XML then use asXML():

$doc = simplexml_load_string($text_all);

echo $doc->Criterions->OrderNumber->asXML();
echo $doc->Criterions->StartDate->asXML();
echo $doc->Criterions->EndDate->asXML();
gradbot
You code is not complete. Its not giving me the complete tags.
Ibn Saeed
Fixed. Sorry about that. Just goes to show the danger of having a really long expression like that. I didn't even notice the closing tag at first.
gradbot
On a side note if you use asXML on an empty element or an element that contains an empty child you will get <tag/> instead of <tag></tag>
gradbot
Oops, I didn't see your answer. Oh well... Technically my answer is sort of easier (o_0) and will work if he adds more children in the future. Good work man.
KyleFarris
@gradbot, the first solution worked great. Now Ill try the SimpleXML solution.
Ibn Saeed
+2  A: 

With SimpleXML:

<?php
$xmlSTR = '<?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>';

$xml = new SimpleXMLElement($xmlSTR);

echo $xml->Credentials->Username;

//To see the entire structure print_r($xml); as php can access objects as arrays
Unkwntech
This would only give me the value of the element. I need both the element along with its value.Like this ::: <StartDate>2009-01-01</StartDate>
Ibn Saeed
+1  A: 

Assuming your XML string is stored in the variable called $text_all (as you show in your example), this will get you what you want man... :-)

$mysql_stuff = '';
$xml = new SimpleXMLElement($text_all);

foreach($xml->Criterions->children() as $criterion) {
    $mysql_stuff .= $criterion->asXML()."\n";
}
echo $mysql_stuff;

$mysql_stuff will now contain:

<OrderNumber/>
<StartDate>2009-01-01</StartDate>
<EndDate>2009-07-01</EndDate>

I tested this and it works. I hope this answers your question!

KyleFarris
Thanks, ill try it out.
Ibn Saeed