Xstream重复属性问题
我一直在研究 XStream XML 解析,但遇到了一些问题。在我需要解析的文件中,我有一个具有多个同名任意属性的节点。节点是一支足球队,属性是每个球员的名字。
<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>
我的问题是,当我尝试解组该文件时,出现重复属性问题。我想做的就是将此字符串加载回列表中,以便我可以维护团队中的人员集合。对于订单等来说并不重要。 有人可以指出如何迭代每个属性的正确方向,即使它们具有相同的名称吗? 谢谢 克里斯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有两个选择:
向生成该文件的人提出错误,要求他们修复具有多个同名属性的错误(您给出的示例不是格式良好的 XML,因此不能期望 XML 处理器向
编写一些在流上作为文本流进行操作并生成一些真实 XML 的内容,例如
Manuel Almunia Rivero Abou Diaby ;...
.第二个是令人担忧的,因为在编写自己的 XML 解析代码时需要警惕各种各样的问题(这是他们的错误迫使你这样做),所以如果你能让生成文件的人来修复它,这就是要走的路(这将使其他进程能够使用它,而且也是一个简单的事实,毕竟这是他们的错)。
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).