Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    Come eggere da file e fare dei controlli?

    Come fare per leggere da file e poi eseguire i controlli?

    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?
    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
        {
          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";
        }
      }
    }

  2. #2
    Utente di HTML.it L'avatar di energie
    Registrato dal
    Apr 2003
    Messaggi
    446
    ma di che controlli parliamo?

  3. #3
    Mi viene creato un file del genere sull'XML.
    Ora io dovrò dire se i nodi w:t Text sono Times New Romans 24 o 26 (e devono essere o 24 per tutto il testo o 26), poi che i titoli devono essere in Times New Romans (maggiori di 26) e che devono essere in grossetto(ossia w:b).
    Se vi è un tag Bibliografia e uno Indice....
    Ecco questi sono i controlli.
    Quest'esempio vi è solo la parola ciao scritta normale, quindi non fa molto testo, ma è solo per fare capire cosa viene fuori dal parser di un XML....
    codice:
    Nome nodo: w:styles
    Tipo nodo: Element
    Numero figli: 6
    
    Nome nodo: w:versionOfBuiltInStylenames
    Tipo nodo: Element
    Attributi: w:val=4; 
    
    Nome nodo: w:latentStyles
    Tipo nodo: Element
    Attributi: w:defLockedState=off; w:latentStyleCount=156; 
    
    Nome nodo: w:style
    Tipo nodo: Element
    Numero figli: 3
    Attributi: w:default=on; w:styleId=Normale; w:type=paragraph; 
    
    Nome nodo: w:name
    Tipo nodo: Element
    Attributi: w:val=Normal; 
    
    Nome nodo: wx:uiName
    Tipo nodo: Element
    Attributi: wx:val=Normale; 
    
    Nome nodo: w:rPr
    Tipo nodo: Element
    Numero figli: 4
    
    Nome nodo: wx:font
    Tipo nodo: Element
    Attributi: wx:val=Times New Roman; 
    
    Nome nodo: w:sz
    Tipo nodo: Element
    Attributi: w:val=24; 
    
    Nome nodo: w:sz-cs
    Tipo nodo: Element
    Attributi: w:val=24; 
    
    Nome nodo: w:lang
    Tipo nodo: Element
    Attributi: w:bidi=AR-SA; w:fareast=IT; w:val=IT; 
    
    Nome nodo: w:style
    Tipo nodo: Element
    Numero figli: 3
    Attributi: w:default=on; w:styleId=Carpredefinitoparagrafo; w:type=character; 
    
    Nome nodo: w:name
    Tipo nodo: Element
    Attributi: w:val=Default Paragraph Font; 
    
    Nome nodo: wx:uiName
    Tipo nodo: Element
    Attributi: wx:val=Car. predefinito paragrafo; 
    
    Nome nodo: w:semiHidden
    Tipo nodo: Element
    
    Nome nodo: w:style
    Tipo nodo: Element
    Numero figli: 5
    Attributi: w:default=on; w:styleId=Tabellanormale; w:type=table; 
    
    Nome nodo: w:name
    Tipo nodo: Element
    Attributi: w:val=Normal Table; 
    
    Nome nodo: wx:uiName
    Tipo nodo: Element
    Attributi: wx:val=Tabella normale; 
    
    Nome nodo: w:semiHidden
    Tipo nodo: Element
    
    Nome nodo: w:rPr
    Tipo nodo: Element
    Numero figli: 1
    
    Nome nodo: wx:font
    Tipo nodo: Element
    Attributi: wx:val=Times New Roman; 
    
    Nome nodo: w:tblPr
    Tipo nodo: Element
    Numero figli: 2
    
    Nome nodo: w:tblInd
    Tipo nodo: Element
    Attributi: w:type=dxa; w:w=0; 
    
    Nome nodo: w:tblCellMar
    Tipo nodo: Element
    Numero figli: 4
    
    Nome nodo: w:top
    Tipo nodo: Element
    Attributi: w:type=dxa; w:w=0; 
    
    Nome nodo: w:left
    Tipo nodo: Element
    Attributi: w:type=dxa; w:w=108; 
    
    Nome nodo: w:bottom
    Tipo nodo: Element
    Attributi: w:type=dxa; w:w=0; 
    
    Nome nodo: w:right
    Tipo nodo: Element
    Attributi: w:type=dxa; w:w=108; 
    
    Nome nodo: w:style
    Tipo nodo: Element
    Numero figli: 3
    Attributi: w:default=on; w:styleId=Nessunelenco; w:type=list; 
    
    Nome nodo: w:name
    Tipo nodo: Element
    Attributi: w:val=No List; 
    
    Nome nodo: wx:uiName
    Tipo nodo: Element
    Attributi: wx:val=Nessun elenco; 
    
    Nome nodo: w:semiHidden
    Tipo nodo: Element
    
    Nome nodo: w:docPr
    Tipo nodo: Element
    Numero figli: 14
    
    Nome nodo: w:view
    Tipo nodo: Element
    Attributi: w:val=print; 
    
    Nome nodo: w:zoom
    Tipo nodo: Element
    Attributi: w:percent=120; w:val=best-fit; 
    
    Nome nodo: w:doNotEmbedSystemFonts
    Tipo nodo: Element
    
    Nome nodo: w:attachedTemplate
    Tipo nodo: Element
    Attributi: w:val=; 
    
    Nome nodo: w:defaultTabStop
    Tipo nodo: Element
    Attributi: w:val=708; 
    
    Nome nodo: w:hyphenationZone
    Tipo nodo: Element
    Attributi: w:val=283; 
    
    Nome nodo: w:characterSpacingControl
    Tipo nodo: Element
    Attributi: w:val=DontCompress; 
    
    Nome nodo: w:optimizeForBrowser
    Tipo nodo: Element
    
    Nome nodo: w:validateAgainstSchema
    Tipo nodo: Element
    
    Nome nodo: w:saveInvalidXML
    Tipo nodo: Element
    Attributi: w:val=off; 
    
    Nome nodo: w:ignoreMixedContent
    Tipo nodo: Element
    Attributi: w:val=off; 
    
    Nome nodo: w:alwaysShowPlaceholderText
    Tipo nodo: Element
    Attributi: w:val=off; 
    
    Nome nodo: w:compat
    Tipo nodo: Element
    Numero figli: 2
    
    Nome nodo: w:dontAllowFieldEndSelect
    Tipo nodo: Element
    
    Nome nodo: w:useWord2002TableStyleRules
    Tipo nodo: Element
    
    Nome nodo: wsp:rsids
    Tipo nodo: Element
    Numero figli: 2
    
    Nome nodo: wsp:rsidRoot
    Tipo nodo: Element
    Attributi: wsp:val=007D4D10; 
    
    Nome nodo: wsp:rsid
    Tipo nodo: Element
    Attributi: wsp:val=007D4D10; 
    
    Nome nodo: w:body
    Tipo nodo: Element
    Numero figli: 1
    
    Nome nodo: wx:sect
    Tipo nodo: Element
    Numero figli: 2
    
    Nome nodo: w:p
    Tipo nodo: Element
    Numero figli: 1
    Attributi: wsp:rsidR=007D4D10; wsp:rsidRDefault=007D4D10; 
    
    Nome nodo: w:r
    Tipo nodo: Element
    Numero figli: 1
    
    Nome nodo: w:t
    Tipo nodo: Element
    Numero figli: 1
    
    Nome nodo: #text
    Tipo nodo: Text
    Valore nodo: ciao
    
    Nome nodo: w:sectPr
    Tipo nodo: Element
    Numero figli: 4
    Attributi: wsp:rsidR=007D4D10; 
    
    Nome nodo: w:pgSz
    Tipo nodo: Element
    Attributi: w:h=16838; w:w=11906; 
    
    Nome nodo: w:pgMar
    Tipo nodo: Element
    Attributi: w:bottom=1134; w:footer=708; w:gutter=0; w:header=708; w:left=1134; w:right=1134; w:top=1417; 
    
    Nome nodo: w:cols
    Tipo nodo: Element
    Attributi: w:space=708; 
    
    Nome nodo: w:docGrid
    Tipo nodo: Element
    Attributi: w:line-pitch=360;

  4. #4
    Secondo voi come mi conviene comportarmi scrivere un programma separato, oppure unirlo a questo che ho già?

  5. #5
    Qualcuno che sappia darmi qualche consiglio???

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 © 2025 vBulletin Solutions, Inc. All rights reserved.