これは、SimpleXML と DOM を使用して URL から XML を読み取り、いくつかのパラメーターを変更して Web ページに表示する PHP コードです。
私が読んでいるフィードはhttp://tinyurl.com/boy7mr5にあります
<?php
$xml = simplexml_load_file('http://www.abc.com');
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "All_Products" );
$doc->appendChild( $r );
foreach( $xml as $Product)
{
$b = $doc->createElement( "Product" );
$doc->appendChild( $b );
foreach($Product as $prname=>$value)
{
$prname1 = $doc->createElement( $prname );
$prname1->appendChild(
$doc->createTextNode( $value )
);
$b->appendChild($prname1);
if($prname=='ProductName')
{
$ProductURL = $doc->createElement( "ProductURL" );
$ProductURL->appendChild(
$doc->createTextNode('http://www.abc.com/'.$Product->ProductName.'-p/'.$Product->ProductCode .'.htm' )
);
$b->appendChild( $ProductURL );
}
if($prname=='Categories'){
foreach($value as $catname=>$catvalue)
{
$c = $doc->createElement( "Category" );
$doc->appendChild( $c );
foreach($catvalue as $catname1=>$catvalue1)
{
// echo $catname1."==".$catvalue1;
$catname12 = $doc->createElement( $catname1);
$catname12 ->appendChild(
$doc->createTextNode(htmlspecialchars_decode($catvalue1) )
);
$c->appendChild( $catname12);
}
$prname1->appendChild( $c );
}
}
}
$r->appendChild( $b );
}
echo $doc->saveXML();
?>
最後の行では、すべての XML がそのまま出力されますが、この URL http://tinyurl.com/bty8286で確認できるように、ガベージ データが表示されます 。
ブラウザでhttp://tinyurl.com/boy7mr5のようにデータを表示したいのですが、コードの何を変更すればよいですか