1

I have the following PHP code:

<?php
$url = 'https://api.datamarket.azure.com/Data.ashx/opendata.rdw/VRTG.Open.Data/v1/KENT_VRTG_O_DAT(\'96TDR3\')';
$xml = simplexml_load_file($url);
print_r($xml);
?>

Output is:

SimpleXMLElement Object
(
[id] => https://api.datamarket.azure.com/Data.ashx/opendata.rdw/VRTG.Open.Data/v1/KENT_VRTG_O_DAT('96TDR3')
[category] => SimpleXMLElement Object
(
[@attributes] => Array
(
[term] => opendata.rdw.VRTG.Open.Data.KENT_VRTG_O_DAT
[scheme] => http://schemas.microsoft.com/ado/2007/08/dataservices/scheme
)

)

[link] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rel] => edit
[title] => KENT_VRTG_O_DAT
[href] => KENT_VRTG_O_DAT('96TDR3')
)

)

[title] => SimpleXMLElement Object
(
)

[updated] => 2014-10-07T21:22:59Z
[author] => SimpleXMLElement Object
(
[name] => SimpleXMLElement Object
(
)

)

[content] => SimpleXMLElement Object
(    

[@attributes] => Array ( [type] => application/xml )

)

)

While when I open the link directly in my browser I get more content. What is wrong I am doing here?

Sinan
  • 83
  • 3
  • 9
  • Look here [Stack Overflow Answer][1] [1]: http://stackoverflow.com/questions/13390738/var-dump-and-simplexml – Billy Oct 07 '14 at 22:32
  • The answer here is simple: do not use `print_r` or `var_dump` to "verify" SimpleXML. Just look at the actual XML you have, and use [the examples in the manual](http://uk1.php.net/manual/en/simplexml.examples-basic.php) to access it. If it has namespaces, use [`->children()`](http://php.net/manual/en/simplexmlelement.children.php) and [`->attributes()`](http://uk1.php.net/manual/en/simplexmlelement.attributes.php) as necessary. – IMSoP Oct 07 '14 at 23:19

3 Answers3

2

What is wrong I am doing here?

It is easily confused when using print_r or var_dump on a SimpleXMLElement the output with the actual content you have inside a SimpleXMLElement object.

Instead of

print_r($xml);

You have to use

echo $xml->asXML();

to show the actual XML data that has been loaded into the SimpleXMLElement object.

If you echo that into your browser, you have to use view-source to view it or you need to encode it to HTML first:

echo '<pre>', htmlspecialchars($xml->asXML()), '</pre>';

You can compare that with the object you have for example with a database:

$dbh = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);

A print_r($dbh); wouldn't show all content of the database neither - even it does allow to access all database content.

This is because SimpleXMLElement and PDO are objects and not array or stdClass for which var_dump or print_r would show all data they contain.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
0

Not sure what you are trying to ask here, but if your code does not have any errors. In browser the output should look like below : SimpleXMLElement Object ( [id] => https://api.datamarket.azure.com/Data.ashx/opendata.rdw/VRTG.Open.Data/v1/KENT_VRTG_O_DAT('96TDR3') [category] => SimpleXMLElement Object ( [@attributes] => Array ( [term] => opendata.rdw.VRTG.Open.Data.KENT_VRTG_O_DAT [scheme] => http://schemas.microsoft.com/ado/2007/08/dataservices/scheme ) ) [link] => SimpleXMLElement Object ( [@attributes] => Array ( [rel] => edit [title] => KENT_VRTG_O_DAT [href] => KENT_VRTG_O_DAT('96TDR3') ) ) [title] => SimpleXMLElement Object ( ) [updated] => 2014-10-07T21:44:15Z [author] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) ) [content] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => application/xml ) ) )

which is nothing but the structure of XML file you are trying fetch in your url and your code is exactly doing that. If you want to style it, then fetch specific child node.

Nav
  • 105
  • 1
  • 2
  • 10
  • When I open the URL (https://api.datamarket.azure.com/Data.ashx/opendata.rdw/VRTG.Open.Data/v1/KENT_VRTG_O_DAT('96TDR3')) directly in my browser I get more data than what PHP is showing. The following data is not shown by PHP: 353.00 – Sinan Oct 07 '14 at 21:53
0
 $xmlContent='<?xml version="1.0" encoding="utf-8"?>
                    <DrugDescriptionStructure
                        xmlns="http://www.medicin.dk/Services"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                        <DrugName>
                            <XHtml Version="1.0"
                                xmlns="http://www.w3.org/1999/xhtml">Aciclodan
                            </XHtml>
                        </DrugName>
                        <PharmaceuticalFormText>
                            <XHtml Title="Dispenseringsform" Version="1.0"
                                xmlns="http://www.w3.org/1999/xhtml">
                                <p>
                                    <b>Creme.</b> 1 g indeholder 50 mg aciclovir.
                                </p>
                            </XHtml>
                        </PharmaceuticalFormText>
                        </DrugDescriptionStructure>';

    $dom = new DomDocument();  
    $xml = simplexml_load_string($xmlContent); 
    $dom->loadXML($xml->asXML()); 
    $result=$dom->getElementsByTagName('PharmaceuticalFormText');
    echo "<pre>"; print_r($result[0]->nodeValue);

//output
Creme. 1 g indeholder 50 mg aciclovir.
Ramesh
  • 1,495
  • 12
  • 14