Ho questo programma:
dentro wcodice:import java.io.*; import java.util.*; import javax.xml.parsers.*; import org.w3c.dom.*; public class XMLStyles { Vector<Node> styleNodes = new Vector<Node>(); Vector<Style> styleObjects = new Vector<Style>(); Vector<Node> bodyNodes = new Vector<Node>(); Vector<Body> bodyObjects = new Vector<Body>(); public XMLStyles() { 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("PROVATESI.txt", false))); document = builder.parse(new File("C:\\Documents and settings\\Administrator\\Desktop\\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 = 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 = styleObjects.elementAt(i); System.out.println(style); pw.println(style); } } public static void main(String args[]) { new XMLStyles(); } 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(actualNode.hasChildNodes()) findBodyNodes(actualNode); } } private boolean isBodyNode(Node node) { return node.getNodeName().equals("w:p"); } private void createBodyObjects() { for ( int i = 0; i < bodyNodes.size(); i++) { Node bodyNode = 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 rNode = getChildNode(bodyNode, "w:r"); if(rNode != null) { 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 = bodyObjects.elementAt(i); System.out.println(body); pw.println(body); } } class Body { String stile; String grassetto; String italico; String testo; public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append("Stile body: " + stile + "\n"); if (grassetto != null) buffer.append(" Grassetto body " + grassetto + "\n"); if (italico != null) buffer.append(" Italico body " + italico + "\n"); if (testo != null) buffer.append(" Testo body: " + testo + "\n"); return buffer.toString(); } } }nel body ho più di un w:r, ma mi analizza solo il primo...in verde la parte incriminata...so solo che la struttura è questa:
e per come esegue il programma creato da me prende solo "Obiettivo di questa tesi è".....invece dovrei prendere la frase intera.....codice:<w:p wsp:rsidR="00574618" wsp:rsidRDefault="00574618"> <w:pPr> <w:pStyle w:val="CorpoTesi"/> </w:pPr> <w:r> <w:t>Obiettivo di questa tesi è</w:t> </w:r> <aml:annotation aml:id="3" w:type="Word.Bookmark.Start" w:name="OLE_LINK1"/ <w:r> <w:t>l’</w:t> </w:r> <w:r wsp:rsidR="003A38B6"> <w:t>introduzione dei principali temi correlati con l’</w:t> </w:r> <w:r wsp:rsidR="00C02ABE"> <w:t>accessibilità del Web</w:t> </w:r> <aml:annotation aml:id="3" w:type="Word.Bookmark.End"/> <w:r wsp:rsidR="00C02ABE"> <w:t>, sia in termini di</w:t> </w:r> <w:r wsp:rsidR="003B5A40"> <w:t>standard e di</w:t> </w:r> <w:r wsp:rsidR="00C02ABE"> <w:t>linee guida</w:t> </w:r> <w:r wsp:rsidR="003B5A40"> <w:t>,</w:t> </w:r> <w:r wsp:rsidR="00C02ABE"> <w:t>sia in termini di</w:t> </w:r> <w:r wsp:rsidR="007E0346"> <w:t>normative e di leggi attualmente in vigore</w:t> </w:r> <w:r wsp:rsidR="00C02ABE"> <w:t>.</w:t> </w:r> </w:p>
Qualcuno saprebbe suggerirmi una soluzione?
Un FOR per caso? Non so bene come gestirlo col buffer....

nel body ho più di un w:r, ma mi analizza solo il primo...in verde la parte incriminata...so solo che la struttura è questa:
Rispondi quotando