2011-01-31 10 views
0

docx4j apiを使用してdocxファイルの表データをJavaコードで取得しようとしています。 ここでは、各セルのデータを一度に取得しようとしています。そのデータを取得するには、再帰的メソッド呼び出しを持つコードを配置しています。docxファイルの表のセルにある完全なテキストを単一の文字列に変換する方法

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(run.getRunContent()); 
     } else { 
      System.out.println(" IGNORED " + o.getClass().getName()); 
     } 
    } 
} 

答えて

0

この部分は疑わしい:

i=children.size(); 
int i=1; 

(そうでない場合は、あなたのコードがコンパイルされませんので)変更可能な静的フィールドである必要があります最初は通常は悪い考えです。 2番目はメソッドのローカルですが、使用されることはありません。

あなたは、単一のStringにすべてのコンテンツを組み合わせしようとしている場合、私はあなたが

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()); 
     } 
    } 
} 

はまたList<T>JAXBElement<T>ジェネリック型である:例えば、StringBuilderを作成し、再帰呼び出しにそれを渡すことをお勧めします。生の型を使用する理由はありますか?

関連する問題