Questo è il mio progetto:
codice:
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import javax.xml.parsers.*;
import java.io.*;
/** DomDocumentCleanParser: versione di DomDocumentParser
con output dei soli elementi di interesse per il nostro esempio.
*/
public class Dom
{
public static org.w3c.dom.Node getChild(org.w3c.dom.Node parentNode, int childIndex)
{
org.w3c.dom.Node childNode = parentNode.getChildNodes().item(childIndex);
return childNode;
}
/* Array che mappa i tipi di elemento con l'indice int corrispondente
* alle specifiche per i node types di:
* Document Object Model (DOM) Level 2 Core Specification
* (http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113)
*/
static final String[] typeName = { "none", "Element", "Attr", "Text", "CDATA", "EntityRef", "Entity", "ProcInstr", "Comment", "Document", "DocType", "DocFragment", "Notation" };
public static void main(String[] args)
{
if ( args.length != 1 )
{
System.err.println("Usage: java Dom nomefile.xml");
System.exit(1);
}
Dom ddp = new Dom();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try
{
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("Prova.txt", true)));
DocumentBuilder builder = dbf.newDocumentBuilder();
Document document = builder.parse(new File(args[0]));
ddp.printNodeInfo(document);
pw.close();
}
catch (FileNotFoundException ex)
{
//Gestione eccezione es.
ex.printStackTrace();
}
catch (SAXException sxe)
{
Exception x = sxe;
if ( sxe.getException() != null )
x = sxe.getException();
x.printStackTrace();
}
catch (ParserConfigurationException pce)
{
pce.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
// main
/** printNodeInfo(Node node)
* Metodo che esplora l'albero Dom ricorsivamente e stampa a video
* le informazioni sugli elementi.
*/
private void printNodeInfo(Node currentNode)
{
String sNodeName = currentNode.getNodeName();
int iNodeType = currentNode.getNodeType();
String sNodeValue = currentNode.getNodeValue();
int iChildNumber = currentNode.getChildNodes().getLength();
NamedNodeMap nnmAttributes = currentNode.getAttributes();
System.out.print("\n");
System.out.println("Nome nodo: " + sNodeName);
System.out.println("Tipo nodo: " + typeName[iNodeType]);
System.out.println("Valore nodo: " + sNodeValue);
System.out.println("Numero figli: " + iChildNumber);
System.out.println("Attributi: " + printAttributes(nnmAttributes));
//Se non si tratta di una foglia continua l'esplorazione ricorsivamente
if ( iChildNumber > 0 )
{
NodeList nlChilds = currentNode.getChildNodes();
for ( int iChild = 0; iChild < iChildNumber; iChild++)
{
printNodeInfo(nlChilds.item(iChild));
}
}
}
/** printAttributes(NamedNodeMap nnm)
* Metodo utilizzato per la formattazione degli attributi ricavati da
* un elemento.
*/
private static String printAttributes(NamedNodeMap nnm)
{
String sAttrList = new String();
if ( nnm != null && nnm.getLength() > 0 )
{
for ( int iAttr = 0; iAttr < nnm.getLength(); iAttr++)
{
sAttrList += nnm.item(iAttr).getNodeName();
sAttrList += "=";
sAttrList += nnm.item(iAttr).getNodeValue();
sAttrList += "; ";
}
return sAttrList;
}
else
{
return "assenti";
}
}
}
Vorrei trasformare i System.out.prinln();
in salvataggio su file.
Nel try del MAIN ho creato il file con questa
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("Prova.txt", true)));
ed ho creato l'eccezione FileNotFoundException
Ora mi chiedo:
come faccio a dire in
private void printNodeInfo
che vorrei pw.prinln() al posto di System.out.println()????
O più semplicemente come si fa ad importare l'oggetto PrintWriter pw del MAIN in un'altra funziona privata void???
Spero sappiate aiutarmi, grazie per coloro che mi risponderanno...