PDA

View Full Version : Esperti java


texerasmo
29-06-2004, 14:12
Questa classe java modifica un xml gia esistente
Ma mi da errore

//


C:\javaxml>javac ModificaXML.java
ModificaXML.java:49: cannot resolve symbol
symbol : variable document
location: class ModificaXML
if(document != null)

//

import java.io.*;

/*
IMPORTIAMO I PACKAGE NECESSARI PER
LAVORATE CON GLI XML
/**/
import org.jdom.*;
import org.jdom.output.XMLOutputter;
import org.jdom.input.SAXBuilder;

public class ModificaXML
{
String nomefile;
public ModificaXML(String nomeFile)
{
this.nomefile = nomeFile;
}

public void modifica()
{
File f = new File(nomefile);

/*
ISTANZIAMO UN OGGETTO SAXBuilder
CHE CI PERMETTE DI PARSARE UN XML
/**/
SAXBuilder saxbuilder = new SAXBuilder
(
"org.apache.xerces.parsers.SAXParser",false

);

/*
ISTANZIAMO UN OGGETTO Document
/**/
Document documento = null;

try
{
/*
COSTRUIAMO UN Document DA UN XML
/**/
documento = saxbuilder.build(f);
}catch (JDOMException e)
{
System.err.println ("errore: "+e);
}

if(document != null)
{
/*
ISTANZIAMO UN OGGETTO Element
PRENDENDO IL RIFERIMENTO AD UN
SUPERTAG GIA' ESISTENTE
/**/
Element root = documento.getRootElement();

/*
ISTANZIAMO ALTRI OGGETTI Element
PASSANDOGLI LE STRINGHE
CHE SARANNO I NOMI DEI NOSTRI TAG
/**/
Element newinitparam = new Element("new-init-param");
Element paramname = new Element("param-name");
Element paramvalue = new Element("param-value");

/*
ASSEGNAMO DEI VALORI DI TIPO "CDATA"
AI TAG param-name E param-value:
<param-name>nuovo nome</param-name>
/**/
paramname.addContent(new CDATA(" nuovo nome"));
paramvalue.addContent(new CDATA(" nuovo valore"));

/*
AGGIUNGIAMO I TAG param-name E
Param-value AL TAG new-init-param
/**/
newinitparam.addContent(paramname);
newinitparam.addContent(paramvalue);

/*
AGGIUNGIAMO IL TAG new-init-param
AL SUPERTAG root
/**/
root.addContent(newinitparam);
}

BufferedWriter out = null;
try
{
/*
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.out.println("Error in producing file: " + e.getMessage());
}finally
{
try
{
out.close();
}catch(IOException e)
{
System.out.println("Error in closing file: " + e.getMessage());
}
}
}

public static void main (String[] args)
{
ModificaXML modifica = new ModificaXML (args[0]);
modifica.modifica();
}
}

cmq questo č il link dove ho prevo il codice
http://www.javaportal.it/docs/filexml.htm
Potete aiutarmi a risolvere questo pb che mi va avnti da gg

kingv
29-06-2004, 14:21
if(document o != null)

texerasmo
29-06-2004, 14:24
risolto
Document documento = null;

errato
if(document != null)

giusto
if(documento != null)

kingv
29-06-2004, 14:26
Originariamente inviato da texerasmo

giusto
if(documento != null)



e io che ho detto :wtf: :D

texerasmo
29-06-2004, 14:28
Ma questo errore cosa vuol dire?


C:\javaxml>java ModificaXML mino.xml
Exception in thread "main" java.lang.NullPointerException
at ModificaXML.modifica(ModificaXML.java:115)
at ModificaXML.main(ModificaXML.java:126)


try
{
out.close(); // line 115
}catch(IOException e)

public static void main (String[] args)
{
ModificaXML modifica = new ModificaXML (args[0]);
modifica.modifica();// line 126
}

kingv
29-06-2004, 14:33
Originariamente inviato da texerasmo

try
{
out.close(); // line 115
}


try
{
xout.close(); // line 115
}

ma ti perdi per strada le lettere? :D

texerasmo
29-06-2004, 14:42
Per la lettera di document quando mi sono collegato sul foprum non c'č la tua risp.


Mettendo xout mi da errore in complilazione ... :(

Questi due errori me li da quando lancio la classe



C:\javaxml>java ModificaXML mino.xml
Exception in thread "main" java.lang.NullPointerException
at ModificaXML.modifica(ModificaXML.java:115)
at ModificaXML.main(ModificaXML.java:126)




MI puoi aitare per favore?

kingv
29-06-2004, 14:55
a:
BufferedWriter out = null;



sostituisci

try
{
BufferedWriter out = new BufferedWriter(new FileWriter(f));

}
catch(IOException ioe)
{
ioe.printStackTrace();
}

texerasmo
29-06-2004, 15:01
Fatto ma ...
da due errori


C:\javaxml>javac ModificaXML.java
ModificaXML.java:117: cannot resolve symbol
symbol : variable out
location: class ModificaXML
xout.output(documento,out);
^
ModificaXML.java:127: cannot resolve symbol
symbol : variable out
location: class ModificaXML
out.close();
^
2 errors

kingv
29-06-2004, 15:04
ho sbagliato,prova cosė :

BufferedWriter out = null;
try
{
out = new BufferedWriter(new FileWriter(f));

}
catch(IOException ioe)
{
ioe.printStackTrace();
}

texerasmo
29-06-2004, 15:08
bella kingv
Grazie :D :D :D :D :D

sei stato gentilissimo;)

kingv
29-06-2004, 15:10
Originariamente inviato da texerasmo
bella kingv
Grazie :D :D :D :D :D



;)