Example usage for javax.xml.bind Binder getXMLNode

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

Introduction

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

Prototype

public abstract XmlNode getXMLNode(Object jaxbObject);

Source Link

Document

Gets the XML element associated with the given JAXB object.

Usage

From source file:org.docx4j.XmlUtils.java

/**
 * Fetch DOM node / JAXB object pairs matching an XPath (for example "//w:p").
 * //from  w w w .ja  v  a  2s . c om
 * 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;
}