簡體   English   中英

XML:如何從php的孫子的值中獲取父級的值

[英]XML: How to get the value of a parent from the value of a grand-child in php

我需要使用PHP 獲取Company 3的帳號 ...

感謝您提供所有幫助...我知道我需要根據代碼創建一個變量。 我想我可以使用XPath,但我不知道該怎么做。 謝謝

這是我的XML響應:

<?xml version="1.0" encoding="UTF-8"?>
<GetAccountsWithAccessResponse>
    <Result>Success</Result>
    <Accounts>
        <Account>
            <AccountNumber>23871</AccountNumber>
            <Contact_id>10135</Contact_id>
            <IsOwner>N</IsOwner>
            <Groups>
                <Group>
                    <GroupID>12</GroupID>
                    <GroupName>Company 1</GroupName>
                    <GroupType>Private</GroupType>
                    <IncludeLoginLink>Yes</IncludeLoginLink>
                </Group>
                <Group>
                    <GroupID>28</GroupID>
                    <GroupName>Partners Group</GroupName>
                    <GroupType>Hidden</GroupType>
                    <IncludeLoginLink>No</IncludeLoginLink>
                </Group>
            </Groups>
        </Account>
        <Account>
            <AccountNumber>45160</AccountNumber>
            <Contact_id>0</Contact_id>
            <IsOwner>Y</IsOwner>
            <Groups>
                <Group>
                    <GroupID>1</GroupID>
                    <GroupName>Company 2</GroupName>
                    <GroupType>Private</GroupType>
                    <IncludeLoginLink>No</IncludeLoginLink>
                </Group>
                <Group>
                    <GroupID>2</GroupID>
                    <GroupName>Support</GroupName>
                    <GroupType>Private</GroupType>
                    <IncludeLoginLink>No</IncludeLoginLink>
                </Group>
            </Groups>
            <NumberOfContacts>10</NumberOfContacts>
            <MaximumContacts>2500</MaximumContacts>
        </Account>
        <Account>
            <AccountNumber>45166</AccountNumber>
            <Contact_id>6</Contact_id>
            <IsOwner>N</IsOwner>
            <Groups>
                <Group>
                    <GroupID>3</GroupID>
                    <GroupName>Company 3</GroupName>
                    <GroupType>Hidden</GroupType>
                    <IncludeLoginLink>No</IncludeLoginLink>
                </Group>
            </Groups>
            <NumberOfContacts>7569</NumberOfContacts>
            <MaximumContacts>10000</MaximumContacts>
        </Account>
    </Accounts>
</GetAccountsWithAccessResponse>

感謝您提供所有幫助...我知道我需要根據代碼創建一個變量。 我想我可以使用XPath,但我不知道該怎么做。 謝謝

使用XPath。 您可以指定...

//Account[Groups/Group/GroupName="Company 3"]/AccountNumber

這表示要查找具有GroupName和所需值的AccountNumber元素。

使用[]可以讓您有條件選擇后面的元素,但結果元素是[之前的元素。 因此,其結果將是Account元素,然后最后的/AccountNumber說在Account元素中選擇AccountNumber元素。 這使...

<AccountNumber>45166</AccountNumber>

PHP的DOM擴展提供了DOMXpath類。 通過其DOMXpath::evaluate()您可以獲取節點和值。

$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);

var_dump($xpath->evaluate('string(//Account[Groups/Group/GroupName="Company 3"]/AccountNumber)'));

輸出:

string(5) "45166"

Xpath表達式

  • 文檔中的任何Account元素節點...
    //Account
  • ...其Groups/Group/GroupName的值為Company 3 ...
    //Account[Groups/Group/GroupName="Company 3"]
  • ...其AccountNumber子元素節點...
    //Account[Groups/Group/GroupName="Company 3"]/AccountNumber
  • ...作為字符串:
    string(//Account[Groups/Group/GroupName="Company 3"]/AccountNumber)

string()將返回列表中第一個節點的文本內容,如果未找到任何節點,則返回一個空字符串。

這是我最終想出的:

$GroupName = "Company 3";

$xmlstr = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<GetAccountsWithAccessResponse>
    <Result>Success</Result>
    <Accounts>
        <Account>
            <AccountNumber>23871</AccountNumber>
            <Contact_id>10135</Contact_id>
            <IsOwner>N</IsOwner>
            <Groups>
                <Group>
                    <GroupID>12</GroupID>
                    <GroupName>Company 1</GroupName>
                    <GroupType>Private</GroupType>
                    <IncludeLoginLink>Yes</IncludeLoginLink>
                </Group>
                <Group>
                    <GroupID>28</GroupID>
                    <GroupName>Partners Group</GroupName>
                    <GroupType>Hidden</GroupType>
                    <IncludeLoginLink>No</IncludeLoginLink>
                </Group>
            </Groups>
        </Account>
        <Account>
            <AccountNumber>45160</AccountNumber>
            <Contact_id>0</Contact_id>
            <IsOwner>Y</IsOwner>
            <Groups>
                <Group>
                    <GroupID>1</GroupID>
                    <GroupName>Company 2</GroupName>
                    <GroupType>Private</GroupType>
                    <IncludeLoginLink>No</IncludeLoginLink>
                </Group>
                <Group>
                    <GroupID>2</GroupID>
                    <GroupName>Support</GroupName>
                    <GroupType>Private</GroupType>
                    <IncludeLoginLink>No</IncludeLoginLink>
                </Group>
            </Groups>
            <NumberOfContacts>10</NumberOfContacts>
            <MaximumContacts>2500</MaximumContacts>
        </Account>
        <Account>
            <AccountNumber>45166</AccountNumber>
            <Contact_id>6</Contact_id>
            <IsOwner>N</IsOwner>
            <Groups>
                <Group>
                    <GroupID>3</GroupID>
                    <GroupName>Company 3</GroupName>
                    <GroupType>Hidden</GroupType>
                    <IncludeLoginLink>No</IncludeLoginLink>
                </Group>
            </Groups>
            <NumberOfContacts>7569</NumberOfContacts>
            <MaximumContacts>10000</MaximumContacts>
        </Account>
    </Accounts>
</GetAccountsWithAccessResponse>
XML;

$xmlstring = new SimpleXMLElement($xmlstr);

//This Works for the GroupID:
$IDS = $xmlstring->xpath('//Group[GroupName="' . $GroupName . '"]/GroupID');
foreach($IDS as $theID) {
    $GroupID = $theID;
}

echo "The Group ID is $GroupID \n\n";

//This works for the Account Number
$acctnumbers = $xmlstring->xpath('//Account[Groups/Group/GroupName="' . $GroupName . '"]/AccountNumber');
    /* The following xpath also works:  $acctnumbers = $xmlstring->xpath('//Group[GroupName="' . $GroupName . '"]/parent::Groups/parent::Account/AccountNumber');  */

foreach($acctnumbers as $number) {
    $AccountNumber = $number;
}

echo "The account number is $AccountNumber";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM