-1

It IS a duplicate, just not for the question that is was closed for. Finally found the answer, even here on SO. Actual Duplicate: PHP SimpleXML Namespace Problem

EDIT: If you read the question closely, you will see it is NOT a duplicate of PHP namespace simplexml problems. The answer from the 'possible duplicate' is not the answer to my question.

Again:
I have no problem with $value = $record->children('cap', true)->$title;.(which is all the 'possible duplicate' answers)
I have a problem when there are other tags inside the tag with the colon.

<tag:something>hello</tag:something> //I parse out hello (this is the 'duplicate questions' answer that I don't need answered)

<tag:something>
 <stuff>hello</stuff> //I cannot grab this. Explanation below.
</tag:something>

END of edit.

ORIGINAL question:
I cannot get the data inside the tag <value> in the XML located at http://alerts.weather.gov/cap/us.php?x=1 (sample of XML below).

The problem is at:

$array[] = $record->children($tag_cap, true)->$tag_geocode->$tag_value;

This is the only data I cannot grab, I have verified that all the other data other than $array[4] is grabbed.

There is just a problem getting data from tags when the parent tag is in the form <cap:something>. For example:

I can get 100 when it is like <cap:something>100</cap:something>. But I cant get 100 if it was like <cap:something><value>100</value></cap:something>.

Piece of the XML:

<?xml version = '1.0' encoding = 'UTF-8' standalone = 'yes'?>

<feed
xmlns = 'http://www.w3.org/2005/Atom'
xmlns:cap = 'urn:oasis:names:tc:emergency:cap:1.1'
xmlns:ha = 'http://www.alerting.net/namespace/index_1.0'
>
<!-- http-date = Tue, 30 Oct 2012 06:34:00 GMT -->

<id>http://alerts.weather.gov/cap/us.atom</id>
<logo>http://alerts.weather.gov/images/xml_logo.gif</logo>
<generator>NWS CAP Server</generator>
<updated>2012-10-30T14:34:00-04:00</updated>
<author>
<name>w-nws.webmaster@noaa.gov</name>
</author>
<title>Current Watches, Warnings and Advisories for the United States Issued by the    National Weather Service</title>
<link href='http://alerts.weather.gov/cap/us.atom'/>

<entry>
<id>http://alerts.weather.gov/cap/wwacapget.php?x=AK124CCADA8120.BlizzardWarning.124CCAE7BFC0AK.AFGWSWNSB.d32adb45b5c82ec5e486c4cfb96d3fb6</id>
<updated>2012-10-30T05:20:00-08:00</updated>
<published>2012-10-30T05:20:00-08:00</published>
<author>
<name>w-nws.webmaster@noaa.gov</name>
</author>
<title>Blizzard Warning issued October 30 at 5:20AM AKDT until October 31 at 6:00AM AKDT by NWS</title>
<link href='http://alerts.weather.gov/cap/wwacapget.php?x=AK124CCADA8120.BlizzardWarning.124CCAE7BFC0AK.AFGWSWNSB.d32adb45b5c82ec5e486c4cfb96d3fb6'/>
<summary>...BLIZZARD WARNING IN EFFECT UNTIL 6 AM AKDT WEDNESDAY... THE NATIONAL WEATHER SERVICE IN FAIRBANKS HAS ISSUED A BLIZZARD WARNING...WHICH IS IN EFFECT UNTIL 6 AM AKDT WEDNESDAY. * VISIBILITY...NEAR ZERO IN SNOW AND BLOWING SNOW. * WINDS...WEST 35 MPH GUSTING TO 50 MPH. * SNOW...ACCUMULATION 3 INCHES THROUGH TONIGHT.</summary>
<cap:event>Blizzard Warning</cap:event>
<cap:effective>2012-10-30T05:20:00-08:00</cap:effective>
<cap:expires>2012-10-30T16:00:00-08:00</cap:expires>
<cap:status>Actual</cap:status>
<cap:msgType>Alert</cap:msgType>
<cap:category>Met</cap:category>
<cap:urgency>Expected</cap:urgency>
<cap:severity>Severe</cap:severity>
<cap:certainty>Likely</cap:certainty>
<cap:areaDesc>Eastern Beaufort Sea Coast</cap:areaDesc>
<cap:polygon></cap:polygon>
<cap:geocode>
<valueName>FIPS6</valueName>
<value>002185</value>
<valueName>UGC</valueName>
<value>AKZ204</value>
</cap:geocode>
<cap:parameter>
<valueName>VTEC</valueName>
<value>/X.NEW.PAFG.BZ.W.0013.121030T1320Z-121031T1400Z/</value>
</cap:parameter>
</entry>

...//rest of XML...

PHP Code:

ini_set('display_errors','1');

$alert_url = 'http://alerts.weather.gov/cap/us.php?x=1';

$alert_string_xml = file_get_contents($alert_url);

$alert_simple_xml_object = simplexml_load_string($alert_string_xml);

$count = 0;

$tag_entry = 'entry';
$tag_summary = 'summary';
$tag_cap = 'cap';
$tag_event = 'event';
$tag_certainty = 'certainty';
$tag_areaDesc = 'areaDesc';
$tag_geocode = 'geocode';
$tag_value = 'value';

foreach ($alert_simple_xml_object->$tag_entry as $record)
{
    $count++;

    $array = array();
    $array[] = $record->$tag_summary;
    $array[] = $record->children($tag_cap, true)->$tag_event;
    $array[] = $record->children($tag_cap, true)->$tag_certainty;
    $array[] = $record->children($tag_cap, true)->$tag_areaDesc;
    $array[] = $record->children($tag_cap, true)->$tag_geocode->$tag_value;
    //$array[] = $record->children($tag_cap, true)->$tag_geocode->$tag_value[0]; //doesnt work either


    echo $array[4]; //nothing is echoed

}

MOST CURRENT ATTEMPT:
I read more on namespaces and understand them better. I even tried what I thought was a better solution:

//inside the above foreach loop
    $namespaces = $record->getNameSpaces(true);
    $caap = $record->children($namespaces['cap']);
    echo $caap->event; //works (but the first way works too)
    echo $caap->geocode->value; //(STILL does not work. Nothing is echoed)

I don't understand why I cannot grab any data from children tags that have a parent tag that includes a namespace.

Community
  • 1
  • 1
Ryan
  • 1,135
  • 1
  • 13
  • 28
  • 1
    `children()` is not a PHP function. You can not call undefined functions. Your code therefore is just broken. Please correct it so it becomes more clear what you've tried. – hakre Nov 01 '12 at 14:15
  • children is a function of SimpleXML correct? I added some info to the question. – Ryan Nov 01 '12 at 14:24
  • yes it is a method of `SimpleXMLElement`, see http://php.net/simplexmlelement.children – hakre Nov 01 '12 at 14:25
  • That question is not a duplicate. When I want information from something like this info I can get it. But I can't get anything from tags inside of . Such as 100 . I can't get the 100. – Ryan Nov 01 '12 at 14:26
  • You are right, it is not an exact duplicate. But it also is ;) - I've now added an answer that should explain it in all details: http://stackoverflow.com/a/13179256/367456 - with your earlier version this problem did not occur, because the root element does not "count" in the sense that it was not a child-element, but the root, hence `children` was not the right function anyway. That is why I wrote my earlier comment that it should be no problem. After looking closer, it's clear that if not the root element, this is a problem. See the answer for details. – hakre Nov 01 '12 at 14:33

3 Answers3

0

cap:stuff is the root, so you would access the elements as:

$xml = simplexml_load_string($your_xml);
$value_name_0 = $xml->valueName[0];
$value_0 = $xml->value[0];
$value_name_1 = $xml->valueName[1];
$value_1 = $xml->value[1];
doublesharp
  • 26,888
  • 6
  • 52
  • 73
0

You are probably looking for this function. There are 2 examples, which should be enough to solve your problem

Darvex
  • 3,624
  • 2
  • 23
  • 39
0

The problem you are facing is not that visible if you have errors and warnings disabled:

namespace error : Namespace prefix cap on stuff is not defined

If you would have errors enable you would see that message. Because simplexml is not able to parse the namespace prefix cap properly, it will be dropped.

Therefore you access it directly:

$xml->stuff->value[1]

And similar. Consider the following code-example (demo:

$xml = simplexml_load_string('<entry>
 <cap:stuff>
  <valueName>aaa</valueName>
  <value>000</value>
  <valueName>bbb</valueName>
  <value>111</value>
 </cap:stuff>
</entry>');

echo "\nResult:", $xml->stuff->value[1], "\n\n";
echo "XML:\n", $xml->asXML();

It demonstrates the error message as well what is in $xml after loading the XML string by outputting it:

Warning: simplexml_load_string(): namespace error : Namespace prefix cap on \ 
                                  stuff is not defined on line 10
Warning: simplexml_load_string():  <cap:stuff> on line 10
Warning: simplexml_load_string():            ^ on line 10

Result:111

XML:
<?xml version="1.0"?>
<entry>
 <stuff>
  <valueName>aaa</valueName>
  <value>000</value>
  <valueName>bbb</valueName>
  <value>111</value>
 </stuff>
</entry>

If you smell that something should work but it isn't, it is always necessary to look closer. One option is to echo the string again as XML to see what simplexml has parsed, the other is to enable error reporting and looking for warnings and error, they often contain further information.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • I have errors on, and I do not get that error. I will update my question again will exactly what I am doing. I tried to make the question easy to understand, it must have actually made it harder to answer. – Ryan Nov 01 '12 at 14:43
  • Yes, it's not easy to write a good question, indeed. **Edit:** For error reporting you're missing an `error_reporting(~0);` so you see all warnings and notices. – hakre Nov 01 '12 at 14:53
  • Question refined. Your answer does not work. I do not get any errors when printing `echo $alert_simple_xml_object->asXML();` even when adding `error_reporting(~0);`. Accessing it directly results in nothing when `echo $array[4]` – Ryan Nov 02 '12 at 19:43