获取docx4j中内容控件的标题或别名



我想知道是否有可能获得内容控件的标题。这是很容易得到的标签,但标题称为别名在XML是不可达的。

当我询问类时,我得到的是"javax.xml.bind.JAXBElement"

这是我想要在xml

中的内容
<w:sdt>
<w:sdtPr>
<w:rPr>
<w:rStyle w:val="Calibri8"/>
</w:rPr>
<w:alias w:val="The title"/>            <== I want this little guy
<w:tag w:val="RULE["BaseSalary"]"/>
<w:id w:val="51973609"/>
<w:placeholder>
<w:docPart w:val="DefaultPlaceholder_1081868574"/>
</w:placeholder> 
这是我如何得到我的标签:
for (Object alias : al) {   
                    if ( alias.getClass().toString().contains("org.docx4j.wml.Tag")) {
                        //gets the Tag
                        String CTagVal = ((org.docx4j.wml.Tag) alias).getVal();
                        // If the tag contain ....
                        if (CTagVal.contains("RULE") || CTagVal.contains("CAL") )  {  
        ...........................
这真的很容易得到标签,因为有一个类叫标签,但为什么不存在"别名"类?但更重要的是,有办法得到它吗?还是? ?提前致谢

你也可以这样做:

// This is for the alias
@SuppressWarnings({ "unchecked", "rawtypes" })
     Alias getAlias(SdtPr element) {
        for (Object o : element.getRPrOrAliasOrLock()) {
            if (o instanceof JAXBElement  && ((JAXBElement)o).getValue() instanceof Alias) {
                return ((JAXBElement<Alias>)o).getValue();
            }
        }
        return null;
    }
    // this is for the tag
     Tag getTag(SdtPr element) {
        for (Object o : element.getRPrOrAliasOrLock()) {
            if (o instanceof Tag) {
                return (Tag) o;
            }
        }
        return null;
    }

当然你需要发送你的sdtPr元素:你可以这样做:

for(SdtElement sdtElement: listOfSdtElements){  // if you have multiple sdtelements
                SdtPr pr = sdtElement.getSdtPr();
                //Gets tags and alias
                Tag tag = getTag( pr);
                String tagVal = "";
                Alias alias = getAlias( pr);
                // if it is indeed an alias
                if(alias!=null){  // needed or else nullexception
                    String aliasVal = alias.getVal();
                    if(tag != null){  // needed or else nullexception
                        //gets the Tag
                        tagVal = tag.getVal();
                    }
             .... ....... ...... ..... 
                 }
   }

想知道答案的人!!

static Alias getAlias(SdtPr element) {
      for (Object o : element.getRPrOrAliasOrLock()) {
       if (o instanceof JAXBElement  && ((JAXBElement)o).getValue() instanceof Alias) {
        return ((JAXBElement<Alias>)o).getValue();
       }
      }
      return null;
     }

                            // for all elements get Tags and title
                            for (Object elem : al) {   

                                org.docx4j.wml.SdtPr.Alias hello = null;

                                if (elem.getClass().toString().contains("avax.xml.bind.JAXBElement") &&
                                        ((javax.xml.bind.JAXBElement) elem).getValue().toString().contains("Alias")) {

                                    hello =  (Alias) ((javax.xml.bind.JAXBElement) elem).getValue();
                                    System.out.println( hello.getVal() );

                                }
                                // tag part much easier
                                else if ( elem.getClass().toString().contains("org.docx4j.wml.Tag")) {
                                    //gets the Tag
                                    String CTagVal = ((org.docx4j.wml.Tag) elem).getVal();
                                                   ..........
                               }
                            }   

最新更新