0

How can I return the '3' in:

<element type="forecast_icon_code">3</element>

from this external xml:

ftp://ftp2.bom.gov.au/anon/gen/fwo/IDV10450.xml

Here is my function:

function state($Sitelink) {

$url = $Sitelink;

$file = $url;
if(!$xml = simplexml_load_file($file))
exit('Failed to open '.$file);

$result = $xml->forecast->forcast-period->element;
print_r($result);
}

Calling the function using:

state("ftp://ftp2.bom.gov.au/anon/gen/fwo/IDV10450.xml")

2 Answers 2

1

You need some xpath action.

How extract XML data using PHP

Has an example using the same XML data that should give you a start.

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

Comments

0

I found that SimpleXMLElement has some trouble parsing elements with hyphens in the middle like you have above in "forcast-period".

I found myself in a similar situation and solved it using xpath.

Refer to the following resource and check usage for SimpleXMLElement::xpath():

SimpleXMLElement xpath doc

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.