我需要解析外部提供的XML文件,其中包含具有换行符的属性。使用SimpleXML时,似乎会丢失这些换行符。根据另一个stackoverflow问题,虽然远非理想,但换行符应该是有效的XML格式。
为什么会丢失它们?[编辑] 我该如何保留它们? [/编辑]
这是一个演示文件脚本(请注意,当换行符不在属性中时,它们会被保留)。 带嵌入式XML的PHP文件
为什么会丢失它们?[编辑] 我该如何保留它们? [/编辑]
这是一个演示文件脚本(请注意,当换行符不在属性中时,它们会被保留)。 带嵌入式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!
)
)