您好我想查找有房号和生日的用户信息。我用file_get_contents和simplexml_load_string加载xml文件。我从url成功获取数据。原始URL XML数据:
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="userinfo.xsd" generated="2013-03-22T11:16:13">
<userinfo>
<BirthDate>1967-12-27</BirthDate>
<RoomNo>111</IDNo>
<ClientName>AAA BBB CCC</ClientName>
</userinfo>
<userinfo>
<BirthDate>1970-01-01</BirthDate>
<RoomNo>121</IDNo>
<ClientName>DDD EEE FFF</ClientName>
</userinfo>
<userinfo>
我试过这段代码:$ found = $ xml-&gt; xpath(&#39; // userinfo / BirthDate [contains(text(),&#34; 1967-12-27&#34;)]和RoomNo [contains(text(),&#34; 111&#34;)]&#39;)[0];但我没有得到任何东西。
答案 0 :(得分:3)
我已经开始清理你的代码并制作了一份工作样本
$string = '<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="userinfo.xsd" generated="2013-03-22T11:16:13">
<userinfo>
<BirthDate>1967-12-27</BirthDate>
<RoomNo>111</RoomNo>
<ClientName>AAA BBB CCC</ClientName>
</userinfo>
<userinfo>
<BirthDate>1970-01-01</BirthDate>
<RoomNo>121</RoomNo>
<ClientName>DDD EEE FFF</ClientName>
</userinfo>
</dataroot>';
$xml = simplexml_load_string( $string );
if( $user = $xml->xpath( '//userinfo[RoomNo="111" and BirthDate="1967-12-27"]' ) )
{
var_dump( (string) $user[0]->ClientName );
// string(11) "AAA BBB CCC"
}