gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:27:33 25 4
gpt4 key购买 nike

我想知道是否可以获得内容控件的标题。获取Tag很容易,但是无法获取XML中名为alias的标题。

当我询问类(class)时,我得到的意思是“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") ) {
...........................

获取Tag确实很容易,因为有一个名为Tag的类,但为什么不存在“alias”类?但更重要的是有没有办法得到它?或者 ???提前谢谢

最佳答案

你也可以这样做:

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

}
.... ....... ...... .....
}
}

关于java - 获取 docx4j 中内容控件的标题或别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31942114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com