xxdavide84xx
05-12-2007, 15:16
Io ho questo programma che mi crea un file .txt e voglio ora analizzarlo e fare ulteriori controlli.
Mi conviene scrivere un nuovo programma, oppure implementare ed aumentare questo già in uso (qui sotto)?
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
{
int cont = 0;
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("Tesi.txt", false)));
DocumentBuilder builder = dbf.newDocumentBuilder();
Document document = builder.parse(new File(args[0]));
ddp.printNodeInfo(pw, document, cont);
pw.close();
}
catch (FileNotFoundException fnf)
{
fnf.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(PrintWriter pw, Node currentNode, int cont)
{
String sNodeName = currentNode.getNodeName();
int iNodeType = currentNode.getNodeType();
String sNodeValue = currentNode.getNodeValue();
int iChildNumber = currentNode.getChildNodes().getLength();
NamedNodeMap nnmAttributes = currentNode.getAttributes();
if ( sNodeName != "#document" && cont != 1 )
cont = 0;
if ( sNodeName == "w:styles" || sNodeName == "w:docPr" || sNodeName == "w:body" )
cont = 1;
if ( cont == 1 )
{
pw.flush();
pw.println("Nome nodo: " + sNodeName);
pw.flush();
pw.println("Tipo nodo: " + typeName[iNodeType]);
pw.flush();
if ( sNodeValue != null )
pw.println("Valore nodo: " + sNodeValue);
pw.flush();
if ( iChildNumber != 0 )
pw.println("Numero figli: " + iChildNumber);
pw.flush();
if ( printAttributes(nnmAttributes) != "assenti" )
pw.println("Attributi: " + printAttributes(nnmAttributes));
pw.flush();
pw.println();
pw.flush();
}
//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(pw, nlChilds.item(iChild), cont);
}
}
}
/** 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";
}
}
}
In pratica il programma mi controlla un file XML e mi scrive tutti i nodi da style fino in fondo...
Io vorrei controllare che nel corpo del testo vi fossero alcune parole tipo
"bibbliografia", "Introduzione", etc,
ma la cosa più difficile è riuscire a controllare che il testo rispetti Times New Roman 12 o 13, in quanto lo stile viene memorizzato prima con tutte le opzioni e viene solo richiamato nel testo....
qualcuno che conosce un pò anche XML potrebbe aiutarmi, grazie!
Mi conviene scrivere un nuovo programma, oppure implementare ed aumentare questo già in uso (qui sotto)?
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
{
int cont = 0;
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("Tesi.txt", false)));
DocumentBuilder builder = dbf.newDocumentBuilder();
Document document = builder.parse(new File(args[0]));
ddp.printNodeInfo(pw, document, cont);
pw.close();
}
catch (FileNotFoundException fnf)
{
fnf.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(PrintWriter pw, Node currentNode, int cont)
{
String sNodeName = currentNode.getNodeName();
int iNodeType = currentNode.getNodeType();
String sNodeValue = currentNode.getNodeValue();
int iChildNumber = currentNode.getChildNodes().getLength();
NamedNodeMap nnmAttributes = currentNode.getAttributes();
if ( sNodeName != "#document" && cont != 1 )
cont = 0;
if ( sNodeName == "w:styles" || sNodeName == "w:docPr" || sNodeName == "w:body" )
cont = 1;
if ( cont == 1 )
{
pw.flush();
pw.println("Nome nodo: " + sNodeName);
pw.flush();
pw.println("Tipo nodo: " + typeName[iNodeType]);
pw.flush();
if ( sNodeValue != null )
pw.println("Valore nodo: " + sNodeValue);
pw.flush();
if ( iChildNumber != 0 )
pw.println("Numero figli: " + iChildNumber);
pw.flush();
if ( printAttributes(nnmAttributes) != "assenti" )
pw.println("Attributi: " + printAttributes(nnmAttributes));
pw.flush();
pw.println();
pw.flush();
}
//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(pw, nlChilds.item(iChild), cont);
}
}
}
/** 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";
}
}
}
In pratica il programma mi controlla un file XML e mi scrive tutti i nodi da style fino in fondo...
Io vorrei controllare che nel corpo del testo vi fossero alcune parole tipo
"bibbliografia", "Introduzione", etc,
ma la cosa più difficile è riuscire a controllare che il testo rispetti Times New Roman 12 o 13, in quanto lo stile viene memorizzato prima con tutte le opzioni e viene solo richiamato nel testo....
qualcuno che conosce un pò anche XML potrebbe aiutarmi, grazie!