Package org.docx4j
Class XmlUtils
java.lang.Object
org.docx4j.XmlUtils
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidappendXmlFragment(Document document, Node parent, String fragment) static <T> TdeepCopy(T value) Clone this JAXB object, using default JAXBContext.static <T> TdeepCopy(T value, jakarta.xml.bind.JAXBContext jc) Clone this JAXB objectstatic DocumentBuilderFactoryDeprecated.static List<JAXBAssociation>getJAXBAssociationsForXPath(jakarta.xml.bind.Binder<Node> binder, Object jaxbElement, String xpathExpr, boolean refreshXmlFirst) Fetch DOM node / JAXB object pairs matching an XPath (for example "//w:p").getJAXBNodesViaXPath(jakarta.xml.bind.Binder<Node> binder, Object jaxbElement, String xpathExpr, boolean refreshXmlFirst) Fetch JAXB Nodes matching an XPath (for example "//w:p").static jakarta.xml.bind.JAXBElement<?>getListItemByQName(List<jakarta.xml.bind.JAXBElement<?>> list, QName name) static DocumentBuilderUse the suitably configured DocumentBuilderFactory to provide a new instance of DocumentBuilder.static TransformerFactorystatic TemplatesgetTransformerTemplate(Source xsltSource) static ObjecthandleUnmarshalException(jakarta.xml.bind.JAXBContext jc, JaxbValidationEventHandler eventHandler, Document document, Exception ue) static StringJAXBElementDebug(jakarta.xml.bind.JAXBElement o) static InputStreammarshaltoInputStream(Object o, boolean suppressDeclaration, jakarta.xml.bind.JAXBContext jc) marshaltoInputStream, declaring mcChoiceNamespacestatic InputStreammarshaltoInputStream(Object o, boolean suppressDeclaration, jakarta.xml.bind.JAXBContext jc, String mcChoiceNamespace) marshaltoInputStream, declaring mcChoiceNamespacestatic StringMarshal this object to a String, pretty printed, and without an XML declaration.static StringmarshaltoString(Object o, boolean suppressDeclaration) Deprecated.static StringmarshaltoString(Object o, boolean suppressDeclaration, boolean prettyprint) Marshal to a Stringstatic StringmarshaltoString(Object o, boolean suppressDeclaration, boolean prettyprint, jakarta.xml.bind.JAXBContext jc) Marshal to a Stringstatic StringmarshaltoString(Object o, boolean suppressDeclaration, boolean prettyprint, jakarta.xml.bind.JAXBContext jc, String mcChoiceNamespace) Marshal to a String, declaring mcChoiceNamespacestatic StringmarshaltoString(Object o, boolean suppressDeclaration, boolean prettyprint, jakarta.xml.bind.JAXBContext jc, String uri, String local, Class declaredType) Marshal to a String, for object missing an @XmlRootElement annotationstatic StringmarshaltoString(Object o, boolean suppressDeclaration, boolean prettyprint, jakarta.xml.bind.JAXBContext jc, String uri, String local, Class declaredType, String mcChoiceNamespace) Marshal to a String, for object missing an @XmlRootElement annotation, declaring mcChoiceNamespacestatic StringmarshaltoString(Object o, boolean suppressDeclaration, jakarta.xml.bind.JAXBContext jc) Deprecated.static StringmarshaltoString(Object o, jakarta.xml.bind.JAXBContext jc) Use the specified JAXBContext to marshal this object to a String, pretty printed, and without an XML declaration.static DocumentMarshal to a W3C documentstatic DocumentmarshaltoW3CDomDocument(Object o, jakarta.xml.bind.JAXBContext jc) Marshal to a W3C document, declaring mcChoiceNamespacestatic DocumentmarshaltoW3CDomDocument(Object o, jakarta.xml.bind.JAXBContext jc, String mcChoiceNamespace) Marshal to a W3C document, declaring mcChoiceNamespacestatic DocumentmarshaltoW3CDomDocument(Object o, jakarta.xml.bind.JAXBContext jc, String uri, String local, Class declaredType) Marshal to a W3C document, for object missing an @XmlRootElement annotation.static DocumentmarshaltoW3CDomDocument(Object o, jakarta.xml.bind.JAXBContext jc, String uri, String local, Class declaredType, String mcChoiceNamespace) Marshal to a W3C document, for object missing an @XmlRootElement annotation, declaring mcChoiceNamespacestatic DocumentUse DocumentBuilderFactory to create and return a new w3c dom Document.static jakarta.xml.bind.util.JAXBResultprepareJAXBResult(jakarta.xml.bind.JAXBContext context) Prepare a JAXB transformation result for some given context.static voidTransform an input document using XSLTstatic voidstatic voidstatic voidCopy a node from one DOM document to another.static byte[]trimNamespaces(Document doc, String ignorables) Removes superflouous namespaces.static Objectunmarshal(InputStream is) Unmarshal an InputStream as an object in the package org.docx4j.jaxb.document.static Objectunmarshal(InputStream is, jakarta.xml.bind.JAXBContext jc) static ObjectUnmarshal a node using Context.jc, WITHOUT fallback to pre-processing in case of failure.static Objectstatic ObjectunmarshallFromTemplate(String wmlTemplateString, Map<String, ?> mappings) Give a string of wml containing ${key1}, ${key2}, return a suitable object.static ObjectunmarshallFromTemplate(String wmlTemplateString, Map<String, ?> mappings, jakarta.xml.bind.JAXBContext jc) static ObjectunmarshallFromTemplate(String wmlTemplateString, Map<String, ?> mappings, jakarta.xml.bind.JAXBContext jc, Class<?> declaredType) static ObjectunmarshalString(String str) Unmarshal a String as an object in the package org.docx4j.jaxb.document.static ObjectunmarshalString(String str, jakarta.xml.bind.JAXBContext jc) static ObjectunmarshalString(String str, jakarta.xml.bind.JAXBContext jc, Class declaredType) static ObjectIf an object is wrapped in a JAXBElement, return the object.static voidstatic Stringxpath(Node node, String xpathExpression, NamespaceContext nsContext)
-
Field Details
-
TRANSFORMER_FACTORY_PROCESSOR_XALAN
-
-
Constructor Details
-
XmlUtils
public XmlUtils()
-
-
Method Details
-
getTransformerFactory
- Since:
- 2.8.1
-
getDocumentBuilderFactory
Deprecated.- Since:
- 2.8.1 TODO replace the various DocumentBuilderFactory.newInstance() throughout docx4j with a call to this.
-
getNewDocumentBuilder
Use the suitably configured DocumentBuilderFactory to provide a new instance of DocumentBuilder. Remember that DocumentBuilder is not thread-safe!- Returns:
- Since:
- 3.2.0
-
unwrap
If an object is wrapped in a JAXBElement, return the object. Warning: be careful with this. If you are copying objects into your document (rather than just reading them), you'll probably want the object to remain wrapped (JAXB usually wraps them for a reason; without the wrapper, you'll (probably?) need an- Parameters:
o-- Returns:
-
JAXBElementDebug
-
getListItemByQName
public static jakarta.xml.bind.JAXBElement<?> getListItemByQName(List<jakarta.xml.bind.JAXBElement<?>> list, QName name) - Parameters:
list-name-- Returns:
- Since:
- 3.2.0
-
unmarshal
Unmarshal an InputStream as an object in the package org.docx4j.jaxb.document. Note: you should ensure you include a namespace declaration for w: and any other namespace in the xml string. Also, the object you are attempting to unmarshall to might need to have an @XmlRootElement annotation for things to work.- Throws:
jakarta.xml.bind.JAXBException
-
unmarshal
public static Object unmarshal(InputStream is, jakarta.xml.bind.JAXBContext jc) throws jakarta.xml.bind.JAXBException - Throws:
jakarta.xml.bind.JAXBException
-
unmarshalString
Unmarshal a String as an object in the package org.docx4j.jaxb.document. Note: you should ensure you include a namespace declaration for w: and any other namespace in the xml string. Also, the object you are attempting to unmarshall to might need to have an @XmlRootElement annotation for things to work.- Throws:
jakarta.xml.bind.JAXBException
-
unmarshalString
public static Object unmarshalString(String str, jakarta.xml.bind.JAXBContext jc, Class declaredType) throws jakarta.xml.bind.JAXBException - Throws:
jakarta.xml.bind.JAXBException
-
unmarshalString
public static Object unmarshalString(String str, jakarta.xml.bind.JAXBContext jc) throws jakarta.xml.bind.JAXBException - Throws:
jakarta.xml.bind.JAXBException
-
handleUnmarshalException
public static Object handleUnmarshalException(jakarta.xml.bind.JAXBContext jc, JaxbValidationEventHandler eventHandler, Document document, Exception ue) throws jakarta.xml.bind.UnmarshalException, jakarta.xml.bind.JAXBException - Throws:
jakarta.xml.bind.UnmarshalExceptionjakarta.xml.bind.JAXBException
-
unmarshal
Unmarshal a node using Context.jc, WITHOUT fallback to pre-processing in case of failure.- Parameters:
n-- Returns:
- Throws:
jakarta.xml.bind.JAXBException
-
unmarshal
public static Object unmarshal(Node n, jakarta.xml.bind.JAXBContext jc, Class declaredType) throws jakarta.xml.bind.JAXBException - Throws:
jakarta.xml.bind.JAXBException
-
unmarshallFromTemplate
public static Object unmarshallFromTemplate(String wmlTemplateString, Map<String, ?> mappings) throws jakarta.xml.bind.JAXBExceptionGive a string of wml containing ${key1}, ${key2}, return a suitable object.- Parameters:
wmlTemplateString-mappings-- Returns:
- Throws:
jakarta.xml.bind.JAXBException- See Also:
-
which can invoke this more efficiently
-
unmarshallFromTemplate
public static Object unmarshallFromTemplate(String wmlTemplateString, Map<String, ?> mappings, jakarta.xml.bind.JAXBContext jc) throws jakarta.xml.bind.JAXBException- Throws:
jakarta.xml.bind.JAXBException
-
unmarshallFromTemplate
public static Object unmarshallFromTemplate(String wmlTemplateString, Map<String, ?> mappings, jakarta.xml.bind.JAXBContext jc, Class<?> declaredType) throws jakarta.xml.bind.JAXBException- Throws:
jakarta.xml.bind.JAXBException
-
marshaltoString
Marshal this object to a String, pretty printed, and without an XML declaration. Useful for debugging.- Parameters:
o-- Returns:
- Since:
- 3.0.1
-
marshaltoString
Use the specified JAXBContext to marshal this object to a String, pretty printed, and without an XML declaration. Useful for debugging.- Parameters:
o-- Returns:
- Since:
- 3.0.1
-
marshaltoString
Deprecated.Marshal to a String -
marshaltoString
@Deprecated public static String marshaltoString(Object o, boolean suppressDeclaration, jakarta.xml.bind.JAXBContext jc) Deprecated.Marshal to a String -
marshaltoString
Marshal to a String -
trimNamespaces
public static byte[] trimNamespaces(Document doc, String ignorables) throws InvalidCanonicalizerException, CanonicalizationException Removes superflouous namespaces. It makes things neater, at the cost of some extra processing. -
marshaltoString
public static String marshaltoString(Object o, boolean suppressDeclaration, boolean prettyprint, jakarta.xml.bind.JAXBContext jc) Marshal to a String -
marshaltoString
public static String marshaltoString(Object o, boolean suppressDeclaration, boolean prettyprint, jakarta.xml.bind.JAXBContext jc, String mcChoiceNamespace) Marshal to a String, declaring mcChoiceNamespace- Since:
- 8.3.2
-
marshaltoString
public static String marshaltoString(Object o, boolean suppressDeclaration, boolean prettyprint, jakarta.xml.bind.JAXBContext jc, String uri, String local, Class declaredType) Marshal to a String, for object missing an @XmlRootElement annotation -
marshaltoString
public static String marshaltoString(Object o, boolean suppressDeclaration, boolean prettyprint, jakarta.xml.bind.JAXBContext jc, String uri, String local, Class declaredType, String mcChoiceNamespace) Marshal to a String, for object missing an @XmlRootElement annotation, declaring mcChoiceNamespace- Since:
- 8.3.2
-
marshaltoInputStream
public static InputStream marshaltoInputStream(Object o, boolean suppressDeclaration, jakarta.xml.bind.JAXBContext jc) marshaltoInputStream, declaring mcChoiceNamespace -
marshaltoInputStream
public static InputStream marshaltoInputStream(Object o, boolean suppressDeclaration, jakarta.xml.bind.JAXBContext jc, String mcChoiceNamespace) marshaltoInputStream, declaring mcChoiceNamespace- Since:
- 8.3.2
-
marshaltoW3CDomDocument
Marshal to a W3C document -
marshaltoW3CDomDocument
Marshal to a W3C document, declaring mcChoiceNamespace -
marshaltoW3CDomDocument
public static Document marshaltoW3CDomDocument(Object o, jakarta.xml.bind.JAXBContext jc, String mcChoiceNamespace) Marshal to a W3C document, declaring mcChoiceNamespace- Since:
- 8.3.2
-
marshaltoW3CDomDocument
public static Document marshaltoW3CDomDocument(Object o, jakarta.xml.bind.JAXBContext jc, String uri, String local, Class declaredType) Marshal to a W3C document, for object missing an @XmlRootElement annotation. -
marshaltoW3CDomDocument
public static Document marshaltoW3CDomDocument(Object o, jakarta.xml.bind.JAXBContext jc, String uri, String local, Class declaredType, String mcChoiceNamespace) Marshal to a W3C document, for object missing an @XmlRootElement annotation, declaring mcChoiceNamespace- Since:
- 8.3.2
-
deepCopy
public static <T> T deepCopy(T value) Clone this JAXB object, using default JAXBContext. -
deepCopy
public static <T> T deepCopy(T value, jakarta.xml.bind.JAXBContext jc) Clone this JAXB object- Parameters:
value-jc-- Returns:
-
w3CDomNodeToString
-
w3CDomNodeToOutputStream
- Parameters:
n-os-- Throws:
Docx4JException
-
neww3cDomDocument
Use DocumentBuilderFactory to create and return a new w3c dom Document. -
appendXmlFragment
public static void appendXmlFragment(Document document, Node parent, String fragment) throws IOException, SAXException, ParserConfigurationException - Parameters:
docBuilder- the parserparent- node to add fragment tofragment- a well formed XML fragment- Throws:
ParserConfigurationExceptionIOExceptionSAXException
-
prepareJAXBResult
public static jakarta.xml.bind.util.JAXBResult prepareJAXBResult(jakarta.xml.bind.JAXBContext context) throws Docx4JException Prepare a JAXB transformation result for some given context.- Parameters:
context- The JAXB context.- Returns:
- The result data structure created.
- Throws:
Docx4JException- In case of configuration errors.
-
transform
public static void transform(Document doc, Templates template, Map<String, Object> transformParameters, Result result) throws Docx4JException- Throws:
Docx4JException
-
getTransformerTemplate
public static Templates getTransformerTemplate(Source xsltSource) throws TransformerConfigurationException -
transform
public static void transform(Source source, Templates template, Map<String, Object> transformParameters, Result result) throws Docx4JExceptionTransform an input document using XSLT- Parameters:
doc-xslt-transformParameters-result-- Throws:
Docx4JException- In case serious transformation errors occur
-
getJAXBNodesViaXPath
public static List<Object> getJAXBNodesViaXPath(jakarta.xml.bind.Binder<Node> binder, Object jaxbElement, String xpathExpr, boolean refreshXmlFirst) throws jakarta.xml.bind.JAXBException, XPathBinderAssociationIsPartialException Fetch JAXB Nodes matching an XPath (for example "//w:p"). In JAXB, this association is partial; not all XML elements have associated JAXB objects, and not all JAXB objects have associated XML elements. If the XPath returns an element which isn't associated with a JAXB object, since 3.0, this method will throw XPathBinderAssociationIsPartialException, to distinguish from no matching elements. If you have modified your JAXB objects (eg added or changed a w:p paragraph), you need to update the association. The problem is that this can only be done ONCE, owing to a bug in JAXB: see https://jaxb.dev.java.net/issues/show_bug.cgi?id=459 So this is left for you to choose to do via the refreshXmlFirst parameter.- Parameters:
binder-jaxbElement-refreshXmlFirst-xpathExpr-- Returns:
- Throws:
jakarta.xml.bind.JAXBExceptionXPathBinderAssociationIsPartialException
-
getJAXBAssociationsForXPath
public static List<JAXBAssociation> getJAXBAssociationsForXPath(jakarta.xml.bind.Binder<Node> binder, Object jaxbElement, String xpathExpr, boolean refreshXmlFirst) throws jakarta.xml.bind.JAXBException, XPathBinderAssociationIsPartialException Fetch DOM node / JAXB object pairs matching an XPath (for example "//w:p"). In JAXB, this association is partial; not all XML elements have associated JAXB objects, and not all JAXB objects have associated XML elements. If the XPath returns an element which isn't associated with a JAXB object, the element's pair will be null. If you have modified your JAXB objects (eg added or changed a w:p paragraph), you need to update the association. The problem is that this can only be done ONCE, owing to a bug in JAXB: see https://jaxb.dev.java.net/issues/show_bug.cgi?id=459 So this is left for you to choose to do via the refreshXmlFirst parameter.- Parameters:
binder-jaxbElement-xpathExpr-refreshXmlFirst-- Returns:
- Throws:
jakarta.xml.bind.JAXBExceptionXPathBinderAssociationIsPartialException- Since:
- 3.0.0
-
xpath
-
xpath
-
treeCopy
-
treeCopy
Copy a node from one DOM document to another. Used to avoid relying on an underlying implementation which might not support importNode (eg Xalan's org.apache.xml.dtm.ref.DTMNodeProxy). WARNING: doesn't fully support namespaces!- Parameters:
sourceNode-destParent-
-