public Document patch( Document mainDoc, Document appendDoc ) { Document resultDoc = mainDoc.clone(); Element resultRoot = resultDoc.getRootElement(); Element appendRoot = appendDoc.getRootElement(); ElementFilter modFilter = new ElementFilter( modNS ); for ( Content content : appendRoot.getContent() ) { if ( modFilter.matches( content ) ) { Element node = (Element)content; boolean handled = false; List<Element> matchedNodes = handleModFind( resultRoot, node ); if ( matchedNodes != null ) { handled = true; for ( Element matchedNode : matchedNodes ) { handleModCommands( matchedNode, node ); } } if ( !handled ) { throw new IllegalArgumentException( String.format( "Unrecognized mod tag <%s> (%s).", node.getName(), getPathToRoot(node) ) ); } } else { resultRoot.addContent( content.clone() ); } } return resultDoc; }