本文整理汇总了Java中org.docx4j.XmlUtils类的典型用法代码示例。如果您正苦于以下问题:Java XmlUtils类的具体用法?Java XmlUtils怎么用?Java XmlUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
XmlUtils类属于org.docx4j包,在下文中一共展示了XmlUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testTblStyle_AllSilent
import org.docx4j.XmlUtils; //导入依赖的package包/类
@Test
public void testTblStyle_AllSilent() throws Exception {
// Result is to use the implicit 10pt
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
wordMLPackage.getMainDocumentPart().setContents(
(Document)XmlUtils.unmarshalString(mdpXml_tblStyle) );
wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().setContents(
(Styles)XmlUtils.unmarshalString(styles_no_font_sz) );
setSetting(wordMLPackage, OVERRIDE); // table style should get overridden
wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
// NB PropertyResolver puts 10pt in DocDefaults, if nothing is specified!
ParagraphStylesInTableFix.process(wordMLPackage);
Style s = getStyle(wordMLPackage, STYLE_NAME);
this.assertSz(s, 20);
// Assert.assertTrue(s.getRPr().getSz().getVal().intValue()==20);
}
示例2: getChildrenElements
import org.docx4j.XmlUtils; //导入依赖的package包/类
public static <T> List<T> getChildrenElements(Object source, Class<T> targetClass) {
List<T> result = new ArrayList<T>();
//获取真实的对象
Object target = XmlUtils.unwrap(source);
//if (target.getClass().equals(targetClass)) {
if (targetClass.isAssignableFrom(target.getClass())) {
result.add((T)target);
} else if (target instanceof ContentAccessor) {
List<?> children = ((ContentAccessor) target).getContent();
//if (children.getClass().equals(targetClass)) {
if (targetClass.isAssignableFrom(children.getClass())) {
result.add((T)children);
}
}
return result;
}
示例3: getTargetElements
import org.docx4j.XmlUtils; //导入依赖的package包/类
/**
* 这样会返回一个表示完整的空白(在此时)文档Java对象。现在我们可以使用Docx4J API添加、删除以及更新这个word文档的内容,Docx4J有一些你可以用于遍历该文档的工具类。
* 我自己写了几个助手方法使查找指定占位符并用真实内容进行替换的操作变地很简单。让我们来看一下其中的一个,这个计算是几个JAXB计算的包装器,
* 允许你针对一个特定的类来搜索指定元素以及它所有的孩子,例如,你可以用它获取文档中所有的表格、表格中所有的行以及其它类似的操作。
*/
public static <T> List<T> getTargetElements(Object source, Class<T> targetClass) {
List<T> result = new ArrayList<T>();
//获取真实的对象
Object target = XmlUtils.unwrap(source);
//if (target.getClass().equals(targetClass)) {
if (targetClass.isAssignableFrom(target.getClass())) {
result.add((T) target);
} else if (target instanceof ContentAccessor) {
List<?> children = ((ContentAccessor) target).getContent();
for (Object child : children) {
result.addAll(getTargetElements(child, targetClass));
}
}
return result;
}
示例4: writeToStream
import org.docx4j.XmlUtils; //导入依赖的package包/类
public static void writeToStream(WordprocessingMLPackage wmlPackage,OutputStream output) throws IOException, Docx4JException {
Assert.notNull(wmlPackage, " wmlPackage is not specified!");
Assert.notNull(output, " output is not specified!");
InputStream input = null;
try {
//Document对象
MainDocumentPart document = wmlPackage.getMainDocumentPart();
//Document XML
String documentXML = XmlUtils.marshaltoString(wmlPackage);
//转成字节输入流
input = IOUtils.toBufferedInputStream(new ByteArrayInputStream(documentXML.getBytes()));
//输出模板
IOUtils.copy(input, output);
} finally {
IOUtils.closeQuietly(input);
}
}
示例5: writeToWriter
import org.docx4j.XmlUtils; //导入依赖的package包/类
public void writeToWriter(WordprocessingMLPackage wmlPackage,Writer output) throws IOException, Docx4JException {
Assert.notNull(wmlPackage, " wmlPackage is not specified!");
Assert.notNull(output, " output is not specified!");
InputStream input = null;
try {
//Document对象
MainDocumentPart document = wmlPackage.getMainDocumentPart();
//Document XML
String documentXML = XmlUtils.marshaltoString(wmlPackage.getPackage());
//转成字节输入流
input = IOUtils.toBufferedInputStream(new ByteArrayInputStream(documentXML.getBytes()));
//获取模板输出编码格式
String charsetName = Docx4jProperties.getProperty(Docx4jConstants.DOCX4J_CONVERT_OUT_WMLTEMPLATE_CHARSETNAME, Docx4jConstants.DEFAULT_CHARSETNAME );
//输出模板
IOUtils.copy(input, output, Charset.forName(charsetName));
} finally {
IOUtils.closeQuietly(input);
}
}
示例6: removeTableByIndex
import org.docx4j.XmlUtils; //导入依赖的package包/类
/**
* @Description:删除指定位置的表格,删除后表格数量减一
*/
public boolean removeTableByIndex(WordprocessingMLPackage wordMLPackage,
int index) throws Exception {
boolean flag = false;
if (index < 0) {
return flag;
}
List<Object> objList = wordMLPackage.getMainDocumentPart().getContent();
if (objList == null) {
return flag;
}
int k = -1;
for (int i = 0, len = objList.size(); i < len; i++) {
Object obj = XmlUtils.unwrap(objList.get(i));
if (obj instanceof Tbl) {
k++;
if (k == index) {
wordMLPackage.getMainDocumentPart().getContent().remove(i);
flag = true;
break;
}
}
}
return flag;
}
示例7: removeTrByIndex
import org.docx4j.XmlUtils; //导入依赖的package包/类
/**
* @Description: 删除指定行 删除后行数减一
*/
public boolean removeTrByIndex(Tbl tbl, int index) {
boolean flag = false;
if (index < 0) {
return flag;
}
List<Object> objList = tbl.getContent();
if (objList == null) {
return flag;
}
int k = -1;
for (int i = 0, len = objList.size(); i < len; i++) {
Object obj = XmlUtils.unwrap(objList.get(i));
if (obj instanceof Tr) {
k++;
if (k == index) {
tbl.getContent().remove(i);
flag = true;
break;
}
}
}
return flag;
}
示例8: walkJAXBElements
import org.docx4j.XmlUtils; //导入依赖的package包/类
public void walkJAXBElements(Object parent) {
List children = getChildren(parent);
if (children != null) {
for (Object o : children) {
if (o instanceof javax.xml.bind.JAXBElement
&& (((JAXBElement) o).getName().getLocalPart()
.equals("sdt"))) {
((Child) ((JAXBElement) o).getValue()).setParent(XmlUtils
.unwrap(parent));
} else {
o = XmlUtils.unwrap(o);
if (o instanceof Child) {
((Child) o).setParent(XmlUtils.unwrap(parent));
}
}
this.apply(o);
if (this.shouldTraverse(o)) {
walkJAXBElements(o);
}
}
}
}
示例9: apply
import org.docx4j.XmlUtils; //导入依赖的package包/类
public List<Object> apply(Object o) {
if (o instanceof javax.xml.bind.JAXBElement
&& (((JAXBElement) o).getName().getLocalPart()
.equals("commentReference")
|| ((JAXBElement) o).getName().getLocalPart()
.equals("commentRangeStart") || ((JAXBElement) o)
.getName().getLocalPart().equals("commentRangeEnd"))) {
System.out.println(((JAXBElement) o).getName().getLocalPart());
commentElements.add((Child) XmlUtils.unwrap(o));
} else if (o instanceof CommentReference
|| o instanceof CommentRangeStart
|| o instanceof CommentRangeEnd) {
System.out.println(o.getClass().getName());
commentElements.add((Child) o);
}
return null;
}
示例10: setFont
import org.docx4j.XmlUtils; //导入依赖的package包/类
/**
* Use RunFontSelector to determine the correct font for the list item label.
*
* @param context
* @param foListItemLabelBody
* @param pPr
* @param rPr
* @param text
*/
protected static void setFont(FOConversionContext context, Element foListItemLabelBody, PPr pPr, RPr rPr, String text) {
DocumentFragment result = (DocumentFragment)context.getRunFontSelector().fontSelector(pPr, rPr, text);
log.debug(XmlUtils.w3CDomNodeToString(result));
// eg <fo:inline xmlns:fo="http://www.w3.org/1999/XSL/Format" font-family="Times New Roman">1)</fo:inline>
// Now get the attribute value
if (result!=null && result.getFirstChild()!=null) {
Attr attr = ((Element)result.getFirstChild()).getAttributeNode("font-family");
if (attr!=null) {
foListItemLabelBody.setAttribute("font-family", attr.getValue());
}
}
}
示例11: getLayoutMasterSetFragment
import org.docx4j.XmlUtils; //导入依赖的package包/类
public static DocumentFragment getLayoutMasterSetFragment(AbstractWmlConversionContext context) {
LayoutMasterSet lms = getFoLayoutMasterSet(context);
// Set suitable extents, for which we need area tree
FOSettings foSettings = (FOSettings)context.getConversionSettings();
if ( !foSettings.lsLayoutMasterSetCalculationInProgress()) // Avoid infinite loop
// Can't just do it where foSettings.getApacheFopMime() is not MimeConstants.MIME_FOP_AREA_TREE,
// since TOC functionality uses that.
{
fixExtents( lms, context, true);
}
org.w3c.dom.Document document = XmlUtils.marshaltoW3CDomDocument(lms, Context.getXslFoContext() );
DocumentFragment docfrag = document.createDocumentFragment();
docfrag.appendChild(document.getDocumentElement());
return docfrag;
}
示例12: appendLayoutMasterSetFragment
import org.docx4j.XmlUtils; //导入依赖的package包/类
/**
* For XSLFOExporterNonXSLT
* @since 3.0
*
*/
public static void appendLayoutMasterSetFragment(AbstractWmlConversionContext context, Node foRoot) {
LayoutMasterSet lms = getFoLayoutMasterSet(context);
// Set suitable extents, for which we need area tree
FOSettings foSettings = (FOSettings)context.getConversionSettings();
if ( !foSettings.lsLayoutMasterSetCalculationInProgress()) // Avoid infinite loop
// Can't just do it where foSettings.getApacheFopMime() is not MimeConstants.MIME_FOP_AREA_TREE,
// since TOC functionality uses that.
{
fixExtents( lms, context, false);
}
org.w3c.dom.Document document = XmlUtils.marshaltoW3CDomDocument(lms, Context.getXslFoContext() );
XmlUtils.treeCopy(document.getDocumentElement(), foRoot);
}
示例13: testTblStyle_AllSilent
import org.docx4j.XmlUtils; //导入依赖的package包/类
/**
* table says 40, p says nothing
* should override, but nothing explicit, so we don't
*
* @throws Exception
*/
@Test
public void testTblStyle_AllSilent() throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
wordMLPackage.getMainDocumentPart().setContents(
(Document)XmlUtils.unmarshalString(mdpXml_tblStyle) );
wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().setContents(
(Styles)XmlUtils.unmarshalString(styles_no_font_sz) );
// // NB createVirtualStylesForDocDefaults() puts 10pt there, if nothing is specified!
// // So we need to delete that!
// wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().createVirtualStylesForDocDefaults();
// Style dd = wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().getStyleById("DocDefaults");
// dd.getRPr().setSz(null);
// dd.getRPr().setSzCs(null);
setSetting(wordMLPackage, OVERRIDE); // table style should get overridden
wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
ParagraphStylesInTableFix.process(wordMLPackage);
Style s = getStyle(wordMLPackage, STYLE_NAME);
this.assertSz(s, 40); // nothing explicit to override with, so use table val
}
示例14: testTblStyle_BasedOnNormal
import org.docx4j.XmlUtils; //导入依赖的package包/类
@Test
public void testTblStyle_BasedOnNormal() throws Exception {
// Compat setting says Paragraph style overrides table style
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
wordMLPackage.getMainDocumentPart().setContents(
(Document)XmlUtils.unmarshalString(mdpXml_tblStyle) );
wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().setContents(
(Styles)XmlUtils.unmarshalString(styles_in_basedOn_Normal) );
// Use our style!
List<Object> xpathResults = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath("//w:p", true);
PPr ppr = Context.getWmlObjectFactory().createPPr();
((P)xpathResults.get(0)).setPPr(ppr);
PStyle ps = Context.getWmlObjectFactory().createPPrBasePStyle();
ps.setVal("testStyle");
ppr.setPStyle(ps);
setSetting(wordMLPackage, OVERRIDE); // table style should get overridden
wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
ParagraphStylesInTableFix.process(wordMLPackage);
// // Revert style and save:
// ppr.setPStyle(ps); // doesn't work - wrong ref!
// wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
Style ours = null;
for (Style s : wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().getContents().getStyle()) {
if ("testStyle-TableGrid-BR".equals(s.getStyleId())) {
ours = s;
break;
}
}
// Style s = getStyle(wordMLPackage, STYLE_NAME);
Assert.assertTrue(ours.getRPr().getSz().getVal().intValue()==24);
}
示例15: testTblStyle_BasedOnNormal
import org.docx4j.XmlUtils; //导入依赖的package包/类
@Test
public void testTblStyle_BasedOnNormal() throws Exception {
// A style basedOn Normal is honoured, provided it (not Normal) contributes the font size
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
wordMLPackage.getMainDocumentPart().setContents(
(Document)XmlUtils.unmarshalString(mdpXml_tblStyle) );
wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().setContents(
(Styles)XmlUtils.unmarshalString(styles_in_basedOn_Normal) );
// Use our style!
List<Object> xpathResults = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath("//w:p", true);
PPr ppr = Context.getWmlObjectFactory().createPPr();
((P)xpathResults.get(0)).setPPr(ppr);
PStyle ps = Context.getWmlObjectFactory().createPPrBasePStyle();
ps.setVal("testStyle");
ppr.setPStyle(ps);
setSetting(wordMLPackage, OVERRIDE);
wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
ParagraphStylesInTableFix.process(wordMLPackage);
// // Revert style and save:
// ppr.setPStyle(ps); // doesn't work - wrong ref!
// wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
Style ours = null;
for (Style s : wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().getContents().getStyle()) {
if ("testStyle-TableNormal-BR".equals(s.getStyleId())) {
ours = s;
break;
}
}
// Style s = getStyle(wordMLPackage, STYLE_NAME);
Assert.assertTrue(ours.getRPr().getSz().getVal().intValue()==EXPECTED_RESULT);
}