################################################################################ # File : xmlpoke.py # Function : xml poke library/script ################################################################################ # Newest version can be obtained at http://www.freshlime.org # Send comments or questions to code at freshlime dot org # $Id: xmlpoke.py 99 2008-07-04 15:21:20Z jbester $ ################################################################################ # Copyright (c) 2008, J. Bester # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * The name of the authors names of its contributors may be used to # endorse or promote products derived from this software without # specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ################################################################################ """ XML Poke library/script. Set value based of xpath expression in an xml file """ import libxml2 import sys def xmlpoke( filename, xpath, value ): """ Xml poke. Similar to xmlpoke in nant. Sets value based on xpath expression. filename - xml file path xpath - xpath expression value - value to set content of xpath expression to be """ doc = libxml2.parseFile(filename) exps = doc.xpathEval( xpath ) if len( exps ) == 0: return False for url in exps: url.setContent( value ) doc.saveFile( filename ) return True def main(): """ Main function: Called if __main__ module """ prog = sys.argv[0] args = sys.argv[1:] if len( args ) != 3: print """\ %s [filename] [xpath] [value] Sets value in xml file based on xpath expression\ """ % ( prog ) return filename = args[ 0 ] xpath = args[ 1 ] value = args[ 2 ] result = xmlpoke( filename, xpath, value ) if not result: print 'Poke not successful' if __name__ == '__main__': main()