0

函数 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;
    }
4

1 回答 1

0

一件事是使用了太多的局部变量,例如 char xmlbuf1、char xmlbuf2 等。此外,您可以使用 std::string 代替那些原始的 c 样式数组吗?

于 2011-04-28T12:25:09.030 回答