我需要使用PHP获取公司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,但我不知道该怎么做。谢谢
答案 0 :(得分:0)
使用XPath。您可以指定...
//Account[Groups/Group/GroupName="Company 3"]/AccountNumber
这表示要查找一个具有GroupName和所需值的AccountNumber元素。
使用[]
可以给您一个条件,以选择之后的元素,但是result元素是[
之前的元素。因此,其结果将是Account元素,然后最后的/AccountNumber
说在Account元素中选择AccountNumber元素。这给...
<AccountNumber>45166</AccountNumber>
答案 1 :(得分:0)
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"
Account
元素节点... //Account
Groups/Group/GroupName
的{{1}} ... Company 3
//Account[Groups/Group/GroupName="Company 3"]
子元素节点... AccountNumber
//Account[Groups/Group/GroupName="Company 3"]/AccountNumber
string(//Account[Groups/Group/GroupName="Company 3"]/AccountNumber)
将返回列表中第一个节点的文本内容,如果找不到节点,则返回一个空字符串。
答案 2 :(得分:0)
这是我最终想出的:
$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";