简体   繁体   中英

PHP / SimpleXML not showing all content

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?

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.

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.

 $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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM