[classad-users] XML Parsing


Date: Thu, 6 May 2004 09:54:51 -0500 (CDT)
From: "Giuseppe Avellino" <giuseppe.avellino@xxxxxxxxxx>
Subject: [classad-users] XML Parsing
Hi,
I am trying to use new Java Classad library 2.1 to handle record expressions
in XML format. These record expressions are written to files and then read
with an instance of ClassAdParser class.
To write record expression to a file, what I do is:

StringWriter stringWriter = new StringWriter();
ClassAdWriter classAdWriter = new ClassAdWriter(stringWriter,
ClassAdWriter.XML);
classAdWriter.enableFormatFlags(ClassAdWriter.MULTI_LINE_LISTS);
classAdWriter.enableFormatFlags(ClassAdWriter.MULTI_LINE_ADS);

classAdWriter.print(recordExpr);
String xmlText = stringWriter.toString();
(... method to write the String representation to a file...)

recordExpr is a RecorExpr created in some way.
This produce an XML file (N.B. please check: when I do this, closing
</classad> tag is missing - it could be a bug)

To read from a file I do:

FileInputStream fis = null;
try {
    fis = new FileInputStream(file);
} catch (Exception ex) {
    (...)
}
ClassAdParser parser = new ClassAdParser(fis, ClassAdParser.XML);
PrintStream ps = new PrintStream(new ByteArrayOutputStream());
parser.setErrorStream(ps);
Expr expr = parser.parse();
ListExpr listExpr = (ListExpr) expr;
RecordExpr recordExpr = new RecordExpr();
if (listExpr != null) {
      recordExpr = (RecordExpr) listExpr.sub(0);
}

This works quite good, but I would like to know how I can get error messages
(a String) during parsing (e.g. telling me that a tag is missing in the
file). What I need is to get exception.getMessage() and not the entire stack
trace as I can do using setErrorStream(PrintStream dest) method of the
ClassAdParser class.
With last version of Classad library I was using a modified version of the
class ClassAdSAXHandler providing a method to get a String containing error
messages. I think I could do the same for new release, but I need a dtd file
to avoid getting errors about XML elements and attribute missing definitions
(XML representation created by ClassAdWriter print() method doesn't contain
SYSTEM to specify a dtd file to use - so these kind of errors are reported).
I know some tags are changed, so old condor.dtd file has to be modified.
If it is not possible to produce a String representation of parsing errors
using standard handler, could you tell me how I can get a new condor.dtd
file (I can modify the last one on my one, but it could be better to have
the one you provide, if any).

I thank you in advace

cheers
-----
Giuseppe Avellino
Grid R&D Group
Government and Institutions Division

DATAMAT  S.p.A.
Via Laurentina, 760 ­ I - 00143 Rome ­ Italy
http://www.datamat.it

mailto: giuseppe.avellino@xxxxxxxxxx
Phone:  +39 06 5027 4587 (direct)  +39 06 5027 4571 (secretary)
Fax:      +39 06 5027 2500


Condor Classads Info:
http://www.cs.wisc.edu/condor/classad/



[← Prev in Thread] Current Thread [Next in Thread→]