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("+================================+");
            System.err.println(ex.getClass(  ));
            System.err.println(ex.getMessage(  ));
            System.err.println("+================================+");
        }
        if (verbose)
            System.err.println(">>>Done " + fileName + "...");
    }

    public static void main(String[] av) {
        if (av.length == 0) {
            System.err.println("Usage: XmlForm file");
            return;
        }
        for (int i=0; i<av.length; i++) {
            String name = av[i];
            new XmlForm(name).convert(true);
        }
        msg.close(  );
    }
}

The actual MIF generator is not shown here -- it’s not really XML-related -- but is included in the online source code for the book.

See Also

XML is an area of rapid change. Schemas are becoming “real.” New APIs (and acronyms!) continue to appear. XML-RPC and SOAP let you build distributed applications using XML and HTTP as the program interchange. The W3C has many new XML standards coming out. Several web sites track the changing XML landscape, including the official W3C site (http://www.w3.org/xml/) and O’Reilly’s XML site (http://www.xml.com).

Sun’s Java API for XML Parsing (JAXP), included with the Java 2 SDK 1.4 and later, provides convenience routines for accessing a variety of different parsers. This also includes SAX, DOM, and XSLT in the standard set of Java APIs for the first time.

And many books compete to cover XML. These range from the simple XML: A Primer by Simon St. Laurent to the comprehensive XML Bible by the prolific Elliotte Rusty Harold. In between is Learning XML by Erik T. Ray (O’Reilly). O’Reilly’s Java and XML by Brett McLaughlin covers these topics in more detail, and also covers XML publishing frameworks such as Apache’s Cocoon and XML information channels using RSS.



[51] Previously from Frame Technologies, a company that Adobe ingested.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.133.49