PHP/SimpleXML不显示所有内容
我有以下PHP代码:PHP/SimpleXML不显示所有内容,php,simplexml,Php,Simplexml,我有以下PHP代码: <?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); ?> [@attributes]=>数组 ( [类型]=>应用程序/xml ) 当我在浏览器中直接打开链接时,我会得到更多的内容。
<?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);
?>
[@attributes]=>数组
(
[类型]=>应用程序/xml
)
当我在浏览器中直接打开链接时,我会得到更多的内容。我在这里做错了什么?不确定您想问什么,但如果您的代码没有任何错误。在浏览器中,输出应如下所示: SimpleXMLElement对象([id]=>('96TDR3')[category]=>SimpleXMLElement对象([@attributes]=>Array([term]=>opendata.rdw.VRTG.Open.Data.KENT_VRTG_O_DAT[scheme]=>)[link]=>SimpleXMLElement对象([@attributes]=>Array([rel]=>edit[title]=>KENT_VRTG O_DAT[href]=>KENT_VRTG_O__DAT('96TDR3'))[title]=>SimpleXMLElement对象()[更新]=>2014-10-07T21:44:15Z[作者]=>SimpleXMLElement对象([0]=>SimpleXMLElement对象())[内容]=>SimpleXMLElement对象([@attributes]=>Array([type]=>application/xml))) 这只不过是您试图在url中获取的XML文件的结构,而您的代码正是这样做的。如果要设置样式,请获取特定的子节点 我在这里干什么 在SimpleXMLElement上使用
print\u r
或var\u dump
时,很容易将输出与SimpleXMLElement对象中的实际内容混淆
而不是
print_r($xml);
你必须使用
echo $xml->asXML();
显示已加载到SimpleXMLElement对象中的实际XML数据
如果将其回显到浏览器中,则必须使用来查看它,或者需要先将其编码为HTML:
echo '<pre>', htmlspecialchars($xml->asXML()), '</pre>';
Aprint\u r($dbh)代码>也不会显示数据库的所有内容-即使它允许访问所有数据库内容
这是因为simplexmlement和PDO是对象,而不是array
或stdClass
,对于这些对象,var\u dump
或print\u r
将显示它们包含的所有数据。$xmlContent=>
阿昔洛丹
乳酪。1克50毫克阿昔洛韦。
';
$dom=新的DomDocument();
$xml=simplexml\u load\u字符串($xmlContent);
$dom->loadXML($xml->asXML());
$result=$dom->getElementsByTagName('PharmaceuticalFormText');
回声“;打印($result[0]->nodeValue);
//输出
乳酪。1克50毫克阿昔洛韦。
当我直接在浏览器中打开URL()时,我得到的数据比PHP显示的要多。PHP没有显示以下数据:353.00请看这里[堆栈溢出答案][1][1]:这里的答案很简单:不要使用print\r
或var\u dump
来“验证”SimpleXML。只需查看您拥有的实际XML,并使用它来访问它。如果它有名称空间,请根据需要使用和。
echo $xml->asXML();
echo '<pre>', htmlspecialchars($xml->asXML()), '</pre>';
$dbh = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);
$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.