how to get complete text which is in a docx file table cell into a single string
I am trying to get the docx file table data in java code using docx4j api. Here i am trying to get the each cell data at a time .how to get that data..here i am placing my code which have recursive method calls.
static void walkList1(List children) {
i=children.size();
int i=1;
for (Object o : children) {
if (o instanceof javax.xml.bind.JAXBElement) {
if (((JAXBElement) o).getDeclaredType().getName()
.equals("org.docx4j.wml.Text")) {
org.docx4j.wml.Text t = (org.docx4j.wml.Text) ((JAXBElement) o)
.getValue();
System.out.println(" 1 1 " + t.getValue());
}
}
else if (o instanceof org.docx4j.wml.R) {
org.docx4j.wml.R run = (org.docx4j.wml.R) o;
walkList1开发者_如何学JAVA(run.getRunContent());
} else {
System.out.println(" IGNORED " + o.getClass().getName());
}
}
}
This part looks suspicious:
i=children.size();
int i=1;
The first must be a mutable static field (because otherwise your code will not compile) which is usually a bad idea. The second is local to the method but is never used.
If you are trying to combine all content into a single String
I suggest you create a StringBuilder
and pass that to your recursive calls, e.g.:
static String walkList(List children) {
StringBuilder dst = new StringBuilder();
walkList1(children, dst);
return dst.toString();
}
static void walkList1(List children, StringBuilder dst) {
for (Object o : children) {
if (o instanceof javax.xml.bind.JAXBElement) {
if (((JAXBElement) o).getDeclaredType().getName()
.equals("org.docx4j.wml.Text")) {
org.docx4j.wml.Text t = (org.docx4j.wml.Text) ((JAXBElement) o)
.getValue();
dst.append(t);
}
}
else if (o instanceof org.docx4j.wml.R) {
org.docx4j.wml.R run = (org.docx4j.wml.R) o;
walkList1(run.getRunContent(), dst);
} else {
System.out.println(" IGNORED " + o.getClass().getName());
}
}
}
Also List<T>
and JAXBElement<T>
are generic types. Is there any reason to use the raw types?
精彩评论