SimpleXML PHP Formatting
I am trying to create a page which will show specific information from an XML document. I have run into a snag where I would like to find out the best possible way to do this.
My XML is a large list that is built from a program called Army Builder. I am trying to dynamically pull specific information for each "squad".
Right now, a sample of my code looks like this:
<?PHP
//THIS SECTION FINDS THE SQUAD/UNIT NAME OF THE FIRST ENTITY
foreach($squad->xpath('entity') as $squadSub){
$squadSub开发者_如何学编程Name = stripslashes($squadSub['name']);
$squadSubCount = $squadSub['count'];
foreach($squadSub->xpath('unitstat[@id="WS"]/@value') as $squadSubStat){
$unitWs = $squadSubStat;
}
?>
<tr>
<td><?= $squadSubCount; ?></td><td><?= $squadSubName; ?></td><td><?= $unitWs; ?></td><td><?= $unitBs; ?></td><td><?= $unitSt; ?></td><td><?= $unitTo; ?></td><td><?= $unitWo; ?></td><td><?= $unitIn; ?></td><td><?= $unitAt; ?></td><td><?= $unitLd; ?></td><td><?= $unitaSv; ?></td><td><?= $unitiSv; ?></td><td><?= $unitcSv; ?></td>
</tr>
The line that finds the "unitWS" is what I am asking about. Now, if you follow the entire xpath, it would read //squad/entity[@name='Nameoftheentity']/unitstat[@id="WS"]/@value
and that would give me the unit's WS. I need to pull 8-13 more values from the xml.
I will show you the /unitstat section of the XML:
<unitstat id="iaStruPnt" name="Structure" value="-" />
<unitstat id="iaVdShld" name="Void Shld" value="-" />
<unitstat id="vdrAt" name="At" value="-" />
<unitstat id="vdrBS" name="BS" value="-" />
<unitstat id="vdrMass" name="Mass Point" value="-" />
<unitstat id="vdrSize" name="Size" value="Small" />
<unitstat id="vdrTpt" name="Transport" value="0" />
<unitstat id="vdrVehSv" name="Veh Save" value="-" />
<unitstat id="vdrWS" name="WS" value="-" />
<unitstat id="vdrWpnAP" name="AP" value="-" />
<unitstat id="vdrWpnRang" name="Range" value="-" />
<unitstat id="vdrWpnSh" name="Shots" value="-" />
<unitstat id="vdrWpnStr" name="S" value="0" />
<unitstat id="vdrWpnType" name="Type" value="0" />
<unitstat id="At" name="At" value="2/3" />
<unitstat id="BS" name="BS" value="4" />
<unitstat id="Fam" name="Familiar" value="0" />
<unitstat id="Front" name="FA" value="0" />
<unitstat id="Group" name="Grp" value="" />
<unitstat id="In" name="In" value="4" />
<unitstat id="Ld" name="Ld" value="10" />
<unitstat id="Rear" name="RA" value="0" />
<unitstat id="Save" name="Save" value="2+/5(i)" />
<unitstat id="Side" name="SA" value="0" />
<unitstat id="St" name="St" value="4" />
<unitstat id="To" name="To" value="4" />
<unitstat id="WS" name="WS" value="4" />
<unitstat id="Wo" name="Wo" value="1" />
<unitstat id="iaAmmo" name="Ammo Load" value="0" />
<unitstat id="iaCrew" name="Crew" value="-" />
<unitstat id="iaMinRge" name="Min Rge" value="n/a" />
<unitstat id="iaPowField" name="PowerField" value="-" />
I need to pull the VALUES of At, BS, In, Ld, St, To, Ws , Wo and Save for each dynamically found unit. The only way I see it possible is to type this:
foreach($squadSub->xpath('unitstat[@id="WS"]/@value') as $squadSubStat){
$unitWs = $squadSubStat;
}
for every one of the values and for every one of the sub groups. Is there an easier way to get all of those values from the specific ID's that I need?
I would assume that I should create an array, then call through them?
Please let me know if you need more information!
With the "or" operator?
<?php
$squadSub->xpath('unitstat[@id="WS" or @id="BS" or @id="whatever"]/@value');
edit:
Sorry I misunderstood. Here is my proposition:
<?php
$units = array();
foreach (array('WS', 'BS', 'Save') as $id)
{
if ($tmp = $squadSub->xpath("unitstat[@id='$id']"))
$units[$id] = (string)$tmp[0]['value'];
}
print_r($units);
精彩评论