SimpleXML and attributes not working with simplexml_load_file

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
OK I am stumped, been working on this for days and for the life of me
I cant figure out why it does not work. Here is some code that is
working and doing exactly what I want BUT it is using the
simplexml_load_string instead of simplexml_load_file. At the end of
the code it echos the number 8 which is what I need. If I use
simplexml_load_file instead I get "Fatal error: Call to a member
function attributes() on a non-object in blah blah blah".

Here is the url to the xml that I would like to use in
simplexml_load_file with.

I have searched all over the net and cant find a solution, can you
please either...

1. Give me the code to get it working, and explain to me what I did
2. Point me to the correct article that is going to fix my issue,  and
explain to me what I did wrong
3. Give me a back rub cause its killing me,  and explain to me what I
did wrong. :)

$string = <<<XML
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl"
name="Dungeons &amp; Raids" requestUrl="/character-statistics.xml">
  <statistic name="Total 5-player dungeons entered" quantity="110"/>
  <statistic name="Total 10-player raids entered" quantity="30"/>
  <statistic name="Total 25-player raids entered" quantity="81"/>
  <statistic name="Lich King 5-player dungeons completed (final boss
killed)" quantity="46"/>
  <statistic name="Lich King 10-player raids completed (final boss
killed)" quantity="9"/>
  <statistic name="Lich King 25-player raids completed (final boss
killed)" quantity="20"/>
  <statistic name="Lich King 5-player bosses killed" quantity="197"/>
  <statistic name="Lich King 5-player different bosses killed"

  <statistic highest="Sartharion the Onyx Guardian" name="Lich King 5-
player boss killed the most" quantity="14"/>
  <statistic name="Lich King 10-player bosses killed" quantity="93"/>
  <statistic name="Lich King 10-player different bosses killed"
  <statistic highest="Anub'Rekhan" name="Lich King 10-player boss
killed the most" quantity="8"/>
  <statistic name="Lich King 25-player bosses killed" quantity="147"/>
  <statistic name="Lich King 25-player different bosses killed"
  <statistic highest="The Four Horsemen" name="Lich King 25-player
boss killed the most" quantity="11"/>
  <category name="Classic">
    <statistic name="Edwin VanCleef kills (Deadmines)" quantity="1"/>

    <statistic name="Archmage Arugal kills (Shadowfang Keep)"
    <statistic name="Scarlet Commander Mograine kills (Scarlet
Monastery)" quantity="1"/>
    <statistic name="Chief Ukorz Sandscalp kills (Zul'Farrak)"
    <statistic name="Emperor Dagran Thaurissan kills (Blackrock
Depths)" quantity="1"/>
    <statistic name="Baron Rivendare kills (Stratholme)" quantity="10"/
Quoted text here. Click to load it
    <statistic name="General Drakkisath kills (Blackrock Spire)"
    <statistic name="Hakkar kills (Zul'Gurub)" quantity="1"/>
    <statistic name="Onyxia kills (Onyxia's Lair)" quantity="1"/>
    <statistic name="Ragnaros kills (Molten Core)" quantity="--"/>

    <statistic name="Nefarian kills (Blackwing Lair)" quantity="--"/>
    <statistic name="C'Thun kills (Temple of Ahn'Qiraj)" quantity="--"/
Quoted text here. Click to load it
  <category name="The Burning Crusade">
    <statistic name="Keli'dan the Breaker kills (The Blood Furnace)"
    <statistic name="Nexus-Prince Shaffar kills (Mana Tombs)"
    <statistic name="Epoch Hunter kills (The Escape From Durnholde)"
    <statistic name="Quagmirran kills (Slave Pens)" quantity="--"/>
    <statistic name="Black Stalker kills (Underbog)" quantity="--"/>

    <statistic name="Exarch Maladaar kills (Auchenai Crypts)"
    <statistic name="Talon King Ikiss kills (Sethekk Halls)"
    <statistic name="Murmur kills (Shadow Labyrinth)" quantity="--"/>
    <statistic name="Aeonus kills (Opening of the Dark Portal)"
    <statistic name="Warlord Kalithresh kills (The Steamvault)"
    <statistic name="Warchief Kargath Bladefist kills (The Shattered
Halls)" quantity="--"/>
    <statistic name="Pathaleon the Calculator kills (The Mechanar)"
    <statistic name="Warp Splinter kills (The Botanica)" quantity="--"/
Quoted text here. Click to load it
    <statistic name="Harbinger Skyriss kills (The Arcatraz)"

    <statistic name="Kael'thas Sunstrider kills (Magister's Terrace)"
    <statistic name="Prince Malchezaar kills (Karazhan)" quantity="1"/
Quoted text here. Click to load it
    <statistic name="Zul'jin kills (Zul'Aman)" quantity="2"/>
    <statistic name="Gruul kills (Gruul's Lair)" quantity="1"/>
    <statistic name="Magtheridon kills (Magtheridon's Lair)"
    <statistic name="Lady Vashj kills (Serpentshrine Cavern)"
    <statistic name="Kael'thas Sunstrider kills (Tempest Keep)"
    <statistic name="Illidan Stormrage kills (The Black Temple)"
    <statistic name="Kil'jaeden kills (Sunwell Plateau)" quantity="--"/
Quoted text here. Click to load it

  <category name="Wrath of the Lich King">
    <statistic name="Ingvar the Plunderer kills (Utgarde Keep)"
    <statistic name="Ingvar the Plunderer kills (Heroic Utgarde Keep)"
    <statistic name="Keristrasza kills (The Nexus)" quantity="2"/>
    <statistic name="Keristrasza kills (Heroic Nexus)" quantity="2"/>
    <statistic name="Anub'arak kills (Azjol-Nerub)" quantity="5"/>
    <statistic name="Anub'arak kills (Heroic Azjol-Nerub)"
    <statistic name="Herald Volazj kills (Ahn'kahet: The Old Kingdom)"

    <statistic name="Herald Volazj kills (Heroic Ahn'kahet)"
    <statistic name="The Prophet Tharon'ja kills (Drak'Tharon Keep)"
    <statistic name="The Prophet Tharon'ja kills (Heroic Drak'Tharon
Keep)" quantity="2"/>
    <statistic name="Cyanigosa kills (The Violet Hold)" quantity="1"/>
    <statistic name="Cyanigosa kills (Heroic Violet Hold)"
    <statistic name="Gal'darah kills (Gundrak)" quantity="1"/>
    <statistic name="Gal'darah kills (Heroic Gundrak)" quantity="3"/>
    <statistic name="Sjonnir the Ironshaper kills (Halls of Stone)"
    <statistic name="Sjonnir the Ironshaper kills (Heroic Halls of
Stone)" quantity="2"/>

    <statistic name="Loken kills (Halls of Lightning)" quantity="1"/>
    <statistic name="Loken kills (Heroic Halls of Lightning)"
    <statistic name="Ley-Guardian Eregos kills (The Oculus)"
    <statistic name="Ley-Guardian Eregos kills (Heroic Oculus)"
    <statistic name="King Ymiron kills (Utgarde Pinnacle)"
    <statistic name="King Ymiron kills (Heroic Utgarde Pinnacle)"
    <statistic name="Mal'Ganis defeated (Caverns of Time: Stratholme)"
    <statistic name="Mal'Ganis defeated (Heroic CoT: Stratholme)"
    <statistic name="Anub'Rekhan kills (Naxxramas 10 player)"

    <statistic name="Gluth kills (Naxxramas 10 player)" quantity="4"/>
    <statistic name="Gothik the Harvester kills (Naxxramas 10 player)"
    <statistic name="Grand Widow Faerlina kills (Naxxramas 10 player)"
    <statistic name="Grobbulus kills (Naxxramas 10 player)"
    <statistic name="Heigan the Unclean kills (Naxxramas 10 player)"
    <statistic name="Four Horsemen kills (Naxxramas 10 player)"
    <statistic name="Instructor Raxuvious kills (Naxxramas 10 player)"
    <statistic name="Loatheb kills (Naxxramas 10 player)" quantity="8"/
Quoted text here. Click to load it
    <statistic name="Maexxna kills (Naxxramas 10 player)" quantity="8"/
Quoted text here. Click to load it

    <statistic name="Noth the Plaguebringer kills (Naxxramas 10
player)" quantity="8"/>
    <statistic name="Patchwerk kills (Naxxramas 10 player)"
    <statistic name="Thaddius kills (Naxxramas 10 player)"
    <statistic name="Sapphiron kills (Naxxramas 10 player)"
    <statistic name="Kel'Thuzad kills (Naxxramas 10 player)"
    <statistic name="Mr. Bigglesworth kills" quantity="--"/>
    <statistic name="Anub'Rekhan kills (Naxxramas 25 player)"
    <statistic name="Gluth kills (Naxxramas 25 player)" quantity="9"/>
    <statistic name="Gothik the Harvester kills (Naxxramas 25 player)"

    <statistic name="Grand Widow Faerlina kills (Naxxramas 25 player)"
    <statistic name="Grobbulus kills (Naxxramas 25 player)"
    <statistic name="Heigan the Unclean kills (Naxxramas 25 player)"
    <statistic name="Four Horsemen kills (Naxxramas 25 player)"
    <statistic name="Instructor Raxuvious kills (Naxxramas 25 player)"
    <statistic name="Loatheb kills (Naxxramas 25 player)" quantity="9"/
Quoted text here. Click to load it
    <statistic name="Maexxna kills (Naxxramas 25 player)" quantity="8"/
Quoted text here. Click to load it
    <statistic name="Noth the Plaguebringer kills (Naxxramas 25
player)" quantity="9"/>
    <statistic name="Patchwerk kills (Naxxramas 25 player)"

    <statistic name="Thaddius kills (Naxxramas 25 player)"
    <statistic name="Sapphiron kills (Naxxramas 25 player)"
    <statistic name="Kel'Thuzad kills (Naxxramas 25 player)"
    <statistic name="Sartharion kills (Chamber of the Aspects 10
player)" quantity="5"/>
    <statistic name="Sartharion kills (Chamber of the Aspects 25
player)" quantity="9"/>
    <statistic name="Malygos kills (10 player)" quantity="1"/>
    <statistic name="Malygos kills (25 player)" quantity="4"/>
    <statistic name="Archavon the Stone Watcher kills (Wintergrasp 10
player)" quantity="2"/>
    <statistic name="Archavon the Stone Watcher kills (Wintergrasp 25
player)" quantity="6"/>


$xml = simplexml_load_string($string);
foreach($xml->category[2]->statistic[24]->attributes() as $a => $b) {}
echo $b;

Re: SimpleXML and attributes not working with simplexml_load_file

Quoted text here. Click to load it

(code snipped)

I do not have the solution, sorry.
But just to be sure:
did you read the comments on the page:

Did you try with a simple file, with only very few items?

What is the simplest piece of script that gives you troubles?


Re: SimpleXML and attributes not working with simplexml_load_file writes:

Quoted text here. Click to load it

Did you try to call that URL with a client which can't do client
side xml processing? If yes you would have seen something like that:

| C:\>php -r "echo
file_get_contents(' ');"|more
| <div>
| <div>
| <div class="cat_header">Dungeons &amp; Raids</div>
| ...

So that looks a bit different from your XML string.


Quoted text here. Click to load it

Quoted text here. Click to load it

You don't have a node category therefore the errors from simple_xml.


Re: SimpleXML and attributes not working with simplexml_load_file

On Feb 22, 1:19=A0pm, (Karl Pfl=E4sterer) wrote:
Quoted text here. Click to load it

Thats the problem I think with using simplexml_load_file, everything
comes back in div tags, how can I bring the URL XML and keep the
integrity, even better maybe I can just get the xml in a string and
that will work for the code I already have using simple_load_string.

Any ideas to read a xml file from a url and get that into a string
keeping the structure of the XML intact?

Re: SimpleXML and attributes not working with simplexml_load_file

On Feb 22, 7:50=A0pm, wrote:
Quoted text here. Click to load it

Ok I got it, I was not using simplexml properly, here is the working
code. If you want to get achievements for a character in the wow
armory here it is

$xmltext = join(file(" -
statistics.xml?r=3DStonemaul&n=3DAmeden&c=3D14807"), "");
$xmlobj = simplexml_load_string($xmltext);
print $xmlobj->div[3]->div[25]->div;

The print above gets the boss kills for "Loatheb kills (Naxxramas 10
player)" for the specific player, im sure looking at the xml you can
figure out how to call each achievement you want and then you can
place it into a variable.

Here is the link to the article that showed me what I was doing wrong,
you have to know how simplexml spits out the structure and how to call
each attribute =

Site Timeline