9.10. Implementing XMLGrepTree

We now have all the bits and pieces we need to add a tree-driven XML processing mode to xGrep. The bulk of the code changes are in the new function XMLGrepTree.

 CD-ROM reference=9033.txt # XML-aware grepping using event source--either xmln or xmlv # depending on VALIDATE option. def XMLGrepTree (filename,pattern,options,out,context=""): if options & VALIDATE: # Use xmlv as a PYX event source. f = os.popen ("xmlv %s" % filename) else: # Use xmln as a PYX event source. f = os.popen ("xmln %s" % filename) # Create an empty tree. tree = xTree() tree.PYX2xTree(f) f.close() Pattern = re.compile (pattern) if options & MARK_HITS: StartOfHit = "[" EndOfHit = "]" else: StartOfHit = "" EndOfHit = "" NodeList = pyx.Descendants(tree.Root()) ...

Get XML Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.