# IO Package

The IO package contains classes for reading and writing math objects to various I/O streams.

### Class descriptions

#### MathMLDocument

This class uses IBM's XML Parser, Java Edition. MathMLDocument will translate math objects into MathML code. The MathML code can then be used to construct a MathML document, which can be printed to any output stream.

Sample code to create a MathML document:

    import JSci.io.*;
    import org.w3c.dom.*;
    import com.ibm.xml.parser.*;
    .
    .
    MathMLDocument doc=new MathMLDocument();
    Element root=doc.getDocumentElement();
    root.appendChild(doc.createNumber(2.3));
    Element aNode=doc.createElement("a tag name");
    aNode.appendChild(doc.createVector(aVector));
    aNode.appendChild(doc.createNumber(4));
    root.appendChild(aNode);

Use either:

- `doc.print(java.io.Writer) throws IOException`
- `doc.printWithFormat(java.io.Writer) throws IOException`

to write the document to an output stream.

There is also a DocumentFragment interface which can be instantiated using the TXDocumentFragment class. This class represents a fragment of MathML code to which elements can be appended. The DocumentFragment can then itself be appended to the MathML document. Some of the MathMLDocument.createXXX() methods require a DocumentFragment as an argument.

DocumentFragment sample code:

    TXDocumentFragment frag=new TXDocumentFragment();
    frag.appendChild(node1);
    frag.appendChild(node2);
    doc.appendChild(frag);

#### MathMLParser

This class uses IBM's XML Parser, Java Edition. MathMLParser will translate a MathML document into math objects.

Sample code to parse a MathML document:

    import JSci.io.*;
    .
    .
    MathMLParser parser=new MathMLParser();
    try {
    	parser.parse("doc.MathML");
    } catch(Exception e) {
    	e.printStackTrace();
    }
    Object parseList[]=parser.getJSciObjects();

The parseList is an array containing parsed MathML lines. A MathML line may consist of a mathematical expression or just a single math object. The `instanceof` operator should be used to determine which. If parseList[i] is an instance of MathNumber, MathVector, etc then the line just contains that object. However, if it is an instance of MathMLExpression then this object will specify a math operation and arguments to which it is to be applied. The arguments may either be math objects or other MathMLExpressions. The evaluate() method can be used to evaluate a MathMLExpression to a single math object. An instance of MathMLRelation is used to store math objects parsed from MathML relations. It functions in a similar way to the MathMLExpression class.

Return to the Developer's Guide contents.