Jump to content

[Resolved] xml with php, getting attributes, optimizing


spelltwister

Recommended Posts

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
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.