Recipe 21.7 Program: xml2mif


Adobe FrameMaker[2] 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 this book.

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

The main program, shown in Example 21-10, 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-10. XmlForm.java
import java.io.*; import org.w3c.dom.*; import javax.xml.parsers.*; /** Convert a simple XML file to text.  * @version $Id: ch21.xml,v 1.5 2004/05/04 20:13:38 ian Exp $  */ 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( );             InputStream uri = getClass( ).getResourceAsStream(fileName);             DocumentBuilderFactory factory =                 DocumentBuilderFactory.newInstance( );             DocumentBuilder builder = factory.newDocumentBuilder( );             Document doc = builder.parse( 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-related technology is an area of rapid change. New APIs (and acronyms!) continue to appear. XML-RPC and SOAP let you build distributed applications known as web services 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 JDK 1.4 and later, provides convenience routines for accessing a variety of different parsers. It also includes SAX, DOM, and XSLT in the standard set of Java APIs.

For an interesting historical perspective on HTML by the person who primarily invented the Web and HTML, see Tim Berners-Lee's book, Weaving the Web (Harper).

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 byErik 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 developing XML information channels using RSS, often used for blogging.



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net