Package org.docx4j

Class XmlUtils

java.lang.Object
org.docx4j.XmlUtils

public class XmlUtils
extends java.lang.Object
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static java.lang.String TRANSFORMER_FACTORY_PROCESSOR_XALAN  
  • Constructor Summary

    Constructors 
    Constructor Description
    XmlUtils()  
  • Method Summary

    Modifier and Type Method Description
    static void appendXmlFragment​(org.w3c.dom.Document document, org.w3c.dom.Node parent, java.lang.String fragment)  
    static <T> T deepCopy​(T value)
    Clone this JAXB object, using default JAXBContext.
    static <T> T deepCopy​(T value, javax.xml.bind.JAXBContext jc)
    Clone this JAXB object
    static javax.xml.parsers.DocumentBuilderFactory getDocumentBuilderFactory()
    Deprecated.
    static java.util.List<JAXBAssociation> getJAXBAssociationsForXPath​(javax.xml.bind.Binder<org.w3c.dom.Node> binder, java.lang.Object jaxbElement, java.lang.String xpathExpr, boolean refreshXmlFirst)
    Fetch DOM node / JAXB object pairs matching an XPath (for example "//w:p").
    static java.util.List<java.lang.Object> getJAXBNodesViaXPath​(javax.xml.bind.Binder<org.w3c.dom.Node> binder, java.lang.Object jaxbElement, java.lang.String xpathExpr, boolean refreshXmlFirst)
    Fetch JAXB Nodes matching an XPath (for example "//w:p").
    static javax.xml.bind.JAXBElement<?> getListItemByQName​(java.util.List<javax.xml.bind.JAXBElement<?>> list, javax.xml.namespace.QName name)  
    static javax.xml.parsers.DocumentBuilder getNewDocumentBuilder()
    Use the suitably configured DocumentBuilderFactory to provide a new instance of DocumentBuilder.
    static javax.xml.transform.TransformerFactory getTransformerFactory()  
    static javax.xml.transform.Templates getTransformerTemplate​(javax.xml.transform.Source xsltSource)  
    static java.lang.String JAXBElementDebug​(javax.xml.bind.JAXBElement o)  
    static java.io.InputStream marshaltoInputStream​(java.lang.Object o, boolean suppressDeclaration, javax.xml.bind.JAXBContext jc)  
    static java.lang.String marshaltoString​(java.lang.Object o)
    Marshal this object to a String, pretty printed, and without an XML declaration.
    static java.lang.String marshaltoString​(java.lang.Object o, boolean suppressDeclaration)
    Deprecated.
    static java.lang.String marshaltoString​(java.lang.Object o, boolean suppressDeclaration, boolean prettyprint)
    Marshal to a String
    static java.lang.String marshaltoString​(java.lang.Object o, boolean suppressDeclaration, boolean prettyprint, javax.xml.bind.JAXBContext jc)
    Marshal to a String
    static java.lang.String marshaltoString​(java.lang.Object o, boolean suppressDeclaration, boolean prettyprint, javax.xml.bind.JAXBContext jc, java.lang.String uri, java.lang.String local, java.lang.Class declaredType)
    Marshal to a String, for object missing an @XmlRootElement annotation.
    static java.lang.String marshaltoString​(java.lang.Object o, boolean suppressDeclaration, javax.xml.bind.JAXBContext jc)
    Deprecated.
    static java.lang.String marshaltoString​(java.lang.Object o, javax.xml.bind.JAXBContext jc)
    Use the specified JAXBContext to marshal this object to a String, pretty printed, and without an XML declaration.
    static org.w3c.dom.Document marshaltoW3CDomDocument​(java.lang.Object o)
    Marshal to a W3C document
    static org.w3c.dom.Document marshaltoW3CDomDocument​(java.lang.Object o, javax.xml.bind.JAXBContext jc)
    Marshal to a W3C document
    static org.w3c.dom.Document marshaltoW3CDomDocument​(java.lang.Object o, javax.xml.bind.JAXBContext jc, java.lang.String uri, java.lang.String local, java.lang.Class declaredType)
    Marshal to a W3C document, for object missing an @XmlRootElement annotation.
    static org.w3c.dom.Document neww3cDomDocument()
    Use DocumentBuilderFactory to create and return a new w3c dom Document.
    static javax.xml.bind.util.JAXBResult prepareJAXBResult​(javax.xml.bind.JAXBContext context)
    Prepare a JAXB transformation result for some given context.
    static void transform​(javax.xml.transform.Source source, javax.xml.transform.Templates template, java.util.Map<java.lang.String,​java.lang.Object> transformParameters, javax.xml.transform.Result result)
    Transform an input document using XSLT
    static void transform​(org.w3c.dom.Document doc, javax.xml.transform.Templates template, java.util.Map<java.lang.String,​java.lang.Object> transformParameters, javax.xml.transform.Result result)  
    static void treeCopy​(org.w3c.dom.NodeList sourceNodes, org.w3c.dom.Node destParent)  
    static void treeCopy​(org.w3c.dom.Node sourceNode, org.w3c.dom.Node destParent)
    Copy a node from one DOM document to another.
    static byte[] trimNamespaces​(org.w3c.dom.Document doc, java.lang.String ignorables)
    Removes superflouous namespaces.
    static java.lang.Object unmarshal​(java.io.InputStream is)
    Unmarshal an InputStream as an object in the package org.docx4j.jaxb.document.
    static java.lang.Object unmarshal​(java.io.InputStream is, javax.xml.bind.JAXBContext jc)  
    static java.lang.Object unmarshal​(org.w3c.dom.Node n)
    Unmarshal a node using Context.jc, WITHOUT fallback to pre-processing in case of failure.
    static java.lang.Object unmarshal​(org.w3c.dom.Node n, javax.xml.bind.JAXBContext jc, java.lang.Class declaredType)  
    static java.lang.Object unmarshallFromTemplate​(java.lang.String wmlTemplateString, java.util.Map<java.lang.String,​?> mappings)
    Give a string of wml containing ${key1}, ${key2}, return a suitable object.
    static java.lang.Object unmarshallFromTemplate​(java.lang.String wmlTemplateString, java.util.Map<java.lang.String,​?> mappings, javax.xml.bind.JAXBContext jc)  
    static java.lang.Object unmarshallFromTemplate​(java.lang.String wmlTemplateString, java.util.Map<java.lang.String,​?> mappings, javax.xml.bind.JAXBContext jc, java.lang.Class<?> declaredType)  
    static java.lang.Object unmarshalString​(java.lang.String str)
    Unmarshal a String as an object in the package org.docx4j.jaxb.document.
    static java.lang.Object unmarshalString​(java.lang.String str, javax.xml.bind.JAXBContext jc)  
    static java.lang.Object unmarshalString​(java.lang.String str, javax.xml.bind.JAXBContext jc, java.lang.Class declaredType)  
    static java.lang.Object unwrap​(java.lang.Object o)
    If an object is wrapped in a JAXBElement, return the object.
    static void w3CDomNodeToOutputStream​(org.w3c.dom.Node n, java.io.OutputStream os)  
    static java.lang.String w3CDomNodeToString​(org.w3c.dom.Node n)  
    static java.util.List<org.w3c.dom.Node> xpath​(org.w3c.dom.Node node, java.lang.String xpathExpression)  
    static java.util.List<org.w3c.dom.Node> xpath​(org.w3c.dom.Node node, java.lang.String xpathExpression, javax.xml.namespace.NamespaceContext nsContext)  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

  • Method Details

    • getTransformerFactory

      public static javax.xml.transform.TransformerFactory getTransformerFactory()
      Since:
      2.8.1
    • getDocumentBuilderFactory

      @Deprecated public static javax.xml.parsers.DocumentBuilderFactory getDocumentBuilderFactory()
      Deprecated.
      Since:
      2.8.1 TODO replace the various DocumentBuilderFactory.newInstance() throughout docx4j with a call to this.
    • getNewDocumentBuilder

      public static javax.xml.parsers.DocumentBuilder 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

      public static java.lang.Object unwrap​(java.lang.Object o)
      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

      public static java.lang.String JAXBElementDebug​(javax.xml.bind.JAXBElement o)
    • getListItemByQName

      public static javax.xml.bind.JAXBElement<?> getListItemByQName​(java.util.List<javax.xml.bind.JAXBElement<?>> list, javax.xml.namespace.QName name)
      Parameters:
      list -
      name -
      Returns:
      Since:
      3.2.0
    • unmarshal

      public static java.lang.Object unmarshal​(java.io.InputStream is) throws javax.xml.bind.JAXBException
      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:
      javax.xml.bind.JAXBException
    • unmarshal

      public static java.lang.Object unmarshal​(java.io.InputStream is, javax.xml.bind.JAXBContext jc) throws javax.xml.bind.JAXBException
      Throws:
      javax.xml.bind.JAXBException
    • unmarshalString

      public static java.lang.Object unmarshalString​(java.lang.String str) throws javax.xml.bind.JAXBException
      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:
      javax.xml.bind.JAXBException
    • unmarshalString

      public static java.lang.Object unmarshalString​(java.lang.String str, javax.xml.bind.JAXBContext jc, java.lang.Class declaredType) throws javax.xml.bind.JAXBException
      Throws:
      javax.xml.bind.JAXBException
    • unmarshalString

      public static java.lang.Object unmarshalString​(java.lang.String str, javax.xml.bind.JAXBContext jc) throws javax.xml.bind.JAXBException
      Throws:
      javax.xml.bind.JAXBException
    • unmarshal

      public static java.lang.Object unmarshal​(org.w3c.dom.Node n) throws javax.xml.bind.JAXBException
      Unmarshal a node using Context.jc, WITHOUT fallback to pre-processing in case of failure.
      Parameters:
      n -
      Returns:
      Throws:
      javax.xml.bind.JAXBException
    • unmarshal

      public static java.lang.Object unmarshal​(org.w3c.dom.Node n, javax.xml.bind.JAXBContext jc, java.lang.Class declaredType) throws javax.xml.bind.JAXBException
      Throws:
      javax.xml.bind.JAXBException
    • unmarshallFromTemplate

      public static java.lang.Object unmarshallFromTemplate​(java.lang.String wmlTemplateString, java.util.Map<java.lang.String,​?> mappings) throws javax.xml.bind.JAXBException
      Give a string of wml containing ${key1}, ${key2}, return a suitable object.
      Parameters:
      wmlTemplateString -
      mappings -
      Returns:
      Throws:
      javax.xml.bind.JAXBException
      See Also:
      which can invoke this more efficiently
    • unmarshallFromTemplate

      public static java.lang.Object unmarshallFromTemplate​(java.lang.String wmlTemplateString, java.util.Map<java.lang.String,​?> mappings, javax.xml.bind.JAXBContext jc) throws javax.xml.bind.JAXBException
      Throws:
      javax.xml.bind.JAXBException
    • unmarshallFromTemplate

      public static java.lang.Object unmarshallFromTemplate​(java.lang.String wmlTemplateString, java.util.Map<java.lang.String,​?> mappings, javax.xml.bind.JAXBContext jc, java.lang.Class<?> declaredType) throws javax.xml.bind.JAXBException
      Throws:
      javax.xml.bind.JAXBException
    • marshaltoString

      public static java.lang.String marshaltoString​(java.lang.Object o)
      Marshal this object to a String, pretty printed, and without an XML declaration. Useful for debugging.
      Parameters:
      o -
      Returns:
      Since:
      3.0.1
    • marshaltoString

      public static java.lang.String marshaltoString​(java.lang.Object o, javax.xml.bind.JAXBContext jc)
      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 public static java.lang.String marshaltoString​(java.lang.Object o, boolean suppressDeclaration)
      Deprecated.
      Marshal to a String
    • marshaltoString

      @Deprecated public static java.lang.String marshaltoString​(java.lang.Object o, boolean suppressDeclaration, javax.xml.bind.JAXBContext jc)
      Deprecated.
      Marshal to a String
    • marshaltoString

      public static java.lang.String marshaltoString​(java.lang.Object o, boolean suppressDeclaration, boolean prettyprint)
      Marshal to a String
    • trimNamespaces

      public static byte[] trimNamespaces​(org.w3c.dom.Document doc, java.lang.String ignorables) throws InvalidCanonicalizerException, CanonicalizationException
      Removes superflouous namespaces. It makes things neater, at the cost of some extra processing.
      Throws:
      InvalidCanonicalizerException
      CanonicalizationException
    • marshaltoString

      public static java.lang.String marshaltoString​(java.lang.Object o, boolean suppressDeclaration, boolean prettyprint, javax.xml.bind.JAXBContext jc)
      Marshal to a String
    • marshaltoString

      public static java.lang.String marshaltoString​(java.lang.Object o, boolean suppressDeclaration, boolean prettyprint, javax.xml.bind.JAXBContext jc, java.lang.String uri, java.lang.String local, java.lang.Class declaredType)
      Marshal to a String, for object missing an @XmlRootElement annotation.
    • marshaltoInputStream

      public static java.io.InputStream marshaltoInputStream​(java.lang.Object o, boolean suppressDeclaration, javax.xml.bind.JAXBContext jc)
    • marshaltoW3CDomDocument

      public static org.w3c.dom.Document marshaltoW3CDomDocument​(java.lang.Object o)
      Marshal to a W3C document
    • marshaltoW3CDomDocument

      public static org.w3c.dom.Document marshaltoW3CDomDocument​(java.lang.Object o, javax.xml.bind.JAXBContext jc)
      Marshal to a W3C document
    • marshaltoW3CDomDocument

      public static org.w3c.dom.Document marshaltoW3CDomDocument​(java.lang.Object o, javax.xml.bind.JAXBContext jc, java.lang.String uri, java.lang.String local, java.lang.Class declaredType)
      Marshal to a W3C document, for object missing an @XmlRootElement annotation.
    • deepCopy

      public static <T> T deepCopy​(T value)
      Clone this JAXB object, using default JAXBContext.
    • deepCopy

      public static <T> T deepCopy​(T value, javax.xml.bind.JAXBContext jc)
      Clone this JAXB object
      Parameters:
      value -
      jc -
      Returns:
    • w3CDomNodeToString

      public static java.lang.String w3CDomNodeToString​(org.w3c.dom.Node n)
    • w3CDomNodeToOutputStream

      public static void w3CDomNodeToOutputStream​(org.w3c.dom.Node n, java.io.OutputStream os) throws Docx4JException
      Parameters:
      n -
      os -
      Throws:
      Docx4JException
    • neww3cDomDocument

      public static org.w3c.dom.Document neww3cDomDocument()
      Use DocumentBuilderFactory to create and return a new w3c dom Document.
    • appendXmlFragment

      public static void appendXmlFragment​(org.w3c.dom.Document document, org.w3c.dom.Node parent, java.lang.String fragment) throws java.io.IOException, org.xml.sax.SAXException, javax.xml.parsers.ParserConfigurationException
      Parameters:
      docBuilder - the parser
      parent - node to add fragment to
      fragment - a well formed XML fragment
      Throws:
      javax.xml.parsers.ParserConfigurationException
      java.io.IOException
      org.xml.sax.SAXException
    • prepareJAXBResult

      public static javax.xml.bind.util.JAXBResult prepareJAXBResult​(javax.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​(org.w3c.dom.Document doc, javax.xml.transform.Templates template, java.util.Map<java.lang.String,​java.lang.Object> transformParameters, javax.xml.transform.Result result) throws Docx4JException
      Throws:
      Docx4JException
    • getTransformerTemplate

      public static javax.xml.transform.Templates getTransformerTemplate​(javax.xml.transform.Source xsltSource) throws javax.xml.transform.TransformerConfigurationException
      Throws:
      javax.xml.transform.TransformerConfigurationException
    • transform

      public static void transform​(javax.xml.transform.Source source, javax.xml.transform.Templates template, java.util.Map<java.lang.String,​java.lang.Object> transformParameters, javax.xml.transform.Result result) throws Docx4JException
      Transform an input document using XSLT
      Parameters:
      doc -
      xslt -
      transformParameters -
      result -
      Throws:
      Docx4JException - In case serious transformation errors occur
    • getJAXBNodesViaXPath

      public static java.util.List<java.lang.Object> getJAXBNodesViaXPath​(javax.xml.bind.Binder<org.w3c.dom.Node> binder, java.lang.Object jaxbElement, java.lang.String xpathExpr, boolean refreshXmlFirst) throws javax.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:
      javax.xml.bind.JAXBException
      XPathBinderAssociationIsPartialException
    • getJAXBAssociationsForXPath

      public static java.util.List<JAXBAssociation> getJAXBAssociationsForXPath​(javax.xml.bind.Binder<org.w3c.dom.Node> binder, java.lang.Object jaxbElement, java.lang.String xpathExpr, boolean refreshXmlFirst) throws javax.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:
      javax.xml.bind.JAXBException
      XPathBinderAssociationIsPartialException
      Since:
      3.0.0
    • xpath

      public static java.util.List<org.w3c.dom.Node> xpath​(org.w3c.dom.Node node, java.lang.String xpathExpression)
    • xpath

      public static java.util.List<org.w3c.dom.Node> xpath​(org.w3c.dom.Node node, java.lang.String xpathExpression, javax.xml.namespace.NamespaceContext nsContext)
    • treeCopy

      public static void treeCopy​(org.w3c.dom.NodeList sourceNodes, org.w3c.dom.Node destParent)
    • treeCopy

      public static void treeCopy​(org.w3c.dom.Node sourceNode, org.w3c.dom.Node destParent)
      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 -