Torna indietro   Hardware Upgrade Forum > Software > Programmazione

Gigabyte MO32U24 OLED: il 4K a 240Hz su un pannello OLED ideale per il gaming
Gigabyte MO32U24 OLED: il 4K a 240Hz su un pannello OLED ideale per il gaming
Pannello QD-OLED da 32 pollici con risoluzione 4K, frequenza di aggiornamento a 240Hz e tempi di risposta rapidissimi: il Gigabyte MO32U24 evolve il progetto del suo predecessore MO32U e alza ulteriormente l'asticella delle prestazioni. È ancora una volta un monitor indirizzato ai giocatori più esigenti
Recensione realme 16 5G: lo smartphone con Selfie Mirror ha una batteria da 6550mAh
Recensione realme 16 5G: lo smartphone con Selfie Mirror ha una batteria da 6550mAh
realme 16 5G è un nuovo smartphone con sensore Sony IMX 852 da 50MP sul retro e uno specchio selfie fisico integrato nella camera bar, una prima nel segmento di mercato. Batteria da 6550mAh in un corpo da 8,1mm e 183g, certificazione IP69K e ricarica da 45W completano un pacchetto aggressivo per la fascia media, per uno dei prodotti più interessanti del produttore sul piano commerciale
Come rispettare tutte le nuove regole per i monopattini elettrici? La guida per non rischiare sanzioni
Come rispettare tutte le nuove regole per i monopattini elettrici? La guida per non rischiare sanzioni
Sono ormai definitive le nuove norme del Codice della Strada per i monopattini elettrici. Non solo targa e assicurazione, le regole sono tante e riguardano diversi aspetti, vi spieghiamo come evitare sanzioni che possono essere salate
Tutti gli articoli Tutte le news

Vai al Forum
Rispondi
 
Strumenti
Old 14-01-2008, 23:30   #1
xxdavide84xx
Member
 
L'Avatar di xxdavide84xx
 
Iscritto dal: May 2006
Città: Cesenatico
Messaggi: 274
[JAVA] Come calcolare quanti figli w:r ha un nodo w:p?

Salve, ho fatto questo programma in java su un file xml creato da Word....
Codice:
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class XMLStyles1  {
  Vector styleNodes = new Vector();

  Vector styleObjects = new Vector();

  Vector rNodes = new Vector();

  Vector bodyNodes = new Vector();

  Vector bodyObjects = new Vector();

  public XMLStyles1() {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    try {
      builder = factory.newDocumentBuilder();
    }
    catch (ParserConfigurationException e) {
      e.printStackTrace();
      System.exit(1);
    }
    Document document = null;
    try {
      PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("Lunedì 145.txt", false)));
      document = builder.parse(new File("C:\\Documents and settings\\Administrator\\Desktop\\DAVIDE\\Dom\\tesi.xml"));
      findStyleNodes(document);
      createStyleObjects();
      printStyles(pw);
      findBodyNodes(document);
      createBodyObjects();
      printBody(pw);
      pw.close();
    }
    catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
  }

  private void findStyleNodes(Node startingNode) {
    NodeList childNodes = startingNode.getChildNodes();
    for ( int i = 0; i < childNodes.getLength(); i++) {
      Node actualNode = childNodes.item(i);
      if ( isStyleNode(actualNode) )
        styleNodes.addElement(actualNode);
      if ( actualNode.hasChildNodes() )
        findStyleNodes(actualNode);
    }
  }

  private boolean isStyleNode(Node node) {
    return node.getNodeName().equals("w:style");
  }

  private void createStyleObjects() {
    for ( int i = 0; i < styleNodes.size(); i++) {
      Node styleNode = (Node)styleNodes.elementAt(i);
      Style style = new Style();
      Node uinameNode = getChildNode(styleNode, "wx:uiName");
      if ( uinameNode != null )
        style.name = getAttributeValue(uinameNode, "wx:val");
      if ( uinameNode == null ) {
        Node nameNode = getChildNode(styleNode, "w:name");
        if ( nameNode != null )
          style.name = getAttributeValue(nameNode, "w:val");
      }
      Node basedNode = getChildNode(styleNode, "w:basedOn");
      if ( basedNode != null )
        style.based = getAttributeValue(basedNode, "w:val");
      Node pPrNode = getChildNode(styleNode, "w:pPr");
      if ( pPrNode != null ) {
        Node pstileNode = getChildNode(pPrNode, "w:pStyle");
        if ( pstileNode != null )
          style.pstile = getAttributeValue(pstileNode, "w:val");
        Node jcNode = getChildNode(pPrNode, "w:jc");
        if ( jcNode != null )
          style.jc = getAttributeValue(jcNode, "w:val");
      }
      Node paragraphNode = getChildNode(styleNode, "w:rPr");
      if ( paragraphNode != null ) {
        Node fontNode = getChildNode(paragraphNode, "wx:font");
        if ( fontNode != null )
          style.font = getAttributeValue(fontNode, "wx:val");
        Node sizeNode = getChildNode(paragraphNode, "w:sz");
        if ( sizeNode != null )
          style.size = getAttributeValue(sizeNode, "w:val");
        Node grassettoNode = getChildNode(paragraphNode, "w:b");
        if ( grassettoNode != null )
          style.grassetto = getAttributeValue(grassettoNode, "w:val");
        Node italicoNode = getChildNode(paragraphNode, "w:i");
        if ( italicoNode != null )
          style.italico = getAttributeValue(italicoNode, "w:val");
        Node sottolineatoNode = getChildNode(paragraphNode, "w:u");
        if ( sottolineatoNode != null )
          style.sottolineato = getAttributeValue(sottolineatoNode, "w:val");
      }
      styleObjects.addElement(style);
    }
  }

  private String getAttributeValue(Node node, String attributeName) {
    NamedNodeMap attributes = node.getAttributes();
    if ( attributes == null )
      return "";
    Node item = attributes.getNamedItem(attributeName);
    if ( item == null )
      return "";
    return item.getNodeValue();
  }

  private Node getChildNode(Node node, String nodeName) {
    NodeList childNodes = node.getChildNodes();
    for ( int i = 0; i < childNodes.getLength(); i++) {
      Node childNode = childNodes.item(i);
      if ( childNode.getNodeName().equals(nodeName) )
        return childNode;
    }
    return null;
  }

  private void printStyles(PrintWriter pw) {
    for ( int i = 0; i < styleObjects.size(); i++) {
      Style style = (Style)styleObjects.elementAt(i);
      System.out.println(style);
      pw.println(style);
    }
  }

  public static void main(String args[]) {
    new XMLStyles1();
  }

  class Style  {
    String name;

    String based;

    String pstile;

    String jc;

    String font;

    String size;

    String grassetto;

    String italico;

    String sottolineato;

    public String toString() {
      StringBuffer buffer = new StringBuffer();
      if ( name != null ) {
        buffer.append("Stile: " + name + "\n");
        if ( based != null )
          buffer.append(" Basato su: " + based + "\n");
        if ( pstile != null )
          buffer.append(" pStyle: " + pstile + "\n");
        if ( jc != null )
          buffer.append(" Jc: " + jc + "\n");
        if ( font != null )
          buffer.append(" Font: " + font + "\n");
        if ( size != null )
          buffer.append(" Dimensione: " + size + "\n");
        if ( grassetto != null )
          buffer.append(" Grassetto " + grassetto + "\n");
        if ( italico != null )
          buffer.append(" Italico " + italico + "\n");
        if ( sottolineato != null )
          buffer.append(" Sottolineato: " + sottolineato + "\n");
      }
      return buffer.toString();
    }
  }

  private void findBodyNodes(Node startingNode) {
    NodeList childNodes = startingNode.getChildNodes();
    for ( int i = 0; i < childNodes.getLength(); i++) {
      Node actualNode = childNodes.item(i);
      if ( isBodyNode(actualNode) )
        bodyNodes.addElement(actualNode);
      if ( isrNode(actualNode) )
        rNodes.addElement(actualNode);
      if ( actualNode.hasChildNodes() )
        findBodyNodes(actualNode);
    }
  }

  private boolean isBodyNode(Node node) {
    return node.getNodeName().equals("w:p");
  }

  private boolean isrNode(Node node) {
    return node.getNodeName().equals("w:r");
  }

  private void createBodyObjects() {
    for ( int i = 0; i < bodyNodes.size(); i++) {
      Node bodyNode = (Node)bodyNodes.elementAt(i);
      Body body = new Body();
      Node paragraphNode = getChildNode(bodyNode, "w:pPr");
      if ( paragraphNode != null ) {
        Node stileNode = getChildNode(paragraphNode, "w:pStyle");
        if ( stileNode != null )
          body.stile = getAttributeValue(stileNode, "w:val");
        Node jcNode = getChildNode(paragraphNode, "w:jc");
        if ( jcNode != null )
          body.jc = getAttributeValue(jcNode, "w:val");
      }
      bodyObjects.addElement(body);
    }
    for ( int j = 0; j < rNodes.size(); j++) {
      Node rNode = (Node)rNodes.elementAt(j);
      Body body = new Body();
      Node rPrNode = getChildNode(rNode, "w:rPr");
      if ( rPrNode != null ) {
        Node grassettoNode = getChildNode(rPrNode, "w:b");
        if ( grassettoNode != null )
          body.grassetto = getAttributeValue(grassettoNode, "w:val");
        Node italicoNode = getChildNode(rPrNode, "w:i");
        if ( italicoNode != null )
          body.italico = getAttributeValue(italicoNode, "w:val");
      }
      Node testoNode = getChildNode(rNode, "w:t");
      if ( testoNode != null ) {
        Node testoNode1 = getChildNode(testoNode, "#text");
        if ( testoNode1 != null )
          body.testo = testoNode1.getNodeValue();
      }
      bodyObjects.addElement(body);
    }
  }

  private void printBody(PrintWriter pw) {
    for ( int i = 0; i < bodyObjects.size(); i++) {
      Body body = (Body)bodyObjects.elementAt(i);
      System.out.println(body);
      pw.println(body);
    }
  }

  class Body  {
    String stile;

    String jc;

    String grassetto;

    String italico;

    String testo;

    public String toString() {
      StringBuffer buffer = new StringBuffer();
      if ( stile != null )
        buffer.append("Stile body: " + stile + "\n");
      if ( jc != null )
        buffer.append(" Jc body: " + jc + "\n");
      
        buffer.append(" Grassetto body " + grassetto + "\n");
      
        buffer.append(" Italico body " + italico + "\n");
     
        buffer.append(" Testo body: " + testo + "\n");
      return buffer.toString();
    }
  }
}
Ora avrei un piccolo problema da risolvere e non so proprio come fare...
Ho w:p che contiene 1 solo w:pPr e 1 o più w:r

Vorrei che mi stampasse tutto w:p, ossia w:pPr e poi i vari w:r (che variano da w:p a w:p)...
Io col mio programma prima stampo tutti i w:pPr e poi tutti i w:r....
Non riesco a concatenarli.....

Se proprio non volete mettere le mani in questo mio progetto, almeno potreste provare a dirmi che codice usare per calcolare quanti w:r ci sono in ogni w:p.....
GRAZIE!!!
__________________
CPU Intel i5-4590, Scheda Madre Asrock H97 Pro4, RAM DDR3 Corsair Vengeance 1600MHz 8GB CL9, Hard Disk WD Caviar Blue 1TB, SSD Crucial MX100 256GB.

Ultima modifica di xxdavide84xx : 14-01-2008 alle 23:31. Motivo: errore
xxdavide84xx è offline   Rispondi citando il messaggio o parte di esso
 Rispondi


Gigabyte MO32U24 OLED: il 4K a 240Hz su un pannello OLED ideale per il gaming Gigabyte MO32U24 OLED: il 4K a 240Hz su un panne...
Recensione realme 16 5G: lo smartphone con Selfie Mirror ha una batteria da 6550mAh Recensione realme 16 5G: lo smartphone con Selfi...
Come rispettare tutte le nuove regole per i monopattini elettrici? La guida per non rischiare sanzioni Come rispettare tutte le nuove regole per i mono...
DLSS 4.5: con Dynamic Frame Generation e MFG 6X NVIDIA alza la posta DLSS 4.5: con Dynamic Frame Generation e MFG 6X ...
Plaud NotePin S, il registratore IA si fa indossabile (ma è facile da perdere) Plaud NotePin S, il registratore IA si fa indoss...
Infineon apre il 2 luglio lo Smart Power...
Crimson Desert non si ferma: il gioco di...
Con iOS 27 l'iPhone si ripristina da sol...
Visa porta i pagamenti in ChatGPT: gli a...
OpenAI valuta un 'drastico' taglio dei p...
Il MacBook con display touch si far&agra...
Google promette di restituire più...
Quattro monitor 4K, doppia LAN 2.5G e Wi...
ROG Equalizer, il cavo 'salva-GPU': prim...
Falla critica CVSS 9.8 in Oracle PeopleS...
Microsoft accelera su Edge: aggiornament...
AMD ha corretto un bug da 10.000 dollari...
Vertiv: data center, la corsa dell’IA sp...
Siri non diventerà la tua fidanzata virt...
Prezzi in crescita del 200% e forniture ...
Chromium
GPU-Z
OCCT
LibreOffice Portable
Opera One Portable
Opera One 106
CCleaner Portable
CCleaner Standard
Cpu-Z
Driver NVIDIA GeForce 546.65 WHQL
SmartFTP
Trillian
Google Chrome Portable
Google Chrome 120
VirtualBox
Tutti gli articoli Tutte le news Tutti i download

Strumenti

Regole
Non Puoi aprire nuove discussioni
Non Puoi rispondere ai messaggi
Non Puoi allegare file
Non Puoi modificare i tuoi messaggi

Il codice vB è On
Le Faccine sono On
Il codice [IMG] è On
Il codice HTML è Off
Vai al Forum


Tutti gli orari sono GMT +1. Ora sono le: 02:02.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Served by www3v