Xpath and conditionally selecting descendants base

2019-07-16 07:28发布

问题:

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 element based on the 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 .

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!

回答1:

Try this for a start:

<?php
$dom = new DOMDocument();
$dom->load('yourXmlFile.xml');

/*$dom->loadXml('<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>');*/

$xpath = new DOMXPath($dom);

$members = array();
foreach ($xpath->query('//member') as $memberNode) {
    $nameNode = $xpath->query('name', $memberNode);
    if ($nameNode->length > 0) {
        $name = $nameNode->item(0)->nodeValue;
        $members[$name] = array();

        foreach ($xpath->query('information', $memberNode) as $informationNode) {
            $descriptionNode = $xpath->query('description', $informationNode);
            $givenInformationDetailsNode = $xpath->query('given_information/details', $informationNode);

            if ($descriptionNode->length > 0 && $givenInformationDetailsNode->length > 0) {
                $members[$name][$descriptionNode->item(0)->nodeValue] = $givenInformationDetailsNode->item(0)->nodeValue;
            }
        }
    }
} 

var_dump($members);