Jump to content

parsing xml


googlexx

Recommended Posts

I've been reading parsing xml with php forums all day and still can't seem to figure out how to start with this one.

 

there is a node here: http://xml.heroesofnewerth.com/xml_requester.php?f=match_stats&opt=mid&mid[]=30428528

called match_stats. All i need help with is how to get those 10 elements inside match_stats and how to grab the stat name= nickname from each element.

 

I'm just looking for somewhere to start and I'm sure I can figure out the rest. Thanks

Link to comment
Share on other sites

You would use an xpath query. Assuming you have read that URL into a file doc.xml -

 

<?php
$xml = simplexml_load_file('doc.xml');

$result = $xml->xpath("//stat[@name='nickname']"); // 	Selects all the stat elements that have an attribute named 'name' with a value of 'nickname'
foreach($result as $node){
    echo "Nickname: $node<br />";
}
?>

Link to comment
Share on other sites

nevermind i figured it out.

 

for syntax issues is there a better way to grab more nodes than this?

 

$xml = simplexml_load_file('http://xml.heroesofnewerth.com/xml_requester.php?f=match_stats&opt=mid&mid[]=30428528');

$i = 0;
$result = $xml->xpath("//stat[@name='nickname']"); //    Selects all the stat elements that have an attribute named 'name' with a value of 'nickname'
foreach($result as $node){

$name_array[$i] = "$node";
$i++;
}
$i=0;
$result = $xml->xpath("//stat[@name='level']"); //    Selects all the stat elements that have an attribute named 'name' with a value of 'nickname'
foreach($result as $node){

$level_array[$i] = "$node";
$i++;
}

Link to comment
Share on other sites

What's your overall goal and the desired output/result?

 

Because programming is an exact science (computers only do exactly what their code and data tell them to do), when you don't take into account everything you are trying to accomplish (i.e. define what you are trying to achieve first before you write any code), you end up wasting time rewriting your code and redefining your data structures or you end up with an inefficient kludge of code that is hard to read, troubleshoot, and fix.

 

The solution to: "how to get those 10 elements inside match_stats and how to grab the stat name= nickname from each element" can be completely different from: "I want to get the following fields for each team and display them like so _________ and/or insert them into a database like so _________ and/or calculate/sort them like so _________".

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.