输出$ SimpleXML-> asXML()的格式输出。 [英] Format output of $SimpleXML->asXML();

查看:164
本文介绍了输出$ SimpleXML-> asXML()的格式输出。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:



$保存文件格式化的方式b $ b

编辑
是的,这是重复的。投票结束。 - Eli



大家好,

标题几乎全都说。 b

如果我有类似的东西(从php站点的例子):

  $ xmlstr =< << XML 
<?xml version ='1.0'standalone ='yes'?>
<电影>< /电影>
XML;

$ sxe = new SimpleXMLElement($ xmlstr);

$ sxe-> addAttribute('type','documentary');

$ movie = $ sxe-> addChild('movie');
$ movie-> addChild('title','PHP2:More Parser Stories');
$ movie-> addChild('plot','这是所有关于使它工作的人。');

$ characters = $ movie-> addChild('characters');
$ character = $ characters-> addChild('character');
$ character-> addChild('name','Mr. Parser');
$ character-> addChild('actor','John Doe');

$ rating = $ movie-> addChild('rating','5');
$ rating-> addAttribute('type','stars');

$ b echo(< pre>。htmlspecialchars($ sxe-> asXML())。< / pre>);

die();

最后输出一个长字符串如下:

 <?xml version =1.0standalone =yes?> 
< movie type =documentary>< movie>< title> PHP2:更多解析器故事< / title>< plot>< / plot> ;<字符><字符><名称>先生。 < / movies>< / rating>< / actor> John Doe< / actor>< / character>< / characters>< rating type =stars> 5< / rating>< / movie>< / movies>

这对程序使用者来说是好的,但是对于调试/人工任务,没有人知道如何获得这到一个很好的缩进格式?

解决方案

SimpleXMLElement的PHP手册页面。不是非常有效率,但肯定是简洁,是一个由匿名解决方案

  $ dom = dom_import_simplexml($ simpleXml) - > ownerDocument; 
$ dom-> formatOutput = true;
echo $ dom-> saveXML();

PHP手册页面注释通常是常见需求的良好来源,只要您明确地过滤掉错误的东西先。


Possible Duplicate:
PHP simpleXML how to save the file in a formatted way?

Edit Yes, this is a duplicate. Voted to close. - Eli

Hi All,

The title pretty much says it all.

If I have something like (from the php site examples):

 $xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies></movies>
XML;

$sxe = new SimpleXMLElement($xmlstr);

$sxe->addAttribute('type', 'documentary');

$movie = $sxe->addChild('movie');
$movie->addChild('title', 'PHP2: More Parser Stories');
$movie->addChild('plot', 'This is all about the people who make it work.');

$characters = $movie->addChild('characters');
$character  = $characters->addChild('character');
$character->addChild('name', 'Mr. Parser');
$character->addChild('actor', 'John Doe');

$rating = $movie->addChild('rating', '5');
$rating->addAttribute('type', 'stars');


echo("<pre>".htmlspecialchars($sxe->asXML())."</pre>");

die();

I end up outputing a long string like so:

<?xml version="1.0" standalone="yes"?>
<movies type="documentary"><movie><title>PHP2: More Parser Stories</title><plot>This is all about the people who make it work.</plot><characters><character><name>Mr. Parser</name><actor>John Doe</actor></character></characters><rating type="stars">5</rating></movie></movies>

This is fine for a program consumer, but for debugging/human tasks, does anyone know how to get this into a nice indented format?

解决方案

There's a variety of solutions in the comments on the PHP manual page for SimpleXMLElement. Not very efficient, but certainly terse, is a solution by Anonymous

$dom = dom_import_simplexml($simpleXml)->ownerDocument;
$dom->formatOutput = true;
echo $dom->saveXML();

The PHP manual page comments are often good sources for common needs, as long as you filter out the patently wrong stuff first.

这篇关于输出$ SimpleXML-&gt; asXML()的格式输出。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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