Jump to content

use of minus with xml


ukweb

Recommended Posts

Hi

 

I'm using SimpleXMLElement to work with a supplied XML file, problem is with the way the XML document is formatted. The document uses '-' (minus) in the element names instead of _ so when I go to read the values, nothing is returned:

$developer->developer-region; //returns nothing

 

However if I change the sample XML document that I've been supplied to <developer_region></developer_region> and do

$developer->developer_region; //returns child elements

 

This is not practical to manually change the document, is there a way to use '-' in the xml document?

 

Thanks in advance

Ste

 

Link to comment
Share on other sites

Now that I know you've read the manual (:)) I'll copy the answer into here for any future visitors having the same issue (if you do, go read the manual page!).

 

Accessing elements within an XML document that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

 

Example #3 Getting <line>

<?php
include 'example.php';

$xml = new SimpleXMLElement($xmlstr);

echo $xml->movie->{'great-lines'}->line;
?>

 

The above example will output:

 

PHP solves all my web problems

 

P.S. Don't forget to mark the thread as "solved"  :-*

Link to comment
Share on other sites

It would have been nice to announce this sort of thing; mostly to show that the place is being looked after, but also to celebrate the awesome new features available (are there any?). :)

 

P.S. Apologies for wandering off topic!

Link to comment
Share on other sites

The forum upgrade was announced here

 

Urgh, why make it in entirely the wrong forum? I guess it slipped under my radar (was on vacation :)) and I would never have thought to look in there for an announcement!

Because I think he was looking for Questions, Comments, and Suggestions for the new upgrade. :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.