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
Link to comment
Share on other sites

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