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
https://forums.phpfreaks.com/topic/229973-use-of-minus-with-xml/
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"  :-*

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.