0

I'm building a process that creates XML (from a variety of sources and for a variety of purposes that I don't know in advance) and I want to pump the resulting XML directly into standard XML processing like SAX, StAX, and DOM. I've already done a StAX implementation that first produces an XML file and the StAX process reads the file. But I'd really like to skip the intermediate file creation and read. It seems like SAX is the right choice because a push parser is well suited for the job; processing the XML as it becomes available.

I don't quite see how to set up simply passing strings from one Java component into the SAX parser process. Most examples I've found read files and there are some that use URLConnection in support of service type applications. The source in this case will be another Java component that is part of the same system.

Roger F. Gay
  • 1,830
  • 2
  • 20
  • 25

1 Answers1

1

A sax parser can read from an InputSource based on a Reader. To feed a string to it, just wrap that string up in a StringReader.

The code to parse would look something like:

    XMLReader xmlReader = SAXParserFactory.newInstance()
                            .newSAXParser().getXMLReader();
    //Attach content handler, etc...
    InputSource source = new InputSource(new StringReader(xml_string));
    xmlReader.parse(source);

with of course some exception handling that I haven't bothered to show.

You can do the same in StAX and DOM, though of course the details of where you connect the StringReader are different.

Don Roby
  • 40,677
  • 6
  • 91
  • 113
  • I guess that could work. (Not home for a day or two to try it.) It seems odd however to create a new StringReader and InputSource for every line or element as I pass them through. It seems like DOM might have a real problem with it, unless I collect to end of file first and send it all. InputSource can also take a byte or character string. I'm not sure at all how to use the systemId. – Roger F. Gay Jan 01 '15 at 17:43
  • It's also possible you could write your strings to a `PipedWriter` attached to a `PipedReader` from which the `InputSource` (and thereby SAX) reads. – Don Roby Jan 01 '15 at 21:03
  • OK. I did that one, with my input source in a separate thread. It seems to work pretty well. For all I know, it's the best solution. I really like to find the simplest and best (and fastest) and am not entirely sure that using an extra thread isn't overkill in some way. But it is fast and there isn't a whole lot of code to it. Works quite well in simple tests. – Roger F. Gay Jan 02 '15 at 10:29