XML 子树中属于父级的属性
假设我有这个 XML
<doc:document>
<objects>
<circle radius="10" doc:colour="red" />
<circle radius="20" doc:colour="blue" />
</objects>
</doc:document>
,这就是它的解析方式(伪代码):
// class DocumentParser
public Document parse(Element edoc) {
doc = new Document();
doc.objects = ObjectsParser.parse(edoc.getChild("objects"));
for ( ...?... ) {
doc.objectColours.put(object, colour);
}
return doc;
}
ObjectsParser
负责解析对象位,但不是也不应该知道文档的存在。但是,在Document
中,颜色通过使用Map
与对象关联。
您建议使用哪种模式将颜色设置从 ObjectsParser.parse
返回给 DocumentParser.parse
,以便它可以将其与它们所属的地图中的对象关联起来?
另一种选择是这样的:
<doc:document>
<objects>
<circle id="1938" radius="10" />
<circle id="6398" radius="20" />
</objects>
<doc:objectViewSettings>
<doc:objectViewSetting object="1938" colour="red" />
<doc:objectViewSetting object="6398" colour="blue" />
</doc:objectViewSettings>
</doc:document>
丑陋!
Say I have this XML
<doc:document>
<objects>
<circle radius="10" doc:colour="red" />
<circle radius="20" doc:colour="blue" />
</objects>
</doc:document>
And this is how it is parsed (pseudo code):
// class DocumentParser
public Document parse(Element edoc) {
doc = new Document();
doc.objects = ObjectsParser.parse(edoc.getChild("objects"));
for ( ...?... ) {
doc.objectColours.put(object, colour);
}
return doc;
}
ObjectsParser
is responsible for parsing the objects bit, but is not and should not be aware of the existence of documents. However, in Document
colours are associated with objects by use of a Map
.
What kind of pattern would you recommend to give the colour settings back to DocumentParser.parse
from ObjectsParser.parse
so it can associate it with the objects they belong to in a map?
The alternative would be something like this:
<doc:document>
<objects>
<circle id="1938" radius="10" />
<circle id="6398" radius="20" />
</objects>
<doc:objectViewSettings>
<doc:objectViewSetting object="1938" colour="red" />
<doc:objectViewSetting object="6398" colour="blue" />
</doc:objectViewSettings>
</doc:document>
Ugly!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有什么问题
这假设
集合实例实现
可迭代的
可用作哈希键(实现
equals() 和 hashCode() 正确)
What's wrong with
This assumes
collection instance implementing
Iterable
usable as a hash key (implements
equals() and hashCode() correctly)
这是我自己的一个想法:
Here's an idea of my own: