假设我从mysql表中获取此数据,并将其存储在变量$xmlFromMysqlRow
<?xml version="1.0" encoding="utf-8"?>
<GridInstantMessage xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<fromAgentID>d8445b4a-4ed0-4449-bf33-7709443f7f84</fromAgentID>
<fromAgentName>FABIODK TOPAZ</fromAgentName>
<toAgentID>70d0f6b8-237d-4863-9aef-96e2c93d5130</toAgentID>
<dialog>32</dialog>
<fromGroup>true</fromGroup>
<message>festa|SPLODERS E PREMIOS PRAS 3 MELHORES ROUPAS, O SPLODER MAIOR VAI ESTAR COM 500 AV$ FIXOS AT� O FIM DA FESTA.
A PARTIR DAS 20:00 HORAS.
</message>
<imSessionID>94809bcd-39c3-4e4d-8405-8829a00ba8ea</imSessionID>
<offline>0</offline>
<Position>
<X>0</X>
<Y>0</Y>
<Z>0</Z>
</Position>
<binaryBucket>AADYRFtKTtBESb8zdwlEP3+EAA==</binaryBucket>
<ParentEstateID>0</ParentEstateID>
<RegionID>00000000-0000-0000-0000-000000000000</RegionID>
<timestamp>1437412707</timestamp>
</GridInstantMessage>
如何将此xml的每个部分存储到自己的变量中? 我希望所有内容都存储在
中<message>
</message>
要存储在$message
我该怎么做?
答案 0 :(得分:1)
使用适当的XML解析器可以稳健,轻松地提取XML文档的任何部分。这是使用SimpleXML
的一个示例:
$string = <<<XML
<GridInstantMessage xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<fromAgentID>d8445b4a-4ed0-4449-bf33-7709443f7f84</fromAgentID>
<fromAgentName>FABIODK TOPAZ</fromAgentName>
<toAgentID>70d0f6b8-237d-4863-9aef-96e2c93d5130</toAgentID>
<dialog>32</dialog>
<fromGroup>true</fromGroup>
<message>festa|SPLODERS E PREMIOS PRAS 3 MELHORES ROUPAS, O SPLODER MAIOR VAI ESTAR COM 500 AV$ FIXOS AT� O FIM DA FESTA.
A PARTIR DAS 20:00 HORAS.
</message>
<imSessionID>94809bcd-39c3-4e4d-8405-8829a00ba8ea</imSessionID>
<offline>0</offline>
<Position>
<X>0</X>
<Y>0</Y>
<Z>0</Z>
</Position>
<binaryBucket>AADYRFtKTtBESb8zdwlEP3+EAA==</binaryBucket>
<ParentEstateID>0</ParentEstateID>
<RegionID>00000000-0000-0000-0000-000000000000</RegionID>
<timestamp>1437412707</timestamp>
</GridInstantMessage>
XML;
$xml = new SimpleXMLElement($string);
$message = $xml->xpath("//message")[0]->asXML();
echo $message;
输出
<message>festa|SPLODERS E PREMIOS PRAS 3 MELHORES ROUPAS, O SPLODER MAIOR VAI ESTAR COM 500 AV$ FIXOS AT� O FIM DA FESTA.
A PARTIR DAS 20:00 HORAS.
</message>
答案 1 :(得分:-1)
preg_match('#<message>(.*?)</message>#sui', $xmlFromMysqlRow, $message);
$message = $message[1];