Exemplo n.º 1
0
def getChildElementWithSpecificXpath(start, xpath):
    """
    This method takes a xml file or parent element as input and finds the first child
    containing specified xpath

    Returns the child element.

    Arguments:
    start = xml file or parent element
    xpath = a valid xml path value as supported by python, refer https://docs.python.org/2/library/xml.etree.elementtree.html
    """
    node = False
    if isinstance(start, (file, str)):
        # check if file exist here
        if file_Utils.fileExists(start):
            node = ElementTree.parse(start).getroot()
        else:
            print_warning('The file={0} is not found.'.format(start))
    elif isinstance(start, ElementTree.Element):
        node = start
    if node is not False or node is not None:
        element = node.find(xpath)
    else:
        element = False
    return element
Exemplo n.º 2
0
def get_tree_from_file(filepath):
    """ Get the tree from the xml file"""
    if file_Utils.fileExists(filepath):
            tree = ElementTree.parse(filepath)
    else:
        print_error("xml file does not exist in provided path {0}".format(filepath))
        tree = False
    return tree
Exemplo n.º 3
0
def getElementWithTagAttribValueMatch(start, tag, attrib, value):
    """
    When start is an xml datafile, it finds the root and first element with:
        tag, attrib, value.
    Or when it's an xml element, it finds the first child element with:
        tag, attrib, value.
    If there is not a match, it returns False.
    """
    node = False
    if isinstance(start, (file, str)):
        # check if file exist here
        if file_Utils.fileExists(start):
            node = ElementTree.parse(start).getroot()
        else:
            print_warning('The file={0} is not found.'.format(start))
    elif isinstance(start, ElementTree.Element):
        node = start
    if node is not False and node is not None:
        elementName = ".//%s[@%s='%s']" % (tag, attrib, value)
        element = node.find(elementName)
    else:
        element = node
    return element