Page 1 of 1

can I just don't load contents of "w:instrText" into Text?

PostPosted: Thu Aug 06, 2009 4:30 pm
by dqkit
I have questions like that can I just don't set the contents of "w:instrText" into org.docx4j.wml.Text value?
I got some strings such as HYPERLINK \l "_Toc229233488",PAGEREF _Toc229233488 \h, and so on, I want to remove them from the begining, maybe when docx4j loading the docx file, can we just skip <w:instrText xml:space="preserve"> ?
Thanks.

Re: can I just don't load contents of "w:instrText" into Text?

PostPosted: Thu Aug 06, 2009 6:26 pm
by jason
Sorry, I not sure I fully understand the question.

Could you please post the snippet of incoming WordML, and the desired result?

thanks .. Jason

Re: can I just don't load contents of "w:instrText" into Text?

PostPosted: Fri Aug 07, 2009 4:04 am
by dqkit
jason, I am so sorry that I didn't descripe my question clearly.
here is the xml content :
Code: Select all
      <w:p w:rsidR="00B625A1">
         <w:pPr>
            <w:pStyle w:val="TOC1"></w:pStyle>
            <w:tabs>
               <w:tab w:val="right" w:leader="dot" w:pos="9174"></w:tab>
            </w:tabs>
            <w:rPr>
               <w:noProof></w:noProof>
               <w:sz w:val="21"></w:sz>
            </w:rPr>
         </w:pPr>
         <w:r>
            <w:rPr>
               <w:rStyle w:val="Hyperlink"></w:rStyle>
               <w:noProof></w:noProof>
            </w:rPr>
            <w:fldChar w:fldCharType="begin"></w:fldChar>
         </w:r>
         <w:r w:rsidRPr="001A0CFA">
            <w:rPr>
               <w:rStyle w:val="Hyperlink"></w:rStyle>
               <w:noProof></w:noProof>
            </w:rPr>
            <w:instrText xml:space="preserve"> </w:instrText>
         </w:r>
         <w:r>
            <w:rPr>
               <w:noProof></w:noProof>
            </w:rPr>
            <w:instrText>HYPERLINK \l "_Toc229233425"</w:instrText>
         </w:r>
         <w:r w:rsidRPr="001A0CFA">
            <w:rPr>
               <w:rStyle w:val="Hyperlink"></w:rStyle>
               <w:noProof></w:noProof>
            </w:rPr>
            <w:instrText xml:space="preserve"> </w:instrText>
         </w:r>
         <w:r>
            <w:rPr>
               <w:rStyle w:val="Hyperlink"></w:rStyle>
               <w:noProof></w:noProof>
            </w:rPr>
            <w:fldChar w:fldCharType="separate"></w:fldChar>
         </w:r>
         <w:r w:rsidRPr="001A0CFA">
            <w:rPr>
               <w:rStyle w:val="Hyperlink"></w:rStyle>
               <w:noProof></w:noProof>
            </w:rPr>
            <w:t xml:space="preserve">1 </w:t>
         </w:r>
         <w:r w:rsidRPr="001A0CFA">
            <w:rPr>
               <w:rStyle w:val="Hyperlink"></w:rStyle>
               <w:rFonts w:hint="eastAsia"></w:rFonts>
               <w:noProof></w:noProof>
            </w:rPr>
            <w:t>绪论</w:t>
         </w:r>
         <w:r>
            <w:rPr>
               <w:noProof></w:noProof>
            </w:rPr>
            <w:tab></w:tab>
         </w:r>
         <w:r>
            <w:rPr>
               <w:noProof></w:noProof>
            </w:rPr>
            <w:fldChar w:fldCharType="begin"></w:fldChar>
         </w:r>
         <w:r>
            <w:rPr>
               <w:noProof></w:noProof>
            </w:rPr>
            <w:instrText xml:space="preserve"> PAGEREF _Toc229233425 \h </w:instrText>
         </w:r>
         <w:r>
            <w:rPr>
               <w:noProof></w:noProof>
            </w:rPr>
            <w:fldChar w:fldCharType="separate"></w:fldChar>
         </w:r>
         <w:r>
            <w:rPr>
               <w:noProof></w:noProof>
            </w:rPr>
            <w:t>1</w:t>
         </w:r>
         <w:r>
            <w:rPr>
               <w:noProof></w:noProof>
            </w:rPr>
            <w:fldChar w:fldCharType="end"></w:fldChar>
         </w:r>
         <w:r>
            <w:rPr>
               <w:rStyle w:val="Hyperlink"></w:rStyle>
               <w:noProof></w:noProof>
            </w:rPr>
            <w:fldChar w:fldCharType="end"></w:fldChar>
         </w:r>
      </w:p>
      


I use the following code to get the text:
Code: Select all
   public static String getText(P p) {
      StringBuilder result = new StringBuilder();
      if (!KKUtils.isEmpty(p)) {
         for (Object o : p.getParagraphContent()) {
            if (o instanceof org.docx4j.wml.R) {
               org.docx4j.wml.R r = (org.docx4j.wml.R) o;
               result.append(getRunContentString(r));
            } else {
               // System.out.println("DocxUtil:
               // "+o.getClass().getName());
               if (o instanceof javax.xml.bind.JAXBElement) {
                  // System.out.println(((JAXBElement)
                  // o).getDeclaredType());
                  if (((JAXBElement) o).getDeclaredType().getName().equals("org.docx4j.wml.P$Hyperlink")) {
                     org.docx4j.wml.P.Hyperlink link = (org.docx4j.wml.P.Hyperlink) ((JAXBElement) o).getValue();
                     for (Object o2 : link.getParagraphContent()) {
                        if (o2 instanceof org.docx4j.wml.R) {
                           org.docx4j.wml.R r2 = (org.docx4j.wml.R) o2;
                           result.append(getRunContentString(r2));
                        }
                     }
                  }
               }
            }
         }
      }
      // System.out.println(result.toString());
      return result.toString();
   }

   public static String getRunContentString(org.docx4j.wml.R r) {
      StringBuilder result = new StringBuilder();
      List<Object> runContent = r.getRunContent();
      for (Object o2 : runContent) {
         if (o2 instanceof javax.xml.bind.JAXBElement) {
            // directly to Text.
            if (((JAXBElement) o2).getDeclaredType().getName().equals("org.docx4j.wml.Text")) {
               org.docx4j.wml.Text t = (org.docx4j.wml.Text) ((JAXBElement) o2).getValue();
               result.append(t.getValue());
            }
         }
      }
      return result.toString();
   }


I got text :
HYPERLINK \l "_Toc229233425" 1 绪论 PAGEREF _Toc229233425 \h 1

what I really want is 1 绪论 1. That is mean org.docx4j.wml.Text would contain the w:instrText value. w:instrText is field codes, so I don't want that field codes when I using org.docx4j.wml.Text.getValue().
what should I do ?
Thanks for your reply.

Re: can I just don't load contents of "w:instrText" into Tex

PostPosted: Wed Apr 03, 2013 7:24 pm
by eltayeb
you can use this


((javax.xml.bind.JAXBElement)((org.docx4j.wml.Text) o).getParent()).getName().getLocalPart()