You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xalan.apache.org by "Coker, Jonathan M" <jo...@boeing.com> on 2007/07/05 20:12:58 UTC

RE: Document from a string

Thank you for the suggestion.  The StreamTransform seemed to very close
to what I was trying to do.  However, since I just wanted to get a
XalanDocument*, I tried using XalanTransformer::parseSource.  This is
what I ended up with:


///////////////////////////////////
XalanSourceTreeInit		theSourceTreeInit;
string testXML("<TESTING></TESTING>");

istringstream theXMLStream(testXML);

XSLTInputSource theInputSource(&theXMLStream);
theInputSource.setSystemId(XalanDOMString("foo").c_str());

XalanDefaultParsedSource parsedSource(theInputSource); //Throws unknown
exception

XalanParsedSource* parsedSourcePtr = &parsedSource;


theXalanTransformer.parseSource(theInputSource, parsedSourcePtr);

XalanDocument* xDoc = parsedSourcePtr->getDocument();
//////////////////////////////////////////////

The line indicated throws an (unkown) exception. I have used a
Xalan/Xerces/XPath to get data out of files before, but parsing a
string/stream is giving me problems.  Any advice or corrections would be
appreciated.
 

-----Original Message-----
From: David Bertoni [mailto:dbertoni@apache.org] 
Sent: Friday, June 29, 2007 7:34 PM
To: xalan-c-users@xml.apache.org
Subject: Re: Document from a string

Coker, Jonathan M wrote:
> Another document creation question:  Is there a way to create a 
> parsable document from a standard c++ string (or char*)?  I have a 
> string, not in a file, not broken into a char array.  It is well 
> formed XML and I would lke to be able to use XPath to acces the 
> information.  I have looked through the different ways to create a 
> document, including XalanTransformer::parseSource, but have not seen 
> the leap from a string to an acceptable input source.  I thought 
> XSLTInputSource might work but I could not see what was needed for the
public and system Id values.
> Nudges in the right direction are appreciated.

Take a look at the StreamTransform sample.  Basically, you can use any
std::istream-derived class as input for the source tree or the
stylesheet.

Dave

Re: Document from a string

Posted by David Bertoni <db...@apache.org>.
Coker, Jonathan M wrote:
> Thank you for the suggestion.  The StreamTransform seemed to very close
> to what I was trying to do.  However, since I just wanted to get a
> XalanDocument*, I tried using XalanTransformer::parseSource.  This is
> what I ended up with:
> 
> 
> ///////////////////////////////////
> XalanSourceTreeInit		theSourceTreeInit;
> string testXML("<TESTING></TESTING>");
> 
> istringstream theXMLStream(testXML);
> 
> XSLTInputSource theInputSource(&theXMLStream);
> theInputSource.setSystemId(XalanDOMString("foo").c_str());
> 
> XalanDefaultParsedSource parsedSource(theInputSource); //Throws unknown
> exception
Rather than creating this yourself, you should call
XalanTransformer::createParsedSource().  Or, you should pass in an
ErrorHandler instance so you can see what sort of error is reported.

> 
> XalanParsedSource* parsedSourcePtr = &parsedSource;
> 
> 
> theXalanTransformer.parseSource(theInputSource, parsedSourcePtr);

XalanTransformer::parseSource() creates an instance of a XalanParsedSource
for you, based on the supplied XSLTInputSource, but you're providing a
pointer to a pre-existing instance of XalanDefaultParsedSource.  You're
also re-using an existing XSLTInputSource, which may or may not work,
depending on the source of the data.  In this case, in won't work, because
the istringstream is already at the end of the stream.

What happens if you start with the StreamTransform sample, and just
substitute your code for the existing code?  I modified the sample to use
std::string and istringstream and it works just fine.

Dave