皆さん、ありがとうございました(特にIMSoP、あなたはhtmlspecialcharsについては正解でした)。
私が失敗し続けたのは、マニュアルを読んでいない人間のステレオタイプを補強していたからです。
@http://php.net/manual/en/simplexml.examples-basic.phpの例を見てみると、5分かかりました。また、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'};
を
私は、次のとおりです。
<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'];
私はHTMLの文字とXMLを持っている場合:
<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'};
}
または全体 '<ADMIN>' –
$ XML->子供()[0] - >属性()[ '受信者'] – Andreas
$ xml_string->子供()[0 ] - > attributes()['recipient']が空白として表示されています。 FirefoxでInspect Elementを実行すると、「 」と表示されます。 –