I went through this over the past 10 years, creating an XML DOM for chemistry, graphics, math, etc. My own solution was to use the DOM where the elements can be subclasses (I use xom.nu, but there are others). W3c dom does not allow subclasses (IIRC), so you will have to create a delegate model. (I tried this many years ago and rejected it, but software tools and libraries make it all a lot easier (for example, the IDE will generate delegate methods).
If you do a lot, and especially if you create many custom methods, I would recommend translating your own system. The effort will be in your methods (dotProduct), not in XML.
Here, for example, is my class for a 3D point .
public class CMLPoint3 extends AbstractPoint3
(which extends the base class CMLElement, which extends nu.xom.Element
Creating items is a factory. Here is a piece of my SVGDOM:
public static SVGElement readAndCreateSVG(Element element) { SVGElement newElement = null; String tag = element.getLocalName(); if (tag == null || tag.equals(S_EMPTY)) { throw new RuntimeException("no tag"); } else if (tag.equals(SVGCircle.TAG)) { newElement = new SVGCircle(); } else if (tag.equals(SVGClipPath.TAG)) { newElement = new SVGClipPath(); } else if (tag.equals(SVGDefs.TAG)) { newElement = new SVGDefs(); } else if (tag.equals(SVGDesc.TAG)) { newElement = new SVGDesc(); } else if (tag.equals(SVGEllipse.TAG)) { newElement = new SVGEllipse(); } else if (tag.equals(SVGG.TAG)) { ... } else { newElement = new SVGG(); newElement.setClassName(tag); System.err.println("unsupported svg element: "+tag); } if (newElement != null) { newElement.copyAttributesFrom(element); createSubclassedChildren(element, newElement); } return newElement;
You can see tools for copying and recursion.
The questions you need to think about are the following:
- How closely does this relate to XSD
- Use XSD Data Types
- perform an input check
- I use the DOM as the primary data structure (i)
- how often things change.
FWIW I went through 6 revisions and consider another (using Scala as the main engine).
source share