Jump to content

Extracting specific data from xml


Go to solution Solved by requinix,

Recommended Posts

Hey guys, I have the following xml from which I’d like to extract the <name> followed by the  <level> and <card_id> values from the last <upgrade> element within each <unit> elements

E.g. 

 

name = Infantry, level = 3, card id = 3

name = Bazooka Marine, level = 3, card id = 351

name = Anvil, level = 6, card id = 421



<?xml version="1.0" encoding="UTF-8"?>
<root>
   <unit>
      <id>1</id>
      <name>Infantry</name>
      <picture>basicinfantry.jpg</picture>
      <attack>1</attack>
      <health>2</health>
      <cost>0</cost>
      <rarity>1</rarity>
      <type>1</type>
      <set>1000</set>
      <upgrade>
         <level>2</level>
         <card_id>2</card_id>
         <health>3</health>
      </upgrade>
      <upgrade>
         <level>3</level>
         <picture>basicinfantry_lv3.jpg</picture>
         <card_id>3</card_id>
         <health>5</health>
      </upgrade>
   </unit>
   <unit>
      <id>4</id>
      <name>Bazooka Marine</name>
      <picture>bazookamarine.jpg</picture>
      <attack>2</attack>
      <health>4</health>
      <cost>1</cost>
      <rarity>1</rarity>
      <skill id="pierce" x="1" />
      <type>1</type>
      <set>1000</set>
      <upgrade>
         <level>2</level>
         <card_id>350</card_id>
         <health>5</health>
      </upgrade>
      <upgrade>
         <level>3</level>
         <card_id>351</card_id>
         <health>6</health>
         <skill id="pierce" x="2" />
      </upgrade>
   </unit>
   <unit>
      <id>416</id>
      <name>Anvil</name>
      <picture>abraham.jpg</picture>
      <attack>3</attack>
      <health>5</health>
      <cost>2</cost>
      <rarity>3</rarity>
      <skill id="protect" x="1" y="2" />
      <skill id="rally" x="1" y="2" />
      <type>2</type>
      <set>4700</set>
      <upgrade>
         <level>2</level>
         <card_id>417</card_id>
         <skill id="protect" x="1" y="2" />
         <skill id="rally" x="2" y="2" />
         <health>6</health>
      </upgrade>
      <upgrade>
         <level>3</level>
         <card_id>418</card_id>
         <picture>abraham_lv3.jpg</picture>
         <attack>4</attack>
         <health>8</health>
      </upgrade>
      <upgrade>
         <level>4</level>
         <card_id>419</card_id>
         <skill id="protect" x="2" y="2" />
         <skill id="rally" x="2" y="2" />
         <attack>4</attack>
         <health>10</health>
      </upgrade>
      <upgrade>
         <level>5</level>
         <card_id>420</card_id>
         <skill id="protect" x="2" y="2" />
         <skill id="rally" x="3" y="2" />
         <health>11</health>
      </upgrade>
      <upgrade>
         <level>6</level>
         <card_id>421</card_id>
         <picture>abraham_lv6.jpg</picture>
         <skill id="protect" x="3" y="2" />
         <skill id="rally" x="3" y="2" />
         <health>13</health>
      </upgrade>
   </unit>
</root>


I can to extract all the names levels and card id’s with the code below… how do I restrict the results... can anyone help?

$xmlSource = simplexml_load_file("cards_section_1.xml");
  
            //print("<pre>".print_r($xmlSource,true)."</pre>");
            
            foreach ($xmlSource->unit as $units) {
      printf(
          "<p>the card ID for %s at level one is %d.</p>",
          $units->name,
          $units->id
     );
 
     if (isset($units->upgrade)) {
         
         foreach ($units->upgrade as $unitsUpgrade) {
         
     printf(
         "<p>the card ID for %s at level %d is %s.</p>",
         $units->name,
         $unitsUpgrade->level,
         $unitsUpgrade->card_id
 
     );
 }}}
Link to comment
https://forums.phpfreaks.com/topic/305094-extracting-specific-data-from-xml/
Share on other sites

  • Solution

the last element within each elements

Try not to rely on something being the first or last in a set: that could change at any time (probably won't) and your code would break.

 

What you really want is the final upgrade for the unit, right? The with the highest ?

$level = 0;
$upgrade = null;
foreach ($units->upgrade as $unitsUpgrade) {
	$l = (int)$unitsUpgrade->level;
	if ($l > $level) {
		$upgrade = $unitsUpgrade;
		$level = $l;
	}
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.