Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente bannato
    Registrato dal
    Feb 2005
    Messaggi
    43

    [Java] Creazione di un file Xml e inserimento della trasformazione xsl

    Ho creato un mio file in java che mi crei un file xml
    il file che creo visualizza questo:

    <?xml version="1.0" encoding="UTF-8" ?>
    - <root attributo="valore">
    - <init-param>
    <param-name>mio nome</param-name>
    <param-value>mio valore</param-value>
    </init-param>
    </root>


    Vorrei inserire

    <?xml-stylesheet type="text/xsl" href="miofile.xsl"?>

    in modo che il mio file diventi

    <?xml version="1.0" encoding="UTF-8" ?>

    <?xml-stylesheet type="text/xsl" href="miofile.xsl"?>

    - <root attributo="valore">
    - <init-param>
    <param-name>mio nome</param-name>
    <param-value>mio valore</param-value>
    </init-param>
    </root>

    ho cercato di saltarci fuori con le api jdom XSLTransformer che trovate qui senza ottenrere risultati soddisfacenti. qualcuno può aiutarmi.
    Grazie.

    [ot]Se dovevo inserire il tutto nella sezione xml spostate pure il thread e scusatemi

  2. #2
    Utente bannato
    Registrato dal
    Feb 2005
    Messaggi
    43
    Il codice che mi realizza l'xml sopra è questo:
    codice:
    import java.io.*;
    import java.util.*;
    
    //IMPORTIAMO I PACKAGE NECESSARI PER LAVORATE CON GLI XML 
    import org.jdom.*;
    import org.jdom.output.XMLOutputter;
    
    public class CreaXML 
                 {
    			 public static String PATH="C:\\Programmi\\Apache Group\\Tomcat 4.1\\webapps\\rosi\\xml\\";
    			  public CreaXML(String nomeFile) 
    			         {
    					 try
                            {
    						writeFile(this.PATH+nomeFile);
    						}
    					  catch(Exception e)
                            {
                            System.err.println ("errore: "+e);
                            }
                          }
    
                  public void writeFile(String path) throws Exception
                           {
    					   File fileTmp = new File(path);
    					   //ISTANZIAMO UN OGGETTO Element PASSANDOGLI LA STRINGA CHE SARA' IL NOME DEL NOSTRO SUPERTAG
    					   Element root = new Element("root");
    					   //ISTANZIAMO UN OGGETTO Document PASSANDOGLI COME PARAMETRO L'OGGETTO ELEMENT PRECEDENTEMENTE ISTANZIATO
    					   Document documento = new Document(root);
    					   //AGGIUNGIAMO AL NOSTRO ELEMENT UN ATTRIBUTO: <root attributo=valore>
    					   root = root.setAttribute( "attributo","valore");
    					   //ISTANZIAMO ALTRI OGGETTI Element PASSANDOGLI LE STRINGHE CHE SARANNO I NOMI DEI NOSTRI TAG 
    					   Element initparam = new Element("init-param");
    					   Element paramname = new Element("param-name");
    					   Element paramvalue = new Element("param-value");
    					   //ASSEGNAMO DEI VALORI AI TAG param-name E param-value:<param-name>mio nome</param-name>
    					   paramname.addContent("mio nome");
    					   paramvalue.addContent("mio valore");
    					   //AGGIUNGIAMO I TAG param-name E Param-value AL TAG init-param
    					   initparam.addContent(paramname);
    					   initparam.addContent(paramvalue);
    					   //AGGIUNGIAMO IL TAG init-param AL SUPERTAG root
    					   root.addContent(initparam);
    					   
    					   BufferedWriter out = null;
    					   
    					   try
                                {
    							out = new BufferedWriter(new FileWriter(path));
    							//ISTANZIAMO LA CLASSE XMLOutputter CHE CI PERMETTE DI FORMATTARE IL FILE XML.
    							XMLOutputter xout = new XMLOutputter();
    							xout.setIndent(true);
    							xout.setNewlines(true);
    							//CON QUESTA ISTRUZIONE SCRIVIAMO IL DOCUMENTO documento IN UN BufferedWriter CHE CREA IL FILE TRAMITE UN FileWriter
    							xout.output(documento,out);
    							}
    					   catch (IOException e)
    					        {
    							System.err.println ("errore: "+e);
                                }
    					   finally
                                {
                                 try
                                    {
    								out.close();
    								}
    							 catch(IOException e)
                                    {
    								System.err.println ("errore: "+e);
                                    }
                                }
                            }
    						public static void main(String args[])
    						{
    						CreaXML crea = new CreaXML(args[0]);
                            }
    }
    qualcuno dove e cosa devo aggiungere per ottenere anche

    <?xml-stylesheet type="text/xsl" href="miofile.xsl"?>

  3. #3
    Utente bannato
    Registrato dal
    Feb 2005
    Messaggi
    43
    nessuno sà aiutarmi?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.