2011-04-28 60 views
0

函數converter()接受解析的xsl流作爲輸入並返回輸出字符串。 我可以改進此功能嗎? 我可以緩存傳入的字符串嗎? 任何建議將不勝感激。使用Apache XalanC++改進XSL轉換libaray

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; 
    } 
+0

例如,您爲什麼需要final_encoding? – 2011-04-28 12:21:08

+0

等等...這個代碼甚至工作嗎?我在問關於transform()的調用。它通過** ostrstream **處理輸出,但根據API,期望的參數是一個** XSLTResultTarget **對象。 – IcedDante 2011-08-25 20:12:10

+0

另外,我相當確定transform()的第三個參數也應該有'&'運算符: 'theXalanTransformer.transform(&theXMLStream,&theXSLStream,&theOutput);' – IcedDante 2011-08-29 13:28:36

回答

0

的一件事是,太多的局部變量使用,例如,焦炭xmlbuf1,焦炭xmlbuf2等。此外,可以使用的std :: string,而不是那些純粹的C樣式的數組?