使用SimpleXML的DOM不会使用PHP在浏览器中显示XML格式的数据 [英] DOM with SimpleXML Doesnt show the data in XML format in Browser using PHP

查看:87
本文介绍了使用SimpleXML的DOM不会使用PHP在浏览器中显示XML格式的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的PHP代码,使用SimpleXML和DOM从URL中读取XML,以更改一些参数并在网页上显示

This is my PHP code to read XML from a URL using SimpleXML and DOM to change some parameters and show it on a web page

我正在阅读的Feed http://tinyurl.com/boy7mr5

The Feed i am reading is at 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,但它显示垃圾数据可以在此网址上查看 http://tinyurl.com/bty8286

The last line prints all the XML as it is but it shows the garbage data as you can see at this url http://tinyurl.com/bty8286.

我希望数据看起来像这样 http:// tinyurl。 com / boy7mr5 在浏览器中,我应该在代码中更改

I want the data to look like this http://tinyurl.com/boy7mr5 in the Browser, what should i change in the code

推荐答案

Content-Type 。第二个链接使用 application / xml ,而第一个使用php的默认 text / html 。您可以使用 header()功能更改php脚本的HTTP标头。

It's a matter of the Content-Type HTTP header. The second link uses the application/xml while the first one uses php's default text/html. You can change your php script's HTTP headers with the header() function.

header('content-type: application/xml');



编辑:



能够获取原始输入,唯一的标题没有使它工作(至少在firefox),在48580上得到解析错误,这是由于没有编码被设置为 DOMDocument 对象,而原始输入是utf-8。使用

I've been able to fetch the original input, the only the header doesn't made it work (at least in firefox), got parse error on line 48580, this is due no encoding was set to the DOMDocument object, while the original input is in utf-8. With

$doc = new DOMDocument('1.0', 'utf-8');

应该工作。

这篇关于使用SimpleXML的DOM不会使用PHP在浏览器中显示XML格式的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆