我正在尝试使用PHP从XML中读取一些值。但问题是我想用变量名读取它们,如下所示 -
<?php
$xml = '<RESPONSE type=\"WEBRESPONSE\">
<NAME>ALBERT</NAME>
<HTTPSTATUS>200</HTTPSTATUS>
<VALUES>
<VALUE1>ABCD</VALUE1>
<VALUE2>DEFG</VALUE2>
<VALUE3>HIJK</VALUE3>
</VALUES>
</RESPONSE>';
$responseMappings = array(
"VALUE1","VALUE2","VALUE3"
);
$xmlResponse = @simplexml_load_string ( $xml );
$values = $xmlResponse->VALUES;
foreach($responseMappings as $responseMapping){
//Refer using variable name below? How to?
echo "Value : ".($values->${responseMapping});
}
?>
我想使用$responseMapping
中的变量名$values
来引用或选择值。我不知道如何做到这一点。
非常感谢任何帮助。
答案 0 :(得分:1)
你很接近,但有两件事:
首先,XML无效(看起来就像是一些拼写错误,因为你没有检查过你的示例代码)。在您禁止这些错误时,您需要启用错误记录并跟踪错误日志。解决这些问题。
然后修复如何进行变量属性访问,(a)正确的代码是:
echo "Value : " . $values->{$responseMapping};
示例性输出:
Value : ABCD
Value : DEFG
Value : HIJK
查看完整的工作代码示例(run it as an online demo):
<?php
/**
* @link http://stackoverflow.com/a/24116883/367456
* @link https://eval.in/160030
*/
$xml = '
<RESPONSE type="WEBRESPONSE">
<NAME>ALBERT</NAME>
<HTTPSTATUS>200</HTTPSTATUS>
<VALUES>
<VALUE1>ABCD</VALUE1>
<VALUE2>DEFG</VALUE2>
<VALUE3>HIJK</VALUE3>
</VALUES>
</RESPONSE>';
$responseMappings = array(
"VALUE1", "VALUE2", "VALUE3"
);
$xmlResponse = simplexml_load_string($xml);
if (!$xmlResponse) {
throw new RuntimeException('Failed to open XML');
}
$values = $xmlResponse->VALUES;
foreach ($responseMappings as $responseMapping) {
//Refer using variable name below? How to?
echo "Value : " . $values->{$responseMapping}, "\n";
}
但如果你允许我一些额外的想法:
认为不同:所有这三个<VALUE?>
元素都有共同之处:它们是<VALUES>
的子元素。这样做你不需要保留元素名称的数组。
该场景的示例性输出:
Value "VALUE1": ABCD
Value "VALUE2": DEFG
Value "VALUE3": HIJK
这是另一个工作代码示例(run it as an online demo):
<?php
/**
* @link http://stackoverflow.com/a/24116883/367456
* @link https://eval.in/160032
*/
$xml = '
<RESPONSE type="WEBRESPONSE">
<NAME>ALBERT</NAME>
<HTTPSTATUS>200</HTTPSTATUS>
<VALUES>
<VALUE1>ABCD</VALUE1>
<VALUE2>DEFG</VALUE2>
<VALUE3>HIJK</VALUE3>
</VALUES>
</RESPONSE>';
$xmlResponse = simplexml_load_string($xml);
if (!$xmlResponse) {
throw new RuntimeException('Failed to open XML');
}
/** @var SimpleXMLElement $values */
$values = $xmlResponse->VALUES;
foreach ($values->children() as $name => $value) {
//Refer using variable name below? How to?
printf("Value \"%s\": %s\n", $name, $value);
}
答案 1 :(得分:0)
您可以尝试以下内容:
$dom = new DOMDocument;
$dom->loadXML($xml);
$books = $dom->getElementsByTagName('VALUE1');
foreach ($books as $book) {
echo $book->nodeValue, PHP_EOL;
}
了解更多信息:http://www.php.net/manual/en/domdocument.getelementsbytagname.php
答案 2 :(得分:0)
替代方案你可以做这样的事情。考虑这个例子:
$xml = '<RESPONSE type="WEBRESPONSE">
<NAME>ALBERT</NAME>
<HTTPSTATUS>200</HTTPSTATUS>
<VALUES>
<VALUE1>ABCD</VALUE1>
<VALUE2>DEFG</VALUE2>
<VALUE3>HIJK</VALUE3>
</VALUES>
</RESPONSE>';
$responseMappings = array("VALUE1","VALUE2","VALUE3");
$data = array();
$xmlResponse = simplexml_load_string($xml);
$xmlResponse = json_decode(json_encode($xmlResponse), true);
foreach($responseMappings as $key => $value) {
$data[$value] = $xmlResponse['VALUES'][$value];
}
echo '<pre>';
print_r($data);
echo '</pre>';
示例输出:
Array
(
[VALUE1] => ABCD
[VALUE2] => DEFG
[VALUE3] => HIJK
)
答案 3 :(得分:0)
尝试使用foreach()
和array_merge()
$xml = '<RESPONSE type="WEBRESPONSE">
<NAME>ALBERT</NAME>
<HTTPSTATUS>200</HTTPSTATUS>
<VALUES>
<VALUE1>ABCD</VALUE1>
<VALUE2>DEFG</VALUE2>
<VALUE3>HIJK</VALUE3>
</VALUES>
</RESPONSE>';
$xmlResponse = simplexml_load_string($xml);
$values = $xmlResponse->VALUES;
$responseMapping = array();
foreach($values as $k => $v){
$responseMappings[$k] = $v;
}
$responseMapping = array_merge($responseMapping,(array)$responseMappings['VALUES']);
print_r($responseMapping); //Array([VALUE1] => ABCD [VALUE2] => DEFG [VALUE3] => HIJK)