cainy1982;10986020 wrote:
PHP reads teh XML and extracts the field <name>Fairhaven</name> and displays it in a page.
You only need to compare each postcode (assuming there is such a field) until you find the right one. I'd also wrap this in a function so that the entire xml element containing name, postcode etc is retrieved.
<?php
$xml = simplexml_load_file("test.xml");
function getElementByPostcode($xml, $code)
{
foreach($xml->children() as $child)
{
if ($child->getPostcode() == $code)
return $child;
}
return false;
}
if ($el = getElementByPostcode($xml, $code_input))
echo $el->getName();
else
printf('No node found for postcode %s', $code_input);
But you might want to look into DOMDocument instead which lets you use XPath, or DOMXPath as the actual class is called in this case.
$els = $xp->query(
sprintf('//postcode[text()="%s"]/parent::node()', $code_input)
);
Where $xp is a DOMXpath object tied to your DOMDocument.
//postcode matches any element called "postcode", regardless of where it is in the hierarchy, [text()="..."] further restricts this matching to postcode nodes for which the text matches what is given, and finally /parent::node() selects the parent node of all postcode elements where the text matched.
That's what I'm guessing your after anyway.