[EMAIL PROTECTED] wrote:
heiko ..i know that is not a valid structure :) ...

i'm just trying to find the best and optimal way for unmarshalling multiple
xml documents gathered in one big txt file .... in structure that i
described in my previous post.

danny

I doubt you can do it easily.... What you describe is not XML you know, just somethnig that looks like it.

If I really had to do it, though, i would.

1. Use an XML parser to parse the stream of XML into discrete chunks
2. Make those chunks well formed
3. Pass the chunks into castor to get unmarshalled
4. do what you want with the objects.

This is not really castor related, but you can do the following to parse streams in Xerces ( This code i had lying around is fairly out of date,
but may work with the new xerces, I'm sure that there is an eqivalent anyhow )


1. Create non closing reader, as xerces will try to close the stream at the final end tag:


public class NoCloseBufferedReader extends BufferedReader {
public NoCloseBufferedReader ( InputStreamReader i ) {
super ( i );
}

public void close() {
}

public void reallyClose() {
super.close();
}
}


2. Create content handler for your stuff....

public interface YourMessageHander {
public void startElement ( String uri, String localName, String raw, Attributes attrs ) throws SAXException;
public void characters ( char[] ch, int start, int length );
public void endElement ( String namespaceURI, String localName, String qName ) throws SAXException;
public String getString();
}


public class YourStreamContentHandler extends DefaultHandler {

protected YourMessageHandler processor;
int indentLevel = 0;

public void startDocument () { }
public void ignorableWhitespace( char[] ch, int start, int length ) {}
public void characters(char[] ch, int start, int length ) {
if ( processor != null ) {
processor.characters ( ch, start, length );
}
}

public void startElement( String uri,
String localName,
String raw,
Attributes attrs ) throws SAXException {


++indentLevel;
processor.startElement( uri, localName, raw, attrs );
}

public void endElement ( String namespaceURI,
String localName,
String qName ) throws SAXException {


--indentLevel;

processor.endElement ( namespaceURI, localName, qName );


if ( indentLevel == 0 ) {
// Done with this message;
// This now contains a single top level block
someOtherObject.doSomeThingWith ( processor.getString() );
throw new SAXParseFinishedException();
}
}

4. Create a streaming char factory...

public class StreamingCharFactory extends org.apache.xerces.readers.DefaultReaderFactory {

public XMLEntityHandler.EntityReader createCharReader ( XMLEntityHandler entityHandler,
XMLErrorReporter errorReporter,
boolean sendCharDataAsCharArray,
Reader reader,
StringPool stringPool ) throws Exception {

return new org.apache.xerces.readers.StreamingCharReader ( entityHandler,
errorReporter,
sendCharDataAsCharArray,
reader,
stringPool );
}


public XMLEntityHandler.EntityReader createUTF8Reader ( XMLEntityHandler entityHandler,
XMLErrorReporter errorReporter,
boolean sendCharDataAsCharArray,
InputStream data,
StringPool stringPool ) throws Exception {
XMLEntityHandler.EntityReader reader;

reader = new org.apache.xerces.readers.StreamingCharReader ( entityHandler,
errorReporter,
sendCharDataAsCharArray,
new InputStreamReader(data, "UTF8" ),
stringPool );
return reader;
}
}


5. Create a parser that uses all these:

NoCloseBufferedReader buffer = new NoCloseBufferedReader ( new InputStreamReader ( inputStream ) );

SAXParser parser = (SAXParser) Class.forName("org.apache.xerces.parsers.SAXParser").newInstance();

parser.setContentHandler( new YourStreamContentHandler() );
parser.setReaderFactory( new StreamingCharFactory());

while ( true ) {

try {
parser.parse( new InputSource ( buffer ) );
}
catch ( SAXParseFinishedException e ) {
count++;
debug ( "Parsed " + count + " messages" );
}

parser.reset();

}


The YourMessageHandler could just add the various strings to a list, and then subsequently unmarshal them all..

Hope this is of some use....

James

----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev

Reply via email to