2016-10-01 22 views
2

何らかの理由でSimpleXMLが解析されません&lt;ADMIN&gt;PHP SimpleXMLが解析されない<

私のXMLファイルの内容:XMLファイルを読み込み、

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE client-config SYSTEM "asigra_conf_windows.dtd"> 
<notifications> 
    <email-notification recipient="&lt;ADMIN&gt;"/> 
</notifications> 

はコード:

$xml = simplexml_load_file('http://localhost/test/xml.xml'); 

    echo "Recipient:". $xml['email-notification']['@attributes']['recipient']; 

出力が受信者である: "ブランク"

どのように私はそれがからADMINを読むことができます&lt;ADMIN&gt;

私はApacheとPHP 5.6でxamppを使用しています。

+0

または全体 '<ADMIN>' –

+0

$ XML->子供()[0] - >属性()[ '受信者'] – Andreas

+0

$ xml_string->子供()[0 ] - > attributes()['recipient']が空白として表示されています。 FirefoxでInspect Elementを実行すると、「」と表示されます。 –

答えて

1

皆さん、ありがとうございました(特に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="&lt;ADMIN&gt;"/> 
    </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'}; 
} 
関連する問題