FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » xml parsing issue
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
xml parsing issue [message #184698] Sat, 18 January 2014 09:54 Go to next message
Rob B is currently offline  Rob B
Messages: 1
Registered: January 2014
Karma: 0
Junior Member
I feel pretty stupid posting this but I just can't sort it. I have a very short and simple piece of XML and I can't parse it, I have parsed xml in the past without issue but this I just can't sort for some reason.

The XML:

<?xml version="1.0" encoding="UTF-8"?>
<RegionalFcst xmlns="www.metoffice.gov.uk/xml/metoRegionalFcst"
issuedAt="2014-01-18T04:00:00"></RegionalFcst>


A print_r of the simplexml object it is loaded into:

(
[@attributes] => Array
(
[issuedAt] => 2014-01-18T04:00:00
)

)

All I want is that"issuedAt" time stamp, but it's an attribute not an element. I have tried a foreach through @attributes and got nothing. I am even thinking of streaming it through an awk script (hosted on linux). I really would like to avoid that and stay within PHP.

Any help appreciated.

Rob
Re: xml parsing issue [message #184704 is a reply to message #184698] Sat, 18 January 2014 12:18 Go to previous messageGo to next message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma: 0
Member
Rob B <rob(dot)bradford(at)virgin(dot)net> writes:

> I feel pretty stupid posting this but I just can't sort it.

The manual is not very clear on this. I think the problem you are
having is that everything you access is returned as a SimpleXMLElement
object and you want a PHP string.

> I have a
> very short and simple piece of XML and I can't parse it, I have parsed
> xml in the past without issue but this I just can't sort for some
> reason.
>
> The XML:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <RegionalFcst xmlns="www.metoffice.gov.uk/xml/metoRegionalFcst"
> issuedAt="2014-01-18T04:00:00"></RegionalFcst>

print_r((string)$xml['issuedAt']);

<snip>
--
Ben.
Re: xml parsing issue [message #184705 is a reply to message #184698] Sat, 18 January 2014 13:54 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/18/2014 4:54 AM, Rob B wrote:
> I feel pretty stupid posting this but I just can't sort it. I have a very short and simple piece of XML and I can't parse it, I have parsed xml in the past without issue but this I just can't sort for some reason.
>
> The XML:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <RegionalFcst xmlns="www.metoffice.gov.uk/xml/metoRegionalFcst"
> issuedAt="2014-01-18T04:00:00"></RegionalFcst>
>
>
> A print_r of the simplexml object it is loaded into:
>
> (
> [@attributes] => Array
> (
> [issuedAt] => 2014-01-18T04:00:00
> )
>
> )
>
> All I want is that"issuedAt" time stamp, but it's an attribute not an element. I have tried a foreach through @attributes and got nothing. I am even thinking of streaming it through an awk script (hosted on linux). I really would like to avoid that and stay within PHP.
>
> Any help appreciated.
>
> Rob
>

Rob,

You're close, your simplexml object is but it is an object, not an
array. You have to call the attributes() function to get the array
of attributes, i.e.

foreach ($xml->attributes() as $name => $value)
echo $name . '="' . $value ."\"\n";

You can access just the 'issuedAt' attribute with

$name = 'issuedAt';
$value = $xml->attributes()->$name;

(Note: when using a non-default namespace you will have to specify the
namespace you're using, i.e.

$xml->RegionalFcst[0]->attributes....

Don't try to just cast the object as a string; you will get all kinds of
extra stuff if other attributes are added to your xml object.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: xml parsing issue [message #184708 is a reply to message #184698] Sat, 18 January 2014 14:24 Go to previous message
Rob Bradford is currently offline  Rob Bradford
Messages: 5
Registered: February 2011
Karma: 0
Junior Member
Thanks for the help, it's simple once you know how, isn't that always the case. Anyway I now have what I need and can dump my external call to awk.

Thank you both for the tips and help.

Rob
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: How to locally install Wordpress, Ubuntu 8.10 , XAMPP already installed.
Next Topic: strange parameters...
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 17:13:37 GMT 2024

Total time taken to generate the page: 0.02348 seconds