Answer the question
In order to leave comments, you need to log in
Why doesn't the simplexml_load_file function retrieve data from an XML file?
Hello!
There was a problem extracting data from a file. I have been playing the game for a long time and I was wondering how to use php to extract data from the res.xml file from the site. I also tried to extract using get , there were a few more attempts, and then when nothing happened, I abandoned this matter. Now I have a burning desire to try again, and I'm almost at the finish line)
I was looking for information about this, and read that you can use the simplexml_load_file () function . I managed to make an example of extracting data and understand it. I extracted data from a regular file, but I can’t extract data from a file that is on the game server.
Example from my res.xml file :
<root>
<s>
<r name="1111"/>
<r name="2222"/>
</s>
<s>
<r name="3333"/>
<r name="4444"/>
</s>
</root>
<?php
$xml = simplexml_load_file('res.xml');
/*
echo '<pre>';
print_r($xml);
*/
$s0_r0 = $xml->s[0]->r[0][name];
$s0_r1 = $xml->s[0]->r[1][name];
$s1_r0 = $xml->s[1]->r[0][name];
$s1_r1 = $xml->s[1]->r[1][name];
echo $s0_r0;
print "<br />";
echo $s0_r1;
print "<br />";
echo $s1_r0;
print "<br />";
echo $s1_r1;
?>
Answer the question
In order to leave comments, you need to log in
all these elements must be wrapped
. Example of work:
$xml_str = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<script name="game">
<S time="1389634801" shop="New Moscow Shop" xy="0/-1" city="New Moscow" >
<R name="Metals" cost="1.75" need="41571" />
<R name="Gold" cost="1.1" need="437837" />
<R name="Polymers" cost="1.25" need="301096" />
<R name="Organic" cost="2.6" need="242875" />
<R name="Silicon" cost="1.3" need="909964" />
<R name="Radioactive" cost="2" need="276821" />
<R name="Gems" cost="1.6" need="489296" />
<R name="Venom" cost="0.3" need="252830" />
</S>
<S time="1389634801" shop="Berezka" xy="-1/-1" city="New Moscow" >
<R name="Metals" cost="1.8" need="27183" />
<R name="Gold" cost="1.2" need="14694" />
<R name="Polymers" cost="1.27" need="15719" />
<R name="Organic" cost="2.97" need="17164" />
<R name="Silicon" cost="1.54" need="9195" />
<R name="Radioactive" cost="2.16" need="17663" />
<R name="Gems" cost="1.5" need="17054" />
<R name="Venom" cost="0.37" need="11479" />
</S>
</script>
XML;
$xml = new SimpleXMLElement($xml_str);
echo '<pre>';
echo 'cost:' . $xml->S[0]->R[0]['cost'] . '<br>';
foreach ($xml->S[0]->R as $state => $k) {
echo "name:{$k['name']}
cost:{$k['cost']}
need:{$k['need']}
<br>";
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question