Page 1 of 1

Accessing arrays with : in their names. How?

Posted: Wed Aug 15, 2007 1:14 am
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.

Posted: Wed Aug 15, 2007 1:19 am
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.

Posted: Wed Aug 15, 2007 8:01 am
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

Posted: Sun Aug 19, 2007 8:51 pm
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.

Posted: Mon Aug 20, 2007 12:53 am
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...

Posted: Mon Oct 15, 2007 12:34 am
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

Posted: Tue Oct 16, 2007 6:24 pm
by ubergrafik
Has any one got any ideas? I'm really stumped...

:B

Posted: Tue Oct 16, 2007 11:16 pm
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)

Posted: Mon Oct 22, 2007 3:01 am
by feyd
Variable-variables would have hints on how to do this directly....

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