The only change between the old Sun parser and
the new one
is one line, the line used to instantiate a DOM document object.
Instead
of using XMLDocumentBuilder.createXmlDocument(
url ); use XMLDocument.createXmlDocument(
url );. Just one line should fix the old code.
The new IBM parser has a new way of instantiating DOM
document objects
that has changed considerably since the old version. You can still use
the old way to instantiate a DOM object, but this method creates a
"backward-compatible"
IBM XML parser which is slow. If you use the new way, you get the speed
enhancements of the new IBM parser.
- The import statement has changed from com.ibm.xml.parser.*
to com.ibm.xml.parsers.*.
- You can't use an InputStream
create a DOM document object from anymore; you have to use a String
(that
contains a URL to an XML file).
- The method to get a
Document
object is now getDocument();
instead of readStream(i).
The code to instantiate a DOM object using the
Sun Project
X Technology Release 1 parser is:
import com.sun.xml.tree.*;
import org.w3c.dom.*;
try {
String url
=
"http://beanfactory.com/xml/AddressBook.xml";
Document doc
=
XmlDocument.createXmlDocument(url);
}
catch(Exception e){}
The code to instantiate a DOM object using the IBM XML
Parser for Java
version 2.0.4 is:
import com.ibm.xml.parsers.*;
import org.w3c.dom.*;
try {
String url
=
"http://beanfactory.com/xml/AddressBook.xml";
DOMParser ps
= new DOMParser();
ps.parse(
url );
Document doc
= ps.getDocument();
}
catch( Exception e ){}
The code to instantiate a DOM object using the OpenXML
v1.0.4 parser
is:
import org.w3c.dom.*;
import org.openxml.*;
import org.openxml.io.*;
import org.openxml.parser.*;
import org.openxml.DOMFactory.*;
try {
URL u = new
URL(
"http://beanfactory.com/xml/AddressBook.xml");
InputStream
is = u.openStream();
BufferedReader br = new
BufferedReader(
new InputStreamReader(is) );
org.openxml.io.Parser
ps =
DOMFactory.createParser( br , "whatever" );
Document doc
= ps.parseDocument();
}
catch(Exception e){}
Please note that a class named Parser exists in the
IBM and OpenXML
packages. This is why the OpenXML Parser class is prefixed with its
package
name. This would be necessary only if you were using both the IBM and
OpenXML
parsers in the same class.