Java DOM Document vs Node for MathML
843834Jul 29 2010 — edited Jul 29 2010I'd like to write a Java program that reads/writes and manipulates MathML (math markup language, an XML-based language for representing complex math expressions) documents. I have zero experience with Java's many XML frameworks, including DOM, Stax and JAX.
It looks like DOM is the most fundamental, and it is probaly all I need to get the job done. Stax and JAX look like they are overkill for what I need.
Within the Java DOM, I see the following interfaces: Node, Document and Element. Both Document and Element extend the Node interface.
My unique situation is that I am guaranteed that all the input I ever get will just consist of elements. No character data, no comments, no prologs, etc. Just one super-parent root element that gives way to n child nodes/element tags.
Example:
<mathml>
<function name="add">
<number value="5"/>
<variable symbol="x"/>
</function>
</mathml>
The above MathML represents "5 + x". The <mathml> tag is the document root (and is itself just an element), and its one child, <function>, is an addition operation with two children itself, 5 and x.
So I'm thinking a Document object is not the best tool to use to represent this XML. Because Document objects are Nodes that can also be elements, CDATA, prologs, etc. I'm thinking I can get away with just using Element objects to do my bidding.
But having never used the Java DOM before, it is hard for me to visualize how one could code this up. Could someone post up some sample code showing - using nothing but Elements - one could define an in-memory root Element that represents my example above.
I'm hoping to see something like this:
public class Expression implements Element
{
// ... definition of Element routines
}
// Inside the Driver.java class
Expression mathml = new Expression("mathml");
Expression add = new Expression("function");;
Expression five = new Expression(5);
Expression x = new Expression("x");
add.AppendChild(five);
add.AppendChild(x);
mathml.AppendChild(add);
Am I close? Or am I missing "bigger" principles of the DOM?