Accessing XML elements

How do I reference specific xml elements?
Based on a form post, I want to access the values of certain elements,
and echo that result.
The xml file would contain many elements with an tag.
So in the example below, I would like to access the description value where
id=1a:

<task>
	<group>mygroup</group>
	<id>1a</id>
	<description>New forum for mygroup</description>
	<duration>1:00</duration>
	<app>php</app>
	<dependency>Time</dependency>
	<resource>techteam</resource>
	<comments>use this id to get data needed</comments>
</task>	

I am using simplexml_load_file, and then trying to access specific elements based on a form post.

For example:

[php]
$myapps = ($_POST[apps]);
$xml = simplexml_load_file("…/xml/tasks.xml");
if (in_array(‘Example Form Item’,$myapps))
echo $xml->task-> then what?
[/php]

First of all you shouldn’t have brackets around the $_POST variable:

[php]$myapps = ($_POST[apps]);[/php]
Becomes:
[php]$myapps = $_POST[apps];[/php]

And you should also really put the array key as a string:

[php]$myapps = $_POST[‘apps’];[/php]

If you just want to use any value given for a certain post field, e.g. the $_POST[‘apps’] used above, this would work:

[php]echo $xml->$myapps;[/php]

Thanks for your reply. I have corrected the php mistakes that you saw.
But maybe my question could have been phrased better.
Given the current xml example:

<task>
	<group>mygroup</group>
	<id>1a</id>
	<description>New forum for mygroup</description>
	<duration>1:00</duration>
	<app>php</app>
	<dependency>Time</dependency>
	<resource>techteam</resource>
	<comments>use this id to get data needed</comments>
</task>	

How do I extract the value of where = 1a ?

I take it your XML file has more than one element in?

EDIT: Could you post an example file with a couple of the items in?

<multitask>
<task>
	<group>mygroup</group>
	<id>1a</id>
	<description>New forum for mygroup</description>
	<duration>1:00</duration>
	<app>php</app>
	<dependency>Time</dependency>
	<resource>techteam</resource>
	<comments>use this id to get data needed</comments>
</task>	

<task>
	<group>yourgroup</group>
	<id>2a</id>
	<description>Work items to do</description>
	<duration>1:00</duration>
	<app>mysql</app>
	<dependency>1a</dependency>
	<resource>techteam</resource>
	<comments>use this id to get data needed</comments>
</task>	

<task>
	<group>mygroup</group>
	<id>3a</id>
	<description>add team members</description>
	<duration>1:00</duration>
	<app>textpad</app>
	<dependency>2a</dependency>
	<resource>techteam</resource>
	<comments>under cheesing is rampant</comments>
</task>	

</multitask>

Does this help or would you need more?

Try something similar to:

[php]$raw_xml = simplexml_load_file("…/xml/tasks.xml");

foreach($raw_xml as $task) {
if($task->id == ‘THE ID YOU’RE LOOKING FOR’) {
echo $task->description;
}
}[/php]

Thank you. That should work just fine.

DateTime add question.

From this same xml, I want to add the value in the duration element to a form posted date.
This form posted date is composed of 2 items, a date and time variable that need to be combined into one date item, then added to the duration element.
I am working with the “.” concatentination operator, but I have not yet found the correct way to combine the elements into a date, and add the duration element(from the xml) to that date.

For example, I have tried multiple variations of the code below, but have not reached the desired result:

[php]
$xml = simplexml_load_file("…/xml/my_tasks.xml");
$startdate = $_POST[‘startdate’];
$starttime = $_POST[‘starttime’];

$taskdate = date(“y-m-d H:i”, $startdate . $starttime);
$dur = date(‘H:i’,strtotime($xml->task[0]->duration));
$sum = date(‘H:i’, $starttime) + date(‘H:i’,$dur);

[/php]

Any help would be greatly appreciated.

Sponsor our Newsletter | Privacy Policy | Terms of Service