Attributes in XML subtree that belong to the parent
Posted
by Bart van Heukelom
on Stack Overflow
See other posts from Stack Overflow
or by Bart van Heukelom
Published on 2010-06-16T19:11:30Z
Indexed on
2010/06/16
21:52 UTC
Read the original article
Hit count: 263
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!
© Stack Overflow or respective owner