[英]SimpleXML to get specific data from an XML file
I am making a plugin for JomSocial that will display billing information for a logged-in user, based on an XML file. 我正在为JomSocial制作一个插件,该插件将基于XML文件显示已登录用户的账单信息。 I have made good headway creating the plugin, I just cant seem to get the syntax right to create php statements so I can populate data in various places on the page.
我在创建插件方面进展顺利,我似乎无法正确地创建php语句的语法,因此可以在页面上的各个位置填充数据。 Here is the XML file:
这是XML文件:
<Inquiry>
<Billing>
<Version>4.5.1</Version>
<startTime><![CDATA[4/15/2014 11:09 PM]]></startTime>
<endTime><![CDATA[4/15/2014 11:12 PM]]></endTime>
<Date>20140415</Date>
<MemberId ID="0ESING">
<BillingInfo>
<StatementEndDate>20140430</StatementEndDate>
<BillingSubAccount>
</BillingSubAccount>
<BalanceForward>628.32</BalanceForward>
<BalanceDue>372</BalanceDue>
<Payments>-300</Payments>
</MemberId>
<MemberId ID="F00421">
</BillingInfo>
<BillingInfo>
<StatementEndDate>20140430</StatementEndDate>
<BillingSubAccount>
</BillingSubAccount>
<BalanceForward>1158.36</BalanceForward>
<BalanceDue>93.45</BalanceDue>
<Payments>-1158.36</Payments>
Here is the PHP so far: 到目前为止,这是PHP:
$user =& CFactory::getRequestUser();
$cuser = CFactory::getUser();
$owner = CFactory::getUser($row->user->id);
$ptype = $cuser->getProfileType();
$billingid = $owner->getInfo('FIELD_BILLINGID');
$lastname = $owner->getInfo('FIELD_FAMILYNAME');
$uname = $cuser->username;
$memid = $cuser->id;
$name = $cuser->getDisplayName();
$isMine = COwnerHelper::isMine($cuser->id, $user->id);
$config = CFactory::getConfig();
$source = file_get_contents('data/201404.xml');
$xml = new SimpleXMLElement($source);
$balance = $xml->Billing->MemberId->BillingInfo->BalanceDue;
$BalanceForward = $xml->Billing->MemberId->BillingInfo->BalanceForward;
$Payments = $xml->Billing->MemberId->BillingInfo->Payments;
ob_start();
if( $isMine ) {
if($ptype == '2') {
if(strcasecmp($uname, $billingid) == 0) {
Then, in page to call the fields: 然后,在页面中调用字段:
<?php echo "<div>Balance Due: $". $balance ." | Balance Forward: $" . $BalanceForward . " | Payment: $" . $Payments . "</div>"; ?>
This pulls in the first record of the XML file. 这将获取XML文件的第一条记录。 I was trying something like this for hours:
我试了几个小时是这样的:
$source = file_get_contents('data/201404.xml');
$xml = new SimpleXMLElement($source);
$balance = $xml->Billing->MemberId[.$uname.]->BillingInfo->BalanceDue;
$BalanceForward = $xml->Billing->MemberId[.$uname.]->BillingInfo->BalanceForward;
$Payments = $xml->Billing->MemberId[.$uname.]->BillingInfo->Payments;
to no avail. 无济于事。 I would like to 'pull' the child node from the XML where the MemberId ID= "yadayada" is equal to the $uname.
我想从MemberId ID =“ yadayada”等于$ uname的XML中“拉”子节点。 I hope I am being clear, this is my first post on Stackoverflow!
我希望我很清楚,这是我关于Stackoverflow的第一篇文章!
Why are you not using simplexml_load_file() instead? 为什么不使用simplexml_load_file()? It saves you the hassle in loading the file manually and putting it in a simplexml_element instead.
它为您省去了手动加载文件并将其放入simplexml_element的麻烦。 Furthermore, I think your issues arrise because the XML file itself is invalid, it need 1 root element but instead seems to contain zero (or multiple, depends on how one would read the file).
此外,我认为您的问题浮出水面,因为XML文件本身是无效的,它需要1个根元素,但似乎包含零个(或多个,取决于一个人如何读取文件)。
Using the square bracket notation accesses the attribute by it's name, so you are asking for member[0ESING]
which isn't right because the attribute is named ID
. 使用方括号表示法可以通过名称访问属性,因此您需要的是
member[0ESING]
,因为该属性名为ID
,所以member[0ESING]
。
You can iterate of the members to find the match like so: 您可以像这样迭代成员以找到匹配项:
foreach($xml->Billing->MemberId as $member){
if($member['ID'] == $uname){
$balance = $member->BillingInfo->BalanceDue;
$BalanceForward = $member->BillingInfo->BalanceForward;
$Payments = $member->BillingInfo->Payments;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.