0

重複の可能性:
PHP XML の適切な形式を出力する方法

私はこれを使っています

<?php

$file = '/Applications/MAMP/htdocs/payback_service/payback_records.xml';

if (file_exists($file)) {
    $sxe = simplexml_load_file($file);  
    $movie = $sxe->addChild('record');
    $movie->addChild('lat', 'PHP2: More Parser Stories');
    $movie->addChild('lng', 'This is all about the people who make it work.');
    $movie->addChild('address', 'PHP2: More Parser Stories');
    $movie->addChild('picture_link', 'PHP2: More Parser Stories');
    $movie->addChild('message', 'PHP2: More Parser Stories');
    $sxe->asXML($file);


} else {
    exit('Failed to open '.$file);
}

フォーマットされた形式で更新されるxmlのテキストが必要です。

今私はこれを得ています:

<record><lat>PHP2: More Parser Stories</lat><lng>This is all about the people who make it work.</lng><address>PHP2: More Parser Stories</address><picture_link>PHP2: More Parser Stories</picture_link><message>PHP2: More Parser Stories</message></record><record><lat>PHP2: More Parser Stories</lat><lng>This is all about the people who make it work.</lng><address>PHP2: More Parser Stories</address><picture_link>PHP2: More Parser Stories</picture_link><message>PHP2: More Parser Stories</message></record></root>

ファイルは次のようにする必要があります。

<record>
    <lat>1.5</lat>
    <lng>-8.5</lng>
    <address>this is the address</address>
    <picture_link>this is picture link</picture_link>
    <message>this is the message</message>
</record>
<record>
    <lat>1.5</lat>
    <lng>0.248</lng>
    <address>PHP2: More Parser Stories</address>
    <picture_link>PHP2: More Parser Stories</picture_link>
    <message>PHP2: More Parser Stories</message>
</record>
4

1 に答える 1

1

SimpleXMLはフォーマットをサポートしていないと思います。ただし、DOMDocumentクラスは次 のことを行います。

<?php

$file = '/Applications/MAMP/htdocs/payback_service/payback_records.xml';

if (file_exists($file)) {
    $sxe = simplexml_load_file($file);  
    $movie = $sxe->addChild('record');
    $movie->addChild('lat', 'PHP2: More Parser Stories');
    $movie->addChild('lng', 'This is all about the people who make it work.');
    $movie->addChild('address', 'PHP2: More Parser Stories');
    $movie->addChild('picture_link', 'PHP2: More Parser Stories');
    $movie->addChild('message', 'PHP2: More Parser Stories');

    $dom = new DOMDocument('1.0');
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    $dom->loadXML($sxe->asXML());
    echo $dom->saveXML();

} else {
    exit('Failed to open '.$file);
}
于 2013-01-28T09:54:09.020 に答える