You are here:  » Parsing lines with the same tag name at same level.


Parsing lines with the same tag name at same level.

Submitted by jag959 on Tue, 2009-03-31 01:01 in

I'm trying to parse the 2 <link> lines between the <entry> tags but am not able to. The first one is a hyperlink to the user's page, the second is an image link to the user's avatar, both of whih are at the same level.

Please advise how I could go about this.

Thanks!

- Jason

Submitted by support on Tue, 2009-03-31 08:12

Hi Jason,

Assuming that you are parsing the XML with the following Format String:

xml|FEED/ENTRY/

...then both <link> elements should be present in $record, with the second one having the name LINK@1 (Magic Parser appends @1, @2, etc. to resolve duplicate field names). This can been seen on the following demo page based on the XML that you posted:

http://www.magicparser.com/demo?fileID=49D1CDCA3746C&record=1

However, as you cannot be sure which order the links will appear in the record, I would study the -REL attribute to determine which is the main link and which is the avatar URL, using something like this:

  function myRecordHandler($record)
  {
    if ($record["LINK-REL"] == "alternate" && $record["LINK@1-REL"] == "image")
    {
      $link = $record["LINK-HREF"];
      $avatar = $record["LINK@1-HREF"];
    }
    elseif ($record["LINK-REL"] == "image" && $record["LINK@1-REL"] == "alternate")
    {
      $link = $record["LINK@1-HREF"];
      $avatar = $record["LINK-HREF"];
    }
  }

Hope this helps!
Cheers,
David.

Submitted by jag959 on Tue, 2009-03-31 15:18

Perfectly solved my problem. Thanks for the quick reply.

- Jason