我有这个XML
<parent>
<sms>
<response>
<message>message</message>
<reponse>text<response>
</response>
</sms>
<sms>
<response>
<message>message</message>
<reponse>text2<response>
</response>
</sms>
</parent>
我想获得响应标记的全部内容,即<reponse1>text<response1>
,<reponse2>text<response2>
。我将在数组中进行strore。我试过的是什么
function xmlSplitUpResponseXML($xmlvalue)
{
$returnSplit = array();
$r = 0;
if(simplexml_load_string($xmlvalue))
{
$xml = simplexml_load_string($xmlvalue);
foreach($xml->children() as $child)
{
if(strtolower($child->getName())=='sms')
{
foreach ($child as $fields):
if(strtolower($fields->getName())=='response')
{
echo $fields;
}
}
} }
}
但文字没有回应。 我怎么能这样做。
答案 0 :(得分:0)
你的XML不完全正确,但修复后 - 试试这个:
echo $fields->message."<br>";
echo $fields->response."<hr>";
使用此修复的xml:
,对您的给定示例起作用$string = "<parent><sms><response><message>message</message><response>text</response></response></sms><sms><response><message>message</message><response>text2</response></response></sms></parent>";
答案 1 :(得分:0)
给出以下修复的XML(注释嵌套响应节点)
<parent>
<sms>
<response>
<message>message</message>
<response>text</response>
</response>
</sms>
<sms>
<response>
<message>message</message>
<response>text2</response>
</response>
</sms>
</parent>
以下PHP将为您完成。
<?php
$xml = simplexml_load_file( "sms.xml" );
// Find the sms response (note responses are nested in a parent response for some reason
$result = $xml->xpath('/parent/sms/response/response');
while(list( , $node) = each($result)) {
// to just output the value of the node
// echo $node;
// to wrap value in tags *bad idea*
echo $node->asXML();
}
?>
不确定为什么要返回XML字符串,对我来说似乎是一个坏主意。你是做什么的 做输出?可能更好地正确构建XML文档,只是猜测......