xxdavide84xx
16-01-2008, 18:32
Salve.
Io ho una
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 )
if ( grassetto != null )
buffer.append(" Grassetto body " + grassetto + "\n");
if ( italico != null )
buffer.append(" Italico body " + italico + "\n");
buffer.append(" Testo body: " + testo + "\n");
return buffer.toString();
}
}
con questo funzione il problema è bodyObjects.addElement(body); che mi memorizza sempre lo stesso NODO...
cerco di spiegarmi meglio io per es ho:
w:p
w:rPr 1
w:r ciao
w:r Gino
w:p
w:rPr 2
w:r come
w:r stai
w:r ?
e al posto che restituirmi come sopra il mio programma restituisce:
w:p
w:rPr 1 w:r ciao
w:rPr 1 w:r ciao
w:p
w:rPr 2 w:r come
w:rPr 2 w:r come
w:rPr 2 w:r come
il pezzo in cui penso sia il problema:
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");
}
NodeList pNode = getAllChildNode(bodyNode, "w:r");
for ( int j = 0; j < pNode.getLength(); j++) {
Node rNode = (Node)pNode.item(j);
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);
}
}
}
questo il programma completo:
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class aa {
Vector bodyNodes = new Vector();
Vector bodyObjects = new Vector();
public aa() {
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("Mercoledì 161.txt", false)));
document = builder.parse(new File("C:\\Documents and settings\\Administrator\\Desktop\\DAVIDE\\Dom\\tesi.xml"));
findBodyNodes(document);
createBodyObjects();
printBody(pw);
pw.close();
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
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 aa();
}
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 = (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");
}
NodeList pNode = getAllChildNode(bodyNode, "w:r");
for ( int j = 0; j < pNode.getLength(); j++) {
Node rNode = (Node)pNode.item(j);
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 )
if ( grassetto != null )
buffer.append(" Grassetto body " + grassetto + "\n");
if ( italico != null )
buffer.append(" Italico body " + italico + "\n");
buffer.append(" Testo body: " + testo + "\n");
return buffer.toString();
}
}
private NodeList getAllChildNode(Node node, String nodeName) {
ArrayNodeList arrNodeList = new ArrayNodeList();
NodeList childNodes = node.getChildNodes();
for ( int i = 0; i < childNodes.getLength(); i++) {
Node childNode = childNodes.item(i);
if ( childNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE && childNode.getNodeName().equals(nodeName) ) {
arrNodeList.add(childNode);
}
}
return arrNodeList;
}
class ArrayNodeList implements NodeList {
private ArrayList nodesArray = new ArrayList();
public void add(Node n) {
nodesArray.add(n);
}
public int getLength() {
return nodesArray.size();
}
public Node item(int index) {
try {
return (Node)nodesArray.get(index);
}
catch (IndexOutOfBoundsException e) {
return null;
}
}
}
}
Io ho una
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 )
if ( grassetto != null )
buffer.append(" Grassetto body " + grassetto + "\n");
if ( italico != null )
buffer.append(" Italico body " + italico + "\n");
buffer.append(" Testo body: " + testo + "\n");
return buffer.toString();
}
}
con questo funzione il problema è bodyObjects.addElement(body); che mi memorizza sempre lo stesso NODO...
cerco di spiegarmi meglio io per es ho:
w:p
w:rPr 1
w:r ciao
w:r Gino
w:p
w:rPr 2
w:r come
w:r stai
w:r ?
e al posto che restituirmi come sopra il mio programma restituisce:
w:p
w:rPr 1 w:r ciao
w:rPr 1 w:r ciao
w:p
w:rPr 2 w:r come
w:rPr 2 w:r come
w:rPr 2 w:r come
il pezzo in cui penso sia il problema:
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");
}
NodeList pNode = getAllChildNode(bodyNode, "w:r");
for ( int j = 0; j < pNode.getLength(); j++) {
Node rNode = (Node)pNode.item(j);
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);
}
}
}
questo il programma completo:
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class aa {
Vector bodyNodes = new Vector();
Vector bodyObjects = new Vector();
public aa() {
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("Mercoledì 161.txt", false)));
document = builder.parse(new File("C:\\Documents and settings\\Administrator\\Desktop\\DAVIDE\\Dom\\tesi.xml"));
findBodyNodes(document);
createBodyObjects();
printBody(pw);
pw.close();
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
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 aa();
}
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 = (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");
}
NodeList pNode = getAllChildNode(bodyNode, "w:r");
for ( int j = 0; j < pNode.getLength(); j++) {
Node rNode = (Node)pNode.item(j);
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 )
if ( grassetto != null )
buffer.append(" Grassetto body " + grassetto + "\n");
if ( italico != null )
buffer.append(" Italico body " + italico + "\n");
buffer.append(" Testo body: " + testo + "\n");
return buffer.toString();
}
}
private NodeList getAllChildNode(Node node, String nodeName) {
ArrayNodeList arrNodeList = new ArrayNodeList();
NodeList childNodes = node.getChildNodes();
for ( int i = 0; i < childNodes.getLength(); i++) {
Node childNode = childNodes.item(i);
if ( childNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE && childNode.getNodeName().equals(nodeName) ) {
arrNodeList.add(childNode);
}
}
return arrNodeList;
}
class ArrayNodeList implements NodeList {
private ArrayList nodesArray = new ArrayList();
public void add(Node n) {
nodesArray.add(n);
}
public int getLength() {
return nodesArray.size();
}
public Node item(int index) {
try {
return (Node)nodesArray.get(index);
}
catch (IndexOutOfBoundsException e) {
return null;
}
}
}
}