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
You have two options:
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).
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).