Accessing arrays with : in their names. How?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ubergrafik
Forum Newbie
Posts: 9
Joined: Wed Aug 15, 2007 1:04 am

Accessing arrays with : in their names. How?

Post by ubergrafik »

I have an XML file that contains an element name as follows:

Code: Select all

<imsmd:langstring>Data in here.</imsmd:langstring>
I am using PHP to extract specific elements from the XML file and display them.

I am trying the following:

Code: Select all

echo $doc->imsmd:lom->CDATA();
The parser chokes on the : between imsmd and lom, which is a required element name in the xml.

The error message is as follows:

Parse error: parse error, expecting `','' or `';'' in *\myPhp.php on line *

(I have edited out the file path and line number in this example here)

Thanks.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I would recommend that you do a:

Code: Select all

echo '<pre>' . print_r($doc, 1) . '</pre>';
to find out what the elements in the object are actually named.
(#10850)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

With php5's simplexml extension you can e.g. access the data like this

Code: Select all

$manifest = simplexml_load_file('http://www.imsglobal.org/question/qti_item_v2p0pd/Examples/cpMDExample.xml');

$imsmd =  $manifest->resources->resource->metadata->children('http://www.imsglobal.org/xsd/imsmd_v1p2');
echo $imsmd[0]->general->identifier;
see http://de2.php.net/simplexml-element-children
ubergrafik
Forum Newbie
Posts: 9
Joined: Wed Aug 15, 2007 1:04 am

Post by ubergrafik »

Thanks for the replys. I should have mentioned that I am using php4...

arborint, when i do this, I see a blank page.

volka, unfortunately I am using php4.
ubergrafik
Forum Newbie
Posts: 9
Joined: Wed Aug 15, 2007 1:04 am

Post by ubergrafik »

It's not accessing the elements that's the problem, it's the way they are named. I have googled and found nothing like this. I have tried:

Code: Select all

echo $doc->imsmd.':'.lom->CDATA();

echo $doc->imsmd/lom->CDATA();

echo $doc->imsmd./.lom->CDATA();
Each time I get the same error:
Parse error: parse error, expecting `','' or `';'' in *\myPhp.php on line *
OK, I think that attempting to access the nodes numerically somehow might prove more beneficial. Trying that...
ubergrafik
Forum Newbie
Posts: 9
Joined: Wed Aug 15, 2007 1:04 am

Post by ubergrafik »

I've been playing around with this again and still no joy. I have changed my code to the following:

Code: Select all

<?php

$file = $_FILES['userfile']['tmp_name'];
$xml_str = file_get_contents ($file);
echo 'xml_str: <br><br>'.$xml_str;

//==============================================
//ITEMS
//==============================================
//create items list
$orgs_str = get_unique_element($xml_str,'organizations');
$items = get_node_strings($orgs_str,'item');
$items = process_items($items);

echo '<br>'.$items[0]['identifierref'];

?>
If i have line 15 in my code like this:

Code: Select all

$orgs_str = get_unique_element($xml_str,'organizations');
I get data from my xml file, as expected. However, if I access a node that has a colon in it like this:

Code: Select all

$orgs_str = get_unique_element($xml_str,'organizations:test');
I get the following error;
Notice: Undefined offset: 0 in ...ims_menu.php on line 15

line 15 being where I am using the colon. Is there any way I can wrap that character so that it won't be treated as a break of some sort, and the string treated as a whole?

Thanks
ubergrafik
Forum Newbie
Posts: 9
Joined: Wed Aug 15, 2007 1:04 am

Post by ubergrafik »

Has any one got any ideas? I'm really stumped...

:B
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

You're dealing with something called XML namespaces

Check out: http://ca3.php.net/manual/en/function.xpath-eval.php (especially the comments)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Variable-variables would have hints on how to do this directly....

http://php.net/language.variables.variable
Post Reply