Wrapping DOM in objects - what kind of approach?
I am struggling to come up with a clear, readable, usable and OO-friendly approach to parse and manipulate a custom xml format using objects which represent the xml elements. To illustrate here is a basic xml file using this format:
As you can see the reocurring pattern is a 'container' element like 'dimensions' which hold the 'real' elements, in this case 'dimension'.
<?xml version='1.0' encoding='UTF-8'?>
<form id='form001' code='form001' version='1' description='form001_desc'>
<level id='dim_01_l01' depth='1'>
The complete xml format has many more elements (not every container element has only one type of child element) but I choose not to show them here as they would only waste space. The overal concept is the same as showed above.
The approach I've narrowed in on is that of a builder class, say FormEditor, which works with Object representations of the 'real' elements.
In this example those objects would be
the Form object would have the following attributes and methods
The other Objects would all use this kind of approach. The function of the getXML() method creates a DOMElement object with the tagname of the element the object is representing, that would be <form> in this example.
Then it would set all the attributes, after this it would loop over the aDimensions and aCubes array and then call the getXML() method on these objects.
getXML finally returns the DOMElement object.
My problem now is: (yes finally :)) How would I retrieve a specific Member Object from the top-level Form Element because the Member object is waaaay down in the object hierarchy:
Does anyone who's been able to decypher this riddle have any insights how I should go about creating this? Am I way wrong or just a nudge in the good direction away from my answer?
P.S. I apologize if some of my concepts are badly explained. English not my mother tongue.
let me show u what i've done in the past
I still only see 1 level. the other levels are simply assigning properties or adding smaller composites.. but your main objects are cube and dimension...all child nodes to formeditor.
is still valid.
I normally have one entry point that converts xml to object and vice versa.
Example load your xml file and begin creating objects like these:
noticed i'm sending references to the child xml nodes in the argument... this object is responsible in building itself after that......
XmlNode shiftpoints = systemsetup.NextSibling; // shiftpoints
this._ShiftPointsClass = new ShiftPointsClass(
shiftpoints.FirstChild, //composite 1
shiftpoints.FirstChild.NextSibling, //composite 2
shiftpoints.FirstChild.NextSibling.NextSibling //composite 3
if xml nodes are missing it throws argumentexception error since it wasn't able to construct itself from the xmlnode passed in.