Package org.docx4j.openpackaging.parts
Class CustomXmlDataStoragePart
java.lang.Object
org.docx4j.openpackaging.Base
org.docx4j.openpackaging.parts.Part
org.docx4j.openpackaging.parts.CustomXmlDataStoragePart
- All Implemented Interfaces:
CustomXmlPart
CustomXmlDataStoragePart contains user custom xml,
to which content control bindings can point.
This is the modern best practice approach for
injecting text/data into a docx.
See the Getting Started guide for a general overview,
and additional references, or use the docx4j
website to search for "custom xml binding".
Once you have included this part, and bound content
controls to it, nothing further needs to be done for
Word to display your data (unless you are using
conditional|repeat - see below).
However, if you want your data to show up in
docx4j PDF|HTML output, you need to run the
BindingHandler.applyBindings method first. (TODO: do this
automatically)
The actual XML data is stored in a CustomXmlDataStorage
object, for which accessor is get|setData. (Ths is
useful if you want to generate n docx documents, each
with different XML.)
If this contains XML which is bound in an sdt
via w:sdtPr/w:dataBinding, then its rels
will point to CustomXmlDataStoragePropertiesPart
which gives its datastore itemID.
(The datastore itemID is used in the w:dataBinding)
The Package contains a hashmap<String, CustomXmlDataStoragePart>
to make it easy to get the part to which we apply the
XPath. The part is registered as the document is loaded.
This class supports content control extensions
(ie od:condition, od:repeat). Use the
OpenDoPEHandler class to process these.
- Author:
- jharrop
-
Field Summary
Fields inherited from class org.docx4j.openpackaging.Base
contentType, partName, relationships -
Constructor Summary
ConstructorsConstructorDescriptionCustomXmlDataStoragePart(PartName partName) CustomXmlDataStoragePart(Parts parts) Deprecated. -
Method Summary
Modifier and TypeMethodDescriptioncachedXPathGetString(String xpath, String prefixMappings) Use org.apache.xpath.CachedXPathAPI, since this is much quicker than default javax.xml.xpath.XPath implementations (Apache's anyway) for large XML files.voidUse this to null out the org.apache.xpath.CachedXPathAPI object, which you should do if you've changed your source document, or want to reclaim memory.getData()getXML()Get the XML as a String.voidinit()booleanisContentEqual(Part other) voidremove()Remove this part from the pkg.voidsetData(CustomXmlDataStorage data) booleansetNodeValueAtXPath(String xpath, String value, String prefixMappings) voidSet the XMLxpathGetNodes(String xpathString, String prefixMappings) xpathGetString(String xpath, String prefixMappings) Methods inherited from class org.docx4j.openpackaging.parts.Part
getContentLengthAsLoaded, getOwningRelationshipPart, getPackage, getRelationshipType, getSourceRelationship, getSourceRelationships, getVersion, rename, setContentLengthAsLoaded, setOwningRelationshipPart, setPackage, setPartShortcut, setRelationshipType, setSourceRelationship, setVersionMethods inherited from class org.docx4j.openpackaging.Base
addTargetPart, addTargetPart, addTargetPart, addTargetPart, getContentType, getPartName, getRelationshipsPart, getRelationshipsPart, getUserData, reset, setContentType, setPartName, setRelationships, setUserData
-
Constructor Details
-
CustomXmlDataStoragePart
- Throws:
InvalidFormatException
-
CustomXmlDataStoragePart
- Throws:
InvalidFormatException
-
CustomXmlDataStoragePart
Deprecated.- Parameters:
parts- The parts present in the package to which this will be added. If for example /customXml/item1.xml already exists, this allows the name /customXml/item2.xml to be generated.- Throws:
InvalidFormatException
-
-
Method Details
-
init
public void init() -
getData
- Returns:
- the data
-
setData
- Parameters:
data- the data to set
-
isContentEqual
- Specified by:
isContentEqualin classPart- Throws:
Docx4JException
-
xpathGetString
- Specified by:
xpathGetStringin interfaceCustomXmlPart- Throws:
Docx4JException
-
cachedXPathGetString
Description copied from interface:CustomXmlPartUse org.apache.xpath.CachedXPathAPI, since this is much quicker than default javax.xml.xpath.XPath implementations (Apache's anyway) for large XML files. This is because Apache's old XPathAPI class instantiated a new XPathContext (and thus building a new DTMManager, and new DTMs) each time it was called. XPathAPIObject instead retains its context as long as the object persists, reusing the DTMs. There was the question of whether to declare it here or just implement it in CustomXmlDataStoragePart (since this is mostly of value in binding the user's XML data file). It is here since otherwise BindingHandler would fail on DocPropsCoverPagePart and StandardisedAnswersPart (both of which extend JaxbCustomXmlDataStoragePart).- Specified by:
cachedXPathGetStringin interfaceCustomXmlPart- Returns:
- Throws:
Docx4JException- Since:
- 3.3.1
- See Also:
-
discardCacheXPathObject
public void discardCacheXPathObject()Description copied from interface:CustomXmlPartUse this to null out the org.apache.xpath.CachedXPathAPI object, which you should do if you've changed your source document, or want to reclaim memory.- Specified by:
discardCacheXPathObjectin interfaceCustomXmlPart
-
xpathGetNodes
- Specified by:
xpathGetNodesin interfaceCustomXmlPart- Throws:
Docx4JException
-
setNodeValueAtXPath
public boolean setNodeValueAtXPath(String xpath, String value, String prefixMappings) throws Docx4JException - Specified by:
setNodeValueAtXPathin interfaceCustomXmlPart- Throws:
Docx4JException
-
getXML
Get the XML as a String.- Specified by:
getXMLin interfaceCustomXmlPart- Throws:
Docx4JException- Since:
- 3.0.1
-
setXML
Set the XML- Specified by:
setXMLin interfaceCustomXmlPart- Throws:
Docx4JException- Since:
- 6.0.0
-
getItemId
- Specified by:
getItemIdin interfaceCustomXmlPart- Since:
- 3.0.2
-
remove
public void remove()Remove this part from the pkg. Beware: it is up to you to make sure your content doesn't rely on this part being present! A symptom of that would be that Office now reports your file to be corrupt or in need of repair.
-