Program: xml2mif

Adobe FrameMaker[51] uses an interchange language called MIF (Maker Interchange Format), which is vaguely related to XML but is not well-formed. Let’s look at a program that uses DOM to read an entire document and generate code in MIF for each node. This program was used to create some earlier chapters of the book you are now reading.

The main program, shown in Example 21-9, is called XmlForm ; it parses the XML and calls one of several output generator classes. This could be used as a basis for generating other formats.

Example 21-9. XmlForm.java

import java.io.*; import org.w3c.dom.*; import com.sun.xml.tree.*; /** Convert a simple XML file to text. */ public class XmlForm { protected Reader is; protected String fileName; protected static PrintStream msg = System.out; /** Construct a converter given an input filename */ public XmlForm(String fn) { fileName = fn; } /** Convert the file */ public void convert(boolean verbose) { try { if (verbose) System.err.println(">>>Parsing " + fileName + "..."); // Make the document a URL so relative DTD works. String uri = "file:" + new File(fileName).getAbsolutePath( ); XmlDocument doc = XmlDocument.createXmlDocument(uri); if (verbose) System.err.println(">>>Walking " + fileName + "..."); XmlFormWalker c = new GenMIF(doc, msg); c.convertAll( ); } catch (Exception ex) { System.err.println("+================================+"); System.err.println("| *Parse Error* |"); System.err.println("+================================+"); ...

Get Java Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.