SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Duplicated Tags - Bad Form???

    I have a company generating a daily XML feed I'll have to parse. One of the first things I've noticed is there are 3 <categoryName> tags.

    Am I correct in assuming this is bad form?

    I'd be parsing it with either PHP's DomDocument or SimpleXML and I'm not sure if these duplicate tags would break my parsing code.

    An interesting sidenote, if any instances of the <categoryName> tag have no data to display, they're generated as empty <categoryName/>. So perhaps they're necessary for the software generating the feed?

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suppose I could target these elements based on where they're placed in the XML tree.

    Any other thoughts?

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Yes that is indeed pretty bad form, especially if different nodes contain different values.

    Indeed, you could check by position, and hope for the best. That's what I would do as well.

    Also, I'd store all the other values you get as well, even if you don't do anything with them. Just so you have them and don't have to come back later if you find you picked the wrong one. With the prices of storage nowadays that shouldn't matter.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,633
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I'm guessing you are talking about something that looks like:

    <categoryName>One</categoryName>
    <categoryName>Two</categoryName>
    <categoryName />
    If so, that is often used to represent an array of values in XML. No real big issue with it IMHO. I could tell you how to handle it with C# and .NET XML Deserialization but that probably won't help here.

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you both for your input. wwb_99, yes that's how the tags are formatted. Interesting to hear there might be a legitimate reason for why duplicated tags are being used. I feel though someone at the other end of this xml file is taking the lazy approach. I'm sure it wouldn't be much work to parse each of the array attributes into unique nodes. Though these nodes are the only children of their own parent node, so targeting them specifically should be pretty easy. I'll just have to figure out how the PHP XML parsing libs do it.

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    The correct way to create arrays in XML is IMO:

    Code:
    <categories>
       <name>Category 1</name>
       <name>Category 2</name>
       <name />
    </categories>
    Just makes the most sense, semantically.

    Then again, the best XML is no XML at all

    I'm a huge json fan. I find it makes life so much easier.
    Of course, we don't always get to pick and need to row with the peddles that are handed to us.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,633
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    The correct way to create arrays in XML is IMO:

    Code:
    <categories>
       <name>Category 1</name>
       <name>Category 2</name>
       <name />
    </categories>
    Just makes the most sense, semantically.

    Then again, the best XML is no XML at all

    I'm a huge json fan. I find it makes life so much easier.
    Of course, we don't always get to pick and need to row with the peddles that are handed to us.
    That definitely makes a little more sense but they are really six of one half dozen of the other. Same with json -- it is nice and a little more readable, but the flip side is there are things one can do with xml that you can't do with json.

    In any case, remember one should *never* parse xml or json. They aren't text but rather serialized representations of objects. So remember to deserialize them.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •