PHP SimpleXML 未解析 <
PHP SimpleXML not parsing <
出于某种原因,SimpleXML 未解析 <ADMIN>
。
我的XML文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE client-config SYSTEM "asigra_conf_windows.dtd">
<notifications>
<email-notification recipient="<ADMIN>"/>
</notifications>
读取 XML 文件的代码:
$xml = simplexml_load_file('http://localhost/test/xml.xml');
echo "Recipient:". $xml['email-notification']['@attributes']['recipient'];
输出为收件人:"blank"
如何让它从 <ADMIN>
读取 ADMIN?
我正在使用 xampp 与 Apache 和 PHP 5.6。
感谢大家的帮助(尤其是 IMSoP,关于 htmlspecialchars 的看法是正确的)。
我一直失败的原因是因为我强化了人们不看手册的刻板印象。
我只需要花 5 分钟查看示例@http://php.net/manual/en/simplexml.examples-basic.php。它还解释了为什么你不应该使用 json_encode/decode.
对于其他 tl;dr 人,这里有一些我现在使用的代码示例:
当我 XML 看起来像这样时:
<configuration>
<setup-config>
<account-name>Trump</account-name>
</setup-config>
<configuration>
↴
$xml = simplexml_load_file('http://localhost/config.xml');
echo $xml->{'configuration'}->{'setup-config'}->{'account-name'};
当我 XML 看起来像这样时:
<configuration>
<setup-config>
<user-info country-code="826"/>
</setup-config>
<configuration>
↴
$xml = simplexml_load_file('http://localhost/config.xml');
echo $xml->{'configuration'}->{'setup-config'}->{'user-info'}['country-code'];
当我有 XML 和 HTML 个字符时:
<configuration>
<defaults-config>
<def-notification name="<ADMIN>"/>
</defaults-config>
</configuration>
↴
$xml = simplexml_load_file('http://localhost/config.xml');
echo htmlspecialchars($xml->{'configuration'}->{'defaults-config'}->{'def-notification'}['name']);
迭代:
<configuration>
<roles-config>
<group-role role="administrator" name="Administrators" from="."/>
<group-role role="backup-operator" name="Backup Operators" from="."/>
</roles-config>
</configuration>
↴
$xml = simplexml_load_file('http://localhost/config.xml');
foreach ($xml->{'configuration'}->{'roles-config'}->{'group-role'} as $grouproles => $groles) {
echo "<tr><td>group role name: ".$groles['name']."</td></tr>";
echo "<tr><td>group role role: ".$groles['role']."</td></tr>";
echo "<tr><td>group role role: ".$groles['from']."</td></tr>";
}
简单的存在性检查:
<configuration>
<setup-config>
<account-name>Trump</account-name>
</setup-config>
<configuration>
↴
$xml = simplexml_load_file('http://localhost/config.xml');
if(isset($xml->{'configuration'}->{'setup-config'}->{'account-name'})){
echo $xml->{'configuration'}->{'setup-config'}->{'account-name'};
}
出于某种原因,SimpleXML 未解析 <ADMIN>
。
我的XML文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE client-config SYSTEM "asigra_conf_windows.dtd">
<notifications>
<email-notification recipient="<ADMIN>"/>
</notifications>
读取 XML 文件的代码:
$xml = simplexml_load_file('http://localhost/test/xml.xml');
echo "Recipient:". $xml['email-notification']['@attributes']['recipient'];
输出为收件人:"blank"
如何让它从 <ADMIN>
读取 ADMIN?
我正在使用 xampp 与 Apache 和 PHP 5.6。
感谢大家的帮助(尤其是 IMSoP,关于 htmlspecialchars 的看法是正确的)。
我一直失败的原因是因为我强化了人们不看手册的刻板印象。
我只需要花 5 分钟查看示例@http://php.net/manual/en/simplexml.examples-basic.php。它还解释了为什么你不应该使用 json_encode/decode.
对于其他 tl;dr 人,这里有一些我现在使用的代码示例:
当我 XML 看起来像这样时:
<configuration>
<setup-config>
<account-name>Trump</account-name>
</setup-config>
<configuration>
↴
$xml = simplexml_load_file('http://localhost/config.xml');
echo $xml->{'configuration'}->{'setup-config'}->{'account-name'};
当我 XML 看起来像这样时:
<configuration>
<setup-config>
<user-info country-code="826"/>
</setup-config>
<configuration>
↴
$xml = simplexml_load_file('http://localhost/config.xml');
echo $xml->{'configuration'}->{'setup-config'}->{'user-info'}['country-code'];
当我有 XML 和 HTML 个字符时:
<configuration>
<defaults-config>
<def-notification name="<ADMIN>"/>
</defaults-config>
</configuration>
↴
$xml = simplexml_load_file('http://localhost/config.xml');
echo htmlspecialchars($xml->{'configuration'}->{'defaults-config'}->{'def-notification'}['name']);
迭代:
<configuration>
<roles-config>
<group-role role="administrator" name="Administrators" from="."/>
<group-role role="backup-operator" name="Backup Operators" from="."/>
</roles-config>
</configuration>
↴
$xml = simplexml_load_file('http://localhost/config.xml');
foreach ($xml->{'configuration'}->{'roles-config'}->{'group-role'} as $grouproles => $groles) {
echo "<tr><td>group role name: ".$groles['name']."</td></tr>";
echo "<tr><td>group role role: ".$groles['role']."</td></tr>";
echo "<tr><td>group role role: ".$groles['from']."</td></tr>";
}
简单的存在性检查:
<configuration>
<setup-config>
<account-name>Trump</account-name>
</setup-config>
<configuration>
↴
$xml = simplexml_load_file('http://localhost/config.xml');
if(isset($xml->{'configuration'}->{'setup-config'}->{'account-name'})){
echo $xml->{'configuration'}->{'setup-config'}->{'account-name'};
}