0

I'm trying to echo a specific element's data from an XML file containing all the elements and their data. Here's my code:

$xml = simplexml_load_file("http://cslab.bc.edu/~cs254/data/periodic.xml");
$atom = $_GET['selectAtom'];
$symbol = $xml->$atom->SYMBOL;
echo $symbol;

The $atom variable returns the name of the atom after a user has submitted a form. I would like this php code to echo the symbol of the atom selected.

2 Answers 2

1

You can run XPath query to find it easily. Here's how:

$xml = simplexml_load_file("http://cslab.bc.edu/~cs254/data/periodic.xml");
$atom = 'Actinium';
$symbol = $xml->xpath("//ATOM[NAME='$atom']/SYMBOL");
echo $symbol[0];

Example in codepad.

Sign up to request clarification or add additional context in comments.

Comments

0

Try

$symbol = $xml->{$atom}->SYMBOL;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.