php 格式化xml字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3616540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:26:07  来源:igfitidea点击:

format xml string

phpxmlstring

提问by quj

What is the best way to format XML within a PHP class.

在 PHP 类中格式化 XML 的最佳方法是什么。

$xml = "<element attribute=\"something\">...</element>";

$xml = '<element attribute="something">...</element>';

$xml = '<element attribute=\'something\'>...</element>';

$xml = <<<EOF
<element attribute="something">
</element>
EOF;

I'm pretty sure it is the last one!

我敢肯定这是最后一个!

回答by Gordon

With DOMyou can do

使用DOM你可以做到

$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML('<root><foo><bar>baz</bar></foo></root>');
$dom->formatOutput = TRUE;
echo $dom->saveXML();

gives (live demo)

给出(现场演示

<?xml version="1.0"?>
<root>
  <foo>
    <bar>baz</bar>
  </foo>
</root>

See DOMDocument::formatOutputand DOMDocument::preserveWhiteSpaceproperties description.

请参阅DOMDocument::formatOutputDOMDocument::preserveWhiteSpace属性说明。

回答by pravat231

This function works perfectlly as you want you don't have to use any xml dom library or nething just pass the xml generated string into it and it will parse and generate the new one with tabs and line breaks.

这个函数完美地工作,因为你不需要使用任何 xml dom 库或 nething 只需将 xml 生成的字符串传递给它,它就会解析并生成带有制表符和换行符的新字符串。

function formatXmlString($xml){
    $xml = preg_replace('/(>)(<)(\/*)/', "\n", $xml);
    $token      = strtok($xml, "\n");
    $result     = '';
    $pad        = 0; 
    $matches    = array();
    while ($token !== false) : 
        if (preg_match('/.+<\/\w[^>]*>$/', $token, $matches)) : 
          $indent=0;
        elseif (preg_match('/^<\/\w/', $token, $matches)) :
          $pad--;
          $indent = 0;
        elseif (preg_match('/^<\w[^>]*[^\/]>.*$/', $token, $matches)) :
          $indent=1;
        else :
          $indent = 0; 
        endif;
        $line    = str_pad($token, strlen($token)+$pad, ' ', STR_PAD_LEFT);
        $result .= $line . "\n";
        $token   = strtok("\n");
        $pad    += $indent;
    endwhile; 
    return $result;
}

回答by Pramendra Gupta

//Here is example using XMLWriter

//这里是使用XMLWriter的例子

$w = new XMLWriter;
$w->openMemory();
$w->setIndent(true);
  $w->startElement('foo');
    $w->startElement('bar');
      $w->writeElement("key", "value");
    $w->endElement();
  $w->endElement();
echo $w->outputMemory();

//out put

//输出

<foo> 
 <bar> 
  <key>value</key> 
 </bar> 
</foo> 

回答by Mark Brown

The first is better if you plan to embed values into the XML, The second is better for humans to read. Neither is good if you intend really work with XML.

如果您打算将值嵌入到 XML 中,第一个更好,第二个更适合人类阅读。如果您打算真正使用 XML,两者都不是很好。

However if you intend to perform a simple fire and forget function that takes XML as a input parameter, then I would say use the first method because you will need to embed parameters at some point.

但是,如果您打算执行一个将 XML 作为输入参数的简单的即发即弃函数,那么我会说使用第一种方法,因为您需要在某个时候嵌入参数。

I personally would use the PHP class simplexml, it's very easy to use and it's built in xpath support makes detailing the data returned in XML a dream.

我个人会使用 PHP 类 simplexml,它非常易于使用,并且它内置的 xpath 支持使得详细描述以 XML 返回的数据成为梦想​​。