0

I would like to know if it is possible to get the title of a content control. It is very easy to get the Tag but the title called alias in the XML is unreachable.

When I ask the class I get the mean "javax.xml.bind.JAXBElement"

Here is what I want in the 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> 

Here is how I get my Tag :

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") )  {  
        ...........................

It's really easy to get the Tag because there is a class called Tag but why doesn't "alias" class exists ? But more importantly is there a way to get it ? Or ??? Thx in advance

Igor Beaufils
  • 848
  • 2
  • 12
  • 29

2 Answers2

1

For those who want the answer !!

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

Or

                            // 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();
                                                   ..........
                               }
                            }   
Igor Beaufils
  • 848
  • 2
  • 12
  • 29
1

Well you could also do this :

// 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;
    }

Of course you need to send your sdtPr Element : You could do something like this :

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

                    }
             .... ....... ...... ..... 
                 }
   }
  • 1
    Thanks, that much cleaner then my own answer. We could even use XmlUtils.unwrap(o); before "if (o instanceof JAXBElement && ...." To avoid the usage of JAXBElement which is depreceated. – Igor Beaufils Aug 27 '15 at 09:15