我必须解析外部提供的 XML,其中包含带有换行符的属性。使用 SimpleXML,换行符似乎丢失了。根据另一个 stackoverflow question,换行符对于 XML 应该是有效的(即使远不理想!)。
他们为什么会迷路?[编辑]我怎样才能保存它们?[/编辑]
这是一个演示文件脚本(请注意,当换行符不在属性中时,它们会被保留)。
带有嵌入式 XML 的 PHP 文件
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<Rows>
<data Title='Data Title' Remarks='First line of the row.
Followed by the second line.
Even a third!' />
<data Title='Full Title' Remarks='None really'>First line of the row.
Followed by the second line.
Even a third!</data>
</Rows>
XML;
$xml = new SimpleXMLElement( $xml );
print '<pre>'; print_r($xml); print '</pre>';
print_r 的输出
SimpleXMLElement Object
(
[data] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Title] => Data Title
[Remarks] => First line of the row. Followed by the second line. Even a third!
)
)
[1] => First line of the row.
Followed by the second line.
Even a third!
)
)