Re: [classad-users] XML Parsing


Date: Mon, 10 May 2004 07:49:18 -0500 (CDT)
From: Marvin Solomon <solomon@xxxxxxxxxxx>
Subject: Re: [classad-users] XML Parsing
Giuseppe Avellino wrote:
> 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)
The ClassAdWriter cannot add the closing </classads> tag until you call
close(), because it has no way of knowing whether you intend to add more
output.  Insert
    stringWriter.close();

before the line

    String xmlText = stringWriter.toString();

and this problem will go away.

>
> 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.
You can catch the exception text by calling the method
ClassAdParser.setErrorStream(PrintStream dest).
        OutputStream errs = new ByteArrayOutputStream();
        parser.setErrorStream(new PrintStream(errs));
        Expr expr = parser.parse();
        System.out.println(" + expr);
        System.out.println(" + errs);
[There really ought to be a version setErrorStream(Writer).  I'll add that
to the next release.]
This will give you the entire stack trace.  If you just want the detail message

    End of entity not allowed; an end tag is missing.

add the call

    parser.setVerbosity(1);

before the call to parser.parse().

> 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).
The dtd and the xmlSchema are included in the reference manual, but I don't
understand why you need them for what you're trying to do.
>
> 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/
Ciao,

Marvin Solomon                  Professor
Computer Sciences Department    University of Wisconsin
1210 W. Dayton St.              Madison WI, 53706-1685, USA
(608) 263-2844                  solomon@xxxxxxxxxxx
Condor Classads Info:
http://www.cs.wisc.edu/condor/classad/


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