【发布时间】:2012-10-09 05:11:23
【问题描述】:
鉴于此源文件(file.xml):
<article>
<story name="column">
<runs>
<run p="902" c="103">
THINK ABOUT IT
</run>
</runs>
</story>
<story name="body">
<runs>
<run p="895" c="103">
‘
</run>
<run p="895" c="920">
T
</run>
<run p="895" c="103">
here is an abiding
<eol />
beauty which may be
<eol />
appreciated by those
<eol />
who will see things as
<eol />
they are and who will
<eol />
ask for no reward
<eol />
except to see.’
<eol />
</run>
<run p="896" c="103">
Vera Brittain
<eol />
(1893-1970)
<eol />
</run>
<run p="897" c="103">
British author
</run>
</runs>
</story>
我已将其放入一个简单的 PHP 脚本中,以从故事元素中获取所有具有“body”属性的文本:
<?php
$xml = simplexml_load_file( "file.xml" );
$body = $xml->xpath( "//story[@name='body']/*[not(self::eol)]" );
if( $body ){
print_r( $body[0] );
}
?>
我的输出和我预期的差不多:
SimpleXMLElement Object
(
[run] => Array
(
[0] => ‘
[1] => T
[2] => here is an abiding beauty which may be appreciated by those who will see things as they are and who will ask for no reward except to see.’
[3] => Vera Brittain
(1893-1970)
[4] => British author
)
)
无论出于何种原因,我都找不到访问这些值以将它们连接在一起的方法。我尝试通过$body[0]、$body[0]->run 等进行解析,但没有给出我期望的结果。
底线,我需要得到一个带有值的字符串:
‘There is an abiding
beauty which may be
appreciated by those
who will see things as
they are and who will
ask for no reward
except to see.’
Vera Brittain
(1893-1970)
British author
提前致谢!
【问题讨论】:
-
$body[0]->run(或$tmp=$body[0];$tmp->run)是一个数组吗?那你能implode吗?