0

我正在尝试回显 xml,但我无法回显 xml 标签,这是我的代码

    <?php
    // set feed URL
    $feedURL = 'http://gdata.youtube.com/feeds/api/videos?orderby=updated&vq=rihana';

    // read feed into SimpleXML object
    $sxml = simplexml_load_file($feedURL);
    ?>
      <h1><?php echo $sxml->title; ?></h1>
    <?php
    $xml = new SimpleXMLElement('<xml></xml>');

    // iterate over entries in feed
    foreach ($sxml->entry as $entry) {
      // get nodes in media: namespace for media information
      $media = $entry->children('http://search.yahoo.com/mrss/');

      // get video player URL
      $attrs = $media->group->player->attributes();
      $watch = $attrs['url']; 

      // get video thumbnail
      $attrs = $media->group->thumbnail[0]->attributes();
      $thumbnail = $attrs['url']; 

       $track = $xml->addChild('item');
        $track->addChild('description',  $media->group->description);
        $track->addChild('title', $media->group->title);
        $track->addChild('url', $watch);

    }  
    ?>
     <p>
        <?php       
         header('Content-type: text/xml');


echo( htmlentities( $xml->asXML() ) );

    foreach ($xml->item as $record) {
        echo '<div class="item_record">'."\n";
        echo '<h3>'.$record->title.'</h3>'."\n";
        echo '<p><span class="category">description: </span>'.$record->description.'</p>'."\n";
        echo '<p><span class="category">url: </span><a href='.$record->url.'>'.$record->url.'</a></p>'."\n";

        echo '</div>'."\n";
    }?> 

        </p>

我真的很想以 xml 格式打印数据,就像下面代码的混合

echo( htmlentities( $xml->asXML() ) );

看起来不错

        foreach ($xml->item as $record) {
        echo '<div class="item_record">'."\n";
        echo '<h3>'.$record->title.'</h3>'."\n";
        echo '<p><span class="category">description: </span>'.$record->description.'</p>'."\n";
        echo '<p><span class="category">url: </span><a href='.$record->url.'>'.$record->url.'</a></p>'."\n";

        echo '</div>'."\n";
    }?> 

哪种操作最能解决我的问题?

4

1 回答 1

1

这可能会帮助您:

  $dom = new DOMDocument("1.0");
  $parent = $dom->appendChild($dom->createElement("PARENT1"));
  $parent->setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
  $child = $root->appendChild($dom->createElement("CHILD1"));
  $child->appendChild($dom->createTextNode('VALUE_FOR_CHILD1'));
  $dom->encoding = "UTF-8";
  $xml = $dom->saveXML();
  echo $xml;

会给你这样的东西:

<?xml version="1.0" encoding="UTF-8"?>
<PARENT1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
 <CHILD1>VALUE_FOR_CHILD1</CHILD1>
</PARENT1>
于 2012-10-25T03:38:41.490 回答