Example usage for javax.xml.bind Binder updateXML

List of usage examples for javax.xml.bind Binder updateXML

Introduction

In this page you can find the example usage for javax.xml.bind Binder updateXML.

Prototype

public abstract XmlNode updateXML(Object jaxbObject) throws JAXBException;

Source Link

Document

Takes an JAXB object and updates its associated XML node and its descendants.

Usage

From source file:org.docx4j.XmlUtils.java

/**
 * Fetch DOM node / JAXB object pairs matching an XPath (for example "//w:p").
 * /*from   ww  w.  j av  a2  s  .c o  m*/
 * 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.   
 * 
 * @param binder
 * @param jaxbElement
 * @param xpathExpr
 * @param refreshXmlFirst
 * @return
 * @throws JAXBException
 * @throws XPathBinderAssociationIsPartialException
 * @since 3.0.0
 */
public static List<JAXBAssociation> getJAXBAssociationsForXPath(Binder<Node> binder, Object jaxbElement,
        String xpathExpr, boolean refreshXmlFirst)
        throws JAXBException, XPathBinderAssociationIsPartialException {

    if (binder == null) {
        log.warn("null binder");
    }
    if (jaxbElement == null) {
        log.warn("null jaxbElement");
    }

    Node node;
    if (refreshXmlFirst) {
        node = binder.updateXML(jaxbElement);
    }
    node = binder.getXMLNode(jaxbElement);
    if (node == null) {
        throw new XPathBinderAssociationIsPartialException("binder.getXMLNode returned null");
    }

    //log.debug("XPath will execute against: " + XmlUtils.w3CDomNodeToString(node));

    List<JAXBAssociation> resultList = new ArrayList<JAXBAssociation>();

    for (Node n : xpath(node, xpathExpr)) {
        resultList.add(new JAXBAssociation(n, binder.getJAXBNode(n)));
    }
    return resultList;
}