views:

20

answers:

1

Hi Everyone,

I've been playing about with XStream XML parsing and I have a bit of a problem. In a file I need to parse, I have a node with several arbitrary attributes of the same name. THe node is a football team and the attributes are the names of each player.

<team home="Arsenal">
  <squad player="Manuel Almunia Rivero" player="Abou Diaby" player="Bacary Sagna" ... player="Robin van Persie"/>
  <subs player="Carlos Vela" player="Theo Walcott"/>
</team>

My problem is that when I try and demarshall this file, I get a duplicate attribute problem. All I want to do is load this strings back into a List so I can maintain a collection of people on the team. It is not important for the order etc. Can somebody point me in the right direction of how to iterate though each attribute even if they have the same name? Thanks Chris

A: 

You have two options:

  1. File a bug with whoever produced the file asking them to fix the bug where they have multiple attributes with the same name (the example you gave is not well-formed XML, so an XML processor cannot be expected to parse it).

  2. Write something that operates on the stream as a text stream and produces some real XML, such as e.g <squad><player>Manuel Almunia Rivero</player><player>Abou Diaby</player>....

The second is fraught, because there are all sorts of issues to be wary of when writing your own XML parsing code (which is what their bug is forcing you to do), so if you can get the person producing the file to fix it, that's the way to go (that and it'll make other processes able to use it, and also the simple fact that it is their fault after all).

Jon Hanna
So if I am correct in my understanding of your solution, you suggest to get a copy of the XML file where each value (e.g. name, team etc) is given as a node (e.g. enclosed in its own tags) rather than as an attribute to a node?
Chris Robinson
That would work. If at all possible though, get the person producing it to send you something in real XML rather than fixing it yourself.
Jon Hanna
Thanks for the help. The person producing the XML is a member of my time and this is an addon to the project so we don't have a lot of time to research proper XML conventions. The above output looked nice and human readable but is the correct way to serialise the data to enclose each attribute in its own tags? Thanks for your help and patience.
Chris Robinson
There are times when whether you have an attribute or an element are debatable, but one hard rule is that you can never have more than one attribute of the same name on the same element. It's just like HTML in that regard. So while the issue of which to go for can get more complicated in other cases, here having an element for each is definitely the way to go.
Jon Hanna
Think of attributes as saying "the". Once you've said "the squad's player is Mauel" you can't say "the squad's player is Abou".
Jon Hanna
Understood! Thanks a lot for you help!
Chris Robinson