user3654984
user3654984

Reputation: 11

How to XML to Php

How to write php so I know the link here? There will always be different links

<response>
<redirect>
http://www.example.com/
</redirect>
<code>0</code>
<description>OK</description>
</response>

Upvotes: 0

Views: 53

Answers (3)

ThW
ThW

Reputation: 19502

Use DOM+Xpath:

$xml = <<<'XML'
<response>
  <redirect>http://www.example.com/</redirect>
  <code>0</code>
  <description>OK</description>
</response>
XML;

$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);
var_dump($xpath->evaluate('string(/response/redirect)'));

Output:

string(23) "http://www.example.com/"

Upvotes: 0

JustinM151
JustinM151

Reputation: 744

simplexml_load_string

This should answer all your questions...

http://www.php.net/manual/en/function.simplexml-load-string.php

Upvotes: 0

Rakesh Sharma
Rakesh Sharma

Reputation: 13738

try simplexml

$xml ='<response>
<redirect>
http://www.example.com/
</redirect>
<code>0</code>
<description>OK</description>
</response>';
$xml = simplexml_load_string($xml);
echo $xml->redirect; // http://www.example.com/ 

Upvotes: 1

Related Questions