How to use a parser with crimson XmlDocumentBuilder

Hi,

I'd be very grateful for any help on the following...

I used to use the following code to parse an Xml document, using a SimpleElementFactory to create my own class Element nodes...

XmlDocumentBuilder builder = new XmlDocumentBuilder();

parser = new com.sun.xml.tree.ValidatingParser();

parser.setDocumentHandler( builder );

..

..

parser.parse( in );

Docuent doc = buider.getDocument();

This worked fine using the project X code. However after migrating to the crimson code I can't find a way to link my XmlDocumentBuilder with the parser.

com.apache.crimson.parser.ValidatingParser seems to have no methods to link it to a com.apache.crimson.tree.XmlDocumentBuilder. If I follow a similar pattern to before the getDocument call returns null as the parser is not linked to the builder.

Please could someone point out my obvious mistake!

Many thanks

[953 byte] By [wellards] at [2007-9-26 1:34:39]
# 1

You can try this way:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

Document doc = db.newDocument();

doc = db.parse(new File(argv [0]));

XmlDocument tdoc = (XmlDocument) doc;

Hope this helps,

sri

thotha3 at 2007-6-29 2:18:03 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Hello,

I am using Crimson parser to parse an XML Document containing Czech characters. When I write the stuff read from the file, the output seems to be a corrupted one. It will be highly appreciable if someone can come up with any idea on where things might have gone wrong?

The foll is the piece of code that reads and writes the XML.

Writer xout = null;

String inputXML="content-chzech.xml";

javax.xml.parsers.DocumentBuilderFactory xDbf=javax.xml.parsers.DocumentBuilderFactory.newInstance();

javax.xml.parsers.DocumentBuilderxDb=xDbf.newDocumentBuilder();

Document xD =xDb.newDocument();

File xmlFile = new File(inputXML);

xD = xDb.parse(xmlFile);

XmlDocument xdoc = (XmlDocument) xD;

xdoc.normalize();

Writer writer = new BufferedWriter( new FileWriter( "parsedOutput.xml") );

xdoc.write( writer);

writer.close();

makcbe at 2007-6-29 2:18:03 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

I've done only a small amount of research, but here is what I've found that may be helpful to you.

Between JAXP 1.01 and JAXP 1.1 Sun donated their proprietary parser to Apache as open source and it was renamed to Crimson. Crimson.jar contains basically the same parser as parser.jar did in JAXP 1.0x, however the class hierarchy has changed. Where you used to use com.sun.xml.* you would now specify org.apache.crimson.xml.*.

It appears that the SimpleElementFactory is there and the builders that would let you association specific classes to DOM elements. I have not had time to verify that it all still works as it did, but at least it may be a start.

It should be noted however, if you continue to use this functionality with Crimson, you might have to readdress it at a later date because the Crimson web site says that Crimson will eventually be phased out and replaced by the Xerces 2 parser. See the Crimson web site at http://xml.apache.org/crimson/index.html for more details.

crespino4 at 2007-6-29 2:18:03 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...