问题可能比较弱,之前碰到过,但实在想不起来了,只好求助大家<?php
header("Content-Type:text/xml");
$dom=new DOMDocument('1.0','utf-8');
  $root=$dom->createElement('root');
  $dom->appendChild($root);
  $xmlStr=$dom->saveXML();
  echo $xmlStr;
?>IE上无法显示 XML 页,报 XML 文档必须有一个顶层元素。不知道是不是php文件格式BOM的问题,但改成无BOM utf-8,还是不行。
不知道和后台php,apache是否有关十分感谢

解决方案 »

  1.   

    我一直用的是UltraEdit,很早之前写了个php使用xml放在网站上都正常
    今天按照那个例子重写,然后上传的本地服务器上,就无法显示。会不会和环境和文件格式有关?
      

  2.   

    必须要有文本元素么,我想有一个root就可以了啊
      

  3.   

    <?php
      header("Content-Type:text/xml");
      $dom=new DOMDocument('1.0','utf-8');
      
      $root=$dom->createElement('root');
      $dom->appendChild($root);
      
      $single=$dom->createElement('single');
      $root->appendChild($single);
      
      $name=$dom->createElement('name');
      $single->appendChild($name);
      
      $nameContent=$dom->createTextNode('abc');
      $name->appendChild($nameContent);
      
      $xmlStr=$dom->saveXML();
      echo $xmlStr;
    ?>这是原来程序的一部分,我放在本地服务器上,同样的错误
      

  4.   

    http://apps.hi.baidu.com/share/detail/15121712
    有个小例子
      

  5.   

    唯一区别是,header("Content-Type: text/plain"),试了你的例子,没有报错了,但是无输出
      

  6.   

    header("Content-Type: text/xml");
      

  7.   

    <?php
      header("Content-Type: text/xml");
      echo '<root><single><name>abc</name></single></root>';
    ?>应该不是服务器的问题,直接输出,又可以显示了,用dom操作,就不行,问题出在哪里呢?
      

  8.   

    ie错误如下无法显示 XML 页。 
    使用 样式表无法查看 XML 输入。请更正错误然后单击 刷新按钮,或以后重试。 
    --------------------------------------------------------------------------------
    XML 文档必须有一个顶层元素。处理资源 http://....../1.php 时出错。1.用dom操作的方法,同样的代码,在本机windows+apache+php下,就可以正常输出;
    而在服务器上,linux+apache+php,就不能输出,(php5.1,phpinfo都是支持xml的)。2.用echo直接输出,本机和服务器上,都可以输出xml。3.用了别人和手册上的范例,服务器上依然无法输出。
    初步判断是可能是linux,php环境的问题,但是php5.1.6是支持xml的,而且phpinfo中都是可用的。
    本人是用ultraedit编辑php,难道和php文件格式(utf-8,无BOM)之类的有关?请高人指点。
      

  9.   

    $Xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".Chr(10);
    $Xml.= " <urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">".Chr(10);
    $Xml.= " <url>".Chr(10);
    $Xml.= " <loc>".$Fle."</loc>".Chr(10);
    $Xml.= " <lastmod>".now("Y-m-d",$val[0])."</lastmod>".Chr(10);
    $Xml.= " <changefreq>$changefreq</changefreq>".Chr(10);
    $Xml.= " <priority>$priority</priority>".Chr(10);
    $Xml.= " </url>".Chr(10).Chr(10);
    $i++;
    $Xml.= " </urlset>"; File_Put_Contents("../sitemaps.xml",$Xml,LOCK_EX);
      

  10.   

    这是我的一个购物车程序里使用xml的片段,楼主不妨参考下    $dom = new DOMDocument('1.0', 'utf-8');
        $rootnode = $dom->appendChild(new DOMElement('Order'));
        $rootnode->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
        $rootnode->setAttribute('saveDate', date("Y-m-d H:i:s"));
        //iconv(srctype,dsttype,str)函数用于转换编码
        $rootnode->appendChild(new DOMElement("orderId", iconv("UTF-8", "GBK", $insertId)));
        $rootnode->appendChild(new DOMElement("orderPrice", iconv("UTF-8", "GBK//IGNORE",
            $ordPri)));
        $rootnode->appendChild(new DOMElement("userId", iconv("UTF-8", "GBK//IGNORE", $ordMemId)));
        $rootnode->appendChild(new DOMElement("userName", iconv("UTF-8", "GBK//IGNORE",
            $ordMemNick)));    $goodsList = $rootnode->appendChild(new DOMElement(iconv("UTF-8", "GBK//IGNORE",
            "goodsList")));
        foreach ($objOrdCon as $key => $good) {
            $goodcur = $goodsList->appendChild(new DOMElement(iconv("UTF-8", "GBK//IGNORE",
                'good')));
            $goodcur->setAttribute('goodName', iconv("UTF-8", "GBK//IGNORE", $good->godnam));
            $goodcur->setAttribute('goodId', iconv("UTF-8", "GBK//IGNORE", $good->godid));
            $goodcur->setAttribute('goodPrice', iconv("UTF-8", "GBK//IGNORE", $good->godpri));
            $goodcur->appendChild(new DOMElement('quantity', iconv("UTF-8", "GBK//IGNORE", $good->
                godnum)));
            $goodcur->appendChild(new DOMElement('subtotal', iconv("UTF-8", "GBK//IGNORE", ($good->
                godnum * $good->godpri))));
        }
        $dom->save("cart.$insertId.xml");
      

  11.   

    感谢大家的热心回复,但是问题还没有解决,应该是 php的环境不能使用dom操作,输出xml,用echo直接输出xml就没有问题。难道php需要扩展,才能支持dom操作xml,但是php5.1.6版本支持dom啊,phpinfo中也显示dom可用啊求高人指点啊。
      

  12.   

    搞定,phpinfo中发现是 --disable-dom,在想不是不因为php是rpm安装的,没有编译安装带参数,导致不支持dom。yum -y install php-dom重启apache,搞定,十分感谢大家的关注