Jump to content


Photo

[Resolved] xml with php, getting attributes, optimizing


  • Please log in to reply
2 replies to this topic

#1 spelltwister

spelltwister
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 13 August 2006 - 06:42 PM

Hey all,

I'm having a hard time finding any examples of parsing XML that has attributes.

Here's the xml:

<units>
  <persiancamp unit="infantry" number="8" owner="persian" ranged="0" />
  <persiancamp unit="xerxes" number="1" owner="persian" ranged="0" />
  <m1 unit="sInfantry" number="1" owner="persian" ranged="1" />
<...>
</units>

I have an array with all possible tag names and I need to be able to do something like this:

$array = array('persiancamp','m1'...);
for($i=0;$i<sizeof($array);$i++){
 
  //CYCLE THROUGH XML FINDING IF TAGS MATCH CURRENT ARRAY VALUE, GATHER ALL ATTRIBUTE INFORMATION

}

Also, is this the most efficient way?  Should I setup the XML like this:

<persiancamp>
  <infantry number = '8' ranged ='0' />
  <xerxes number = '1' ranged='0' />
</persiancamp>

OR

<persiancamp>
  <infantry>
    <number>8</number>
    <ranged>0</ranged>
  </infantry>
  <xerxes>
    <number>1</number>
    <ranged>0</ranged>
  </xerxes>
</persiancamp>

and use innertext attributes?

Any help, or links to tutorials are much appreciated.

Thanks!

Mike

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 13 August 2006 - 06:46 PM

What are you using to parse your XML?  PHP's built in functions, or a seperate library?  Additionally, if you are using PHP's functions, what version of PHP are you using, because they differ substantially.

I generally use a seperate library because PHP's functions differs so much between versions.  I recommend minixml (http://minixml.psychogenic.com/), as it is easy to use and is compatible with php 4 and 5.

#3 spelltwister

spelltwister
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 14 August 2006 - 03:59 PM

I should have mentioned that I'm using PHP 4.3.

OMG! That thing is amazingly simple and easy to use.  Thanks so much for letting me know about it! :-D

Mike




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users