Hi,

I am hoping someone can help me with this.

I am fairly new to XML parsing so XML parsing and I have only dealt with fairly simple parsing so far.

I am currently stuck with the following problem...

I have XML with this structure: -

<members>
	<member>
		<name>David Smith</name>
		<information>
			<description>home telephone</description>
			<given_information>
				<details>0123465987</details>
			</given_information>
		</information>
		<information>
			<description>mobile telephone</description>
			<given_information>
				<details>056142856987</details>
			</given_information>
		</information>
		<information>
			<description>email address</description>
			<given_information>
				<details>d.smith@yahoo.com</details>
			</given_information>
		</information>
	</member>
</members>

I am using simplexml & xpath like this: -

$XML_load =  simplexml_load_file('members.xml');
foreach ($XML_load->members->xpath('//member') as $member)
{
$member_name = $member->name;
}

The problem I have is that I dont know how to get a variable from the <details> element based on the <description> element. So I need to be able to create the variables $mobile_number, $home_number and $email_address accurately based on the information in the ancestor element of <description>.

Can anyone help me with this? Any advice would be greatly appreciated.

I hope I have explained the problem well enough and my terminology is correct but please forgive me if it is not as I am still a newbie at this.

BTW is using simplexml/xpath like this the best way to do it? I have heard xmlDOM is better but I cant seem to find any resources related to that which would help me with the problem above.

Thanks in advance,

NDF

    4 days later

    First of all, your xpath is needs to be "/members/member'. Anyway, you could do

    foreach ($XML_load->members->xpath('/members/member') as $member)
    {
    $member_name = $member->name;
    foreach($member->information as $info)
    {
    $$info->description = $info->given_information->details;
    }
    }

    This will loop through all the information, creating a variable of each description name, and assigning it the details element of the same parent information element.

    This code is untested, but should be working or close to working.

    Lastly, I don't know what the performance differences are between simple_xml and xmlDom, however if I remember correct, xmlDom is deprecated so you should probably use simple_xml to keep your code working with newer version of PHP

      Write a Reply...