php输出xml dom不能输出中文字符是为什么?怎样处理?

榟ler 发布于 2012/07/24 10:22
阅读 1K+
收藏 0
PHP
加载中
0
yandong
yandong
编码问题吧,试试输出的时候设置成这样呢,或者设置成UTF-8的<?xml version='1.0' encoding='gb2312'?>
榟ler
榟ler
设置了,没有用,一输入汉字就显示空白页!我是用php来编写的,在页面显示xml!
0
yyg291906351
yyg291906351
代码 贴出来啊!!不然我们怎么知道那里有问题啊!
0
榟ler
榟ler

<?php
header("Content-Type: text/xml;encoding='UTF-8'");
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create attribute node
$price = $dom->createAttribute("price");
$item->appendChild($price);
// create attribute value node
$priceValue = $dom->createTextNode("4");
$price->appendChild($priceValue);
// create CDATA section
$cdata = $dom->createCDATASection(" Customer requests that pizza be sliced into 16 square pieces ");
$root->appendChild($cdata);
// create PI
$pi = $dom->createProcessingInstruction("pizza", "bake()");
$root->appendChild($pi);
// save and display tree
//$dom->formatOutput = true;
echo $dom->saveXML();
?>

0
榟ler
榟ler

引用来自“yyg291906351”的答案

代码 贴出来啊!!不然我们怎么知道那里有问题啊!
<?php
header("Content-Type: text/xml;encoding='UTF-8'");
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create attribute node
$price = $dom->createAttribute("price");
$item->appendChild($price);
// create attribute value node
$priceValue = $dom->createTextNode("4");
$price->appendChild($priceValue);
// create CDATA section
$cdata = $dom->createCDATASection(" Customer requests that pizza be sliced into 16 square pieces ");
$root->appendChild($cdata);
// create PI
$pi = $dom->createProcessingInstruction("pizza", "bake()");
$root->appendChild($pi);
// save and display tree
//$dom->formatOutput = true;
echo $dom->saveXML();
?>
0
yyg291906351
yyg291906351

引用来自“劳侠”的答案

引用来自“yyg291906351”的答案

代码 贴出来啊!!不然我们怎么知道那里有问题啊!
<?php
header("Content-Type: text/xml;encoding='UTF-8'");
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create attribute node
$price = $dom->createAttribute("price");
$item->appendChild($price);
// create attribute value node
$priceValue = $dom->createTextNode("4");
$price->appendChild($priceValue);
// create CDATA section
$cdata = $dom->createCDATASection(" Customer requests that pizza be sliced into 16 square pieces ");
$root->appendChild($cdata);
// create PI
$pi = $dom->createProcessingInstruction("pizza", "bake()");
$root->appendChild($pi);
// save and display tree
//$dom->formatOutput = true;
echo $dom->saveXML();
?>
<? php
/*
用PHP的DOM控件来创建XML输出
设置输出内容的类型为xml
*/
header ( ' Content-Type: text/xml; ' );
// 创建新的xml文件
$dom   =   new  DOMDocument( ' 1.0 ' ,   ' utf-8 ' );

// 建立<response>元素
$response   =   $dom -> createElement( ' response ' );
$dom -> appendChild( $response );

// 建立<books>元素并将其作为<response>的子元素
$books   =   $dom -> createElement( ' books ' );
$response -> appendChild( $books );

// 为book创建标题
$title   =   $dom -> createElement( ' title ' );
$titleText   =   $dom -> createTextNode( ' PHP与AJAX ' );
$title -> appendChild( $titleText );

// 为book创建isbn元素
$isbn   =   $dom -> createElement( ' isbn ' );
$isbnText   =   $dom -> createTextNode( ' 1-21258986 ' );
$isbn -> appendChild( $isbnText );

// 创建book元素
$book   =   $dom -> createElement( ' book ' );
$book -> appendChild( $title );
$book -> appendChild( $isbn );

// 将<book>作为<books>子元素
$books -> appendChild( $book );

// 在一字符串变量中建立XML结构
$xmlString   =   $dom -> saveXML();

// 输出XML字符串
echo   $xmlString ;

?>
返回顶部
顶部