O'Reilly logo

Java Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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("+================================+"); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required