Jump to content

XML Help


devWhiz

Recommended Posts

ok say I have a link called in file_get_contents and it returns xml and I want to have each <entry></entry> defined to a different variable like

 

-<entry>

  <id>1234</id>

  <name>Name</id>

  <age>Age</age>

</entry>

-<entry>

  <id>1234</id>

  <name>Name</id>

  <age>Age</age>

</entry>

-<entry>

  <id>1234</id>

  <name>Name</id>

  <age>Age</age>

</entry>

-<entry>

  <id>1234</id>

  <name>Name</id>

  <age>Age</age>

</entry>

 

See how there are 4 entry sets.. how would I assign the entrys to a variable and parse out the different names for each variable?

 

Like

 

$Entry1->name

$Entry2->age

$Entry3->id

$Entry4->other

 

 

Link to comment
Share on other sites

you need to 'sort out' your xml first....

<?php
$string="
<document>
<entry>
   <id>1234</id>
   <name>Name</name>
   <age>Age</age>
</entry>
<entry>
   <id>1234</id>
   <name>Name</name>
   <age>Age</age>
</entry>
<entry>
   <id>1234</id>
   <name>Name</name>
   <age>Age</age>
</entry>
<entry>
   <id>1234</id>
   <name>Name</name>
   <age>Age</age>
</entry>
</document>
";


if(!$xml = simplexml_load_string($string)){
    $message.="Can't connect to xml";
    }else{
        $count=1;
foreach ($xml->entry as $value){
        $GLOBALS['Entry'.$count]=$value->id;
        echo $GLOBALS['Entry'.$count]."<br />\n";
        $count++; 
         $GLOBALS['Entry'.$count]=$value->name;
        echo $GLOBALS['Entry'.$count]."<br />\n";
        $count++;          
        $GLOBALS['Entry'.$count]=$value->age;
        echo $GLOBALS['Entry'.$count]."<br />\n";
        $count++; 
        }
    }
?>

Link to comment
Share on other sites

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Start tag expected, '<' not found in C:\Program Files (x86)\EasyPHP-5.3.3.1\www\Prop.php on line 6

 

Warning: simplexml_load_string() [function.simplexml-load-string]: http://mob-dynamic-lb4.mobsters09.com/mob/get_city_list?user_id=45045691&auth_ke in C:\Program Files (x86)\EasyPHP-5.3.3.1\www\Prop.php on line 6

 

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in C:\Program Files (x86)\EasyPHP-5.3.3.1\www\Prop.php on line 6

 

Notice: Undefined variable: message in C:\Program Files (x86)\EasyPHP-5.3.3.1\www\Prop.php on line 7

Link to comment
Share on other sites

maybe it is because the xml dock on the url has more entries

 

<outer>
−
<xml>
−
<undeveloped_lands>
−
<land>
<id>200</id>
−
<image_url>
http%3A%2F%2Fcdn0%2Emobwarsapp%2Ecom%2Frpg%5Fimages%2Fopensocial%2Fmob%2Fingame%2Fterritory%2Fpawn%5Fshop%2Egif
</image_url>
−
<details>
%3Cspan%20class%3D%27landName%27%20style%3D%27color%3A%23FFFFFF%3B%20font%2Dsize%3A18px%3B%20font%2Dweight%3Abold%3B%27%3E%20Pawn%20Shop%3C%2Fspan%3E%3Cbr%3E%3Cspan%20class%3D%27landIncome%27%20style%3D%27color%3A%23FFFFFF%27%3EIncome%3A%20%24200%2C000%3C%2Fspan%3E%3Cbr%3E
</details>
<num_owned>0</num_owned>
<cost>1000000000</cost>
<is_racket>true</is_racket>
<favor_points>0</favor_points>
<purchase_limit>5</purchase_limit>
<name>Pawn%20Shop</name>
<income>$200,000</income>
−
<enhancements>
−
<enhancement>
<value>1</value>
−
<suffix>
% chance of a spirited defense (1/2x damage) when attacked
</suffix>
<prefix>+</prefix>
<type>ATTACK_CRITICAL_DEFENSE_PERCENT</type>
</enhancement>
</enhancements>
</land>
−
<land>
<id>0</id>
−
<image_url>
http%3A%2F%2Fcdn0%2Emobwarsapp%2Ecom%2Frpg%5Fimages%2Fopensocial%2Fmob%2Fingame%2Fterritory%2Fbig%2Fempty%5Flot%2Egif
</image_url>
−
<details>
%3Cspan%20class%3D%27landName%27%20style%3D%27color%3A%23FFFFFF%3B%20font%2Dsize%3A18px%3B%20font%2Dweight%3Abold%3B%27%3E%20Vacant%20Lot%3C%2Fspan%3E%3Cbr%3E%3Cspan%20class%3D%27landIncome%27%20style%3D%27color%3A%23FFFFFF%27%3EIncome%3A%20%24100%3C%2Fspan%3E%3Cbr%3E
</details>
<num_owned>0</num_owned>
<is_racket/>
<cost>4500</cost>
<favor_points>0</favor_points>
<name>Vacant%20Lot</name>
<income>$100</income>
</land>

Link to comment
Share on other sites

works for me!

<?php
if($xml = simplexml_load_file("http://mob-dynamic-lb4.mobsters09.com/mob/get_city_list?user_id=530830355&auth_key=d7240dc8bf350a48feb61eb04694bd67d5bc0cb1")){
    echo"XML File Connected<br />\n";
     ob_flush(); flush();   
}else{
    echo"Can't connect to feed";  
exit;
}

foreach ($xml->xml->undeveloped_lands->land as $value){
    $id=$value->id; 
   echo"id=$id<br />\n"; 
   
}
?>

Link to comment
Share on other sites

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.