使用Apache Xalan C ++库改进XSL转换

时间:2011-04-28 12:06:35

标签: c++ c xalan

函数converter()接受已解析的xsl流作为输入并返回输出字符串。 我能改进这个功能吗? 可以缓存传入的字符串吗? 任何建议将不胜感激。

 int  converter( char *xslInput,char *htmlOutput)
   {
    int theResult=-1;
    int i=0;
    char c;
    char xmlbuf[100] = {'\0'};
    char xmlbuf1[]="<?xml version=\"1.0\" encoding=\"";
    char xmlbuf2[]="\"?><a></a>";
    char default_encoding[]="iso-8859-1";
    char final_encoding[15];
    char *encoding = NULL;
    strcpy(final_encoding,default_encoding);
    strcpy(xmlbuf,xmlbuf1);
    strcat(xmlbuf,final_encoding);
    strcat(xmlbuf,xmlbuf2);

    XALAN_USING_STD(cerr)
    XALAN_USING_STD(cout)
    XALAN_USING_STD(endl)
    XALAN_USING_STD(ofstream)
    XALAN_USING_STD(ostrstream)
    XALAN_USING_STD(istrstream)

    XALAN_USING_STD(ostrstream)

    XALAN_USING_XERCES(XMLPlatformUtils)
    XALAN_USING_XALAN(XalanTransformer)

    ostrstream      theOutput;

    // 2. Initialize Xerces and Xalan
    XMLPlatformUtils::Initialize();
    XalanTransformer::initialize();

    {
            // 3. Create a XalanTransformer
            XalanTransformer theXalanTransformer;

            // Our input streams...
            istrstream      theXMLStream(xmlbuf, strlen(xmlbuf));
            istrstream      theXSLStream(xslInput, strlen(xslInput));
            ostrstream      theOutput;

            // 4. Prepare the input and output sources
            XALAN_USING_XALAN(XSLTInputSource)
            XALAN_USING_XALAN(XSLTResultTarget)
           // 5. Perform the transformation
            theResult = theXalanTransformer.transform(&theXMLStream, &theXSLStream, theOutput);
            if(theResult != 0)
            {
                    cerr << "StreamTransform Error: \n" << theXalanTransformer.getLastError()
                     << endl
                     << endl;
            }
            else
            {
                    theOutput << '\0';
                    strcpy(htmlOutput, theOutput.str());
                    cout << "Result of Transformation is SUCCESS\n" ;

            }

    }

            // 5. Shutdown the transformation thingy...
            XalanTransformer::terminate();
            XalanTransformer::ICUCleanUp();
            XMLPlatformUtils::Terminate();

    return theResult;
    }

1 个答案:

答案 0 :(得分:0)

有一点是使用了太多局部变量,例如char xmlbuf1,char xmlbuf2等。此外,你可以使用std :: string而不是那些原始的c风格数组吗?