XML - API ORACLE XML

 
Vista:

API ORACLE XML

Publicado por Vale (1 intervención) el 14/10/2005 11:41:58
Buenos dias, agradeceria un poco de ayuda con la API de ORACLE para XML en C. Estoy tratando de transformar un archivo .xml en una serie de hojas HTML. El problema es que tengo que hacerlo con esta API y no encuentro ningun ejemplo. Tengo la docuemntación pero no soy capaz. Alguien tiene algun pequeño ejemplo de como utilizar funciones tales como: xslinit(), xslsettextparam(), xslprocessex(), xmlparse(), etc......?????


Muchas gracias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Alejandro

Transformación de archivos XML a HTML utilizando la API de Oracle en C

Publicado por Alejandro (258 intervenciones) el 12/07/2023 16:29:22
Buenos días, Vale.

Aquí tienes un pequeño ejemplo que utiliza la API de Oracle para XML en C para transformar un archivo XML en una serie de hojas HTML utilizando una hoja de estilo XSL (archivo .xsl):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdio.h>
#include <oraxml.h>
 
#define BUFFER_SIZE 1024
 
int main() {
    oratext* xmlFile = (oratext*)"input.xml";   // Ruta y nombre del archivo XML de entrada
    oratext* xslFile = (oratext*)"stylesheet.xsl";   // Ruta y nombre del archivo XSL de transformación
    oratext* htmlFile = (oratext*)"output.html";   // Ruta y nombre del archivo HTML de salida
 
    // Inicializar el entorno XML
    if (XMLInit() != XML_SUCCESS) {
        printf("Error al inicializar el entorno XML\n");
        return 1;
    }
 
    // Inicializar la estructura XMLParserCtxt
    XMLParserCtxt* parserCtxt = XMLParserCreateCtxt(NULL, NULL, NULL);
    if (parserCtxt == NULL) {
        printf("Error al crear el contexto del analizador XML\n");
        XMLTerm();
        return 1;
    }
 
    // Crear el analizador XML
    XMLParser* parser = XMLCreateParser(parserCtxt);
    if (parser == NULL) {
        printf("Error al crear el analizador XML\n");
        XMLFreeParserCtxt(parserCtxt);
        XMLTerm();
        return 1;
    }
 
    // Analizar el archivo XML de entrada
    if (!XMLParseFile(parser, xmlFile)) {
        printf("Error al analizar el archivo XML\n");
        XMLFreeParser(parser);
        XMLFreeParserCtxt(parserCtxt);
        XMLTerm();
        return 1;
    }
 
    // Crear la estructura XSLProcessorCtxt
    XSLProcessorCtxt* processorCtxt = XSLProcessorCreateCtxt(NULL, NULL, NULL);
    if (processorCtxt == NULL) {
        printf("Error al crear el contexto del procesador XSL\n");
        XMLFreeParser(parser);
        XMLFreeParserCtxt(parserCtxt);
        XMLTerm();
        return 1;
    }
 
    // Cargar el archivo XSL de transformación
    if (!XSLSetStylesheetFile(processorCtxt, xslFile)) {
        printf("Error al cargar el archivo XSL\n");
        XSLFreeProcessorCtxt(processorCtxt);
        XMLFreeParser(parser);
        XMLFreeParserCtxt(parserCtxt);
        XMLTerm();
        return 1;
    }
 
    // Aplicar la transformación XSL al archivo XML de entrada
    if (!XSLProcess(processorCtxt, parser)) {
        printf("Error al aplicar la transformación XSL\n");
        XSLFreeProcessorCtxt(processorCtxt);
        XMLFreeParser(parser);
        XMLFreeParserCtxt(parserCtxt);
        XMLTerm();
        return 1;
    }
 
    // Obtener la salida HTML generada por la transformación
    XSLStringCtxt* stringCtxt = XSLCreateStringCtxt(processorCtxt);
    if (stringCtxt == NULL) {
        printf("Error al crear el contexto de salida de cadena\n");
        XSLFreeProcessorCtxt(processorCtxt);
        XMLFreeParser(parser);
        XMLFreeParserCtxt(parserCtxt);
        XMLTerm();
        return 1;
    }
 
    char buffer[BUFFER_SIZE];
    size_t bytesRead;
    FILE* outputFile = fopen(htmlFile, "w");
    if (outputFile == NULL) {
        printf("Error al abrir el archivo HTML de salida\n");
        XSLFreeStringCtxt(stringCtxt);
        XSLFreeProcessorCtxt(processorCtxt);
        XMLFreeParser(parser);
        XMLFreeParserCtxt(parserCtxt);
        XMLTerm();
        return 1;
    }
 
    while ((bytesRead = XSLGetBytes(stringCtxt, buffer, BUFFER_SIZE)) > 0) {
        fwrite(buffer, 1, bytesRead, outputFile);
    }
 
    fclose(outputFile);
 
    // Liberar los recursos
    XSLFreeStringCtxt(stringCtxt);
    XSLFreeProcessorCtxt(processorCtxt);
    XMLFreeParser(parser);
    XMLFreeParserCtxt(parserCtxt);
    XMLTerm();
 
    printf("Transformación completada con éxito\n");
 
    return 0;
}

Este es solo un ejemplo básico para darte una idea de cómo utilizar algunas funciones de la API de Oracle para XML en C, como `XMLInit()`, `XMLParserCreateCtxt()`, `XMLCreateParser()`, `XMLParseFile()`, `XSLProcessorCreateCtxt()`, `XSLSetStylesheetFile()`, `XSLProcess()`, `XSLCreateStringCtxt()`, y `XSLGetBytes()`. Debes adaptarlo a tus necesidades específicas, asegurándote de proporcionar las rutas y nombres de archivo correctos para el archivo XML de entrada, el archivo XSL de transformación y el archivo HTML de salida.

Recuerda que también necesitarás configurar correctamente tu entorno de desarrollo con las bibliotecas y archivos de encabezado necesarios para utilizar la API de Oracle para XML en C. Verifica la documentación y los recursos proporcionados por Oracle para asegurarte de tener todo lo necesario para compilar y ejecutar correctamente el código.

Espero que este ejemplo te ayude a empezar con la API de Oracle para XML en C.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar