Reputation: 2608
I am trying to read some values from XML using PHP. But the thing is I want to read them with a variable name as below -
<?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});
}
?>
I would like to refer or pick the values using variable name $responseMapping
from $values
. I am not sure how I could do that.
Any help would be greatly appreciated.
Upvotes: 0
Views: 135
Reputation: 197624
You're close, however two things:
First the XML is invalid (looks just like some typos as you haven't checked your example code in question). As you suppress those errors, you need to enable error logging and track the error log. Fix those.
Then fix how you do variable property access, (a) correct code is:
echo "Value : " . $values->{$responseMapping};
Exemplary output by that:
Value : ABCD
Value : DEFG
Value : HIJK
See the full working code-example (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";
}
But If you allow me some additional thought:
Think different: All those three <VALUE?>
elements have on thing in common: They are a child-elements of <VALUES>
. That done you won't need to keep the array for the element names.
Exemplary output for that scenario:
Value "VALUE1": ABCD
Value "VALUE2": DEFG
Value "VALUE3": HIJK
Here is another workign code-example (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);
}
Upvotes: 1
Reputation: 13728
try with foreach()
and 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)
Upvotes: 0
Reputation: 7948
Alternative you could do something like this. Consider this example:
$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>';
Sample Output:
Array
(
[VALUE1] => ABCD
[VALUE2] => DEFG
[VALUE3] => HIJK
)
Upvotes: 0
Reputation: 16076
You can try something like below:
$dom = new DOMDocument;
$dom->loadXML($xml);
$books = $dom->getElementsByTagName('VALUE1');
foreach ($books as $book) {
echo $book->nodeValue, PHP_EOL;
}
For more info: http://www.php.net/manual/en/domdocument.getelementsbytagname.php
Upvotes: 0