示例#1
0
def convert_dom_to_string(element):
    """
    Converts a dom element into a string
    """
    domstring = ""
    try:
        domstring = str(element.toxml())
    except Exception as exception:
        print_exception(exception)
    return domstring
示例#2
0
def check_and_create_dir(dirpath):
    """Checks if dir exists in provided path.
    If dir exists returns True
    Elseif dir does not exists, tries to create a directory
        - If dir created successfully, returns True.
        - If dir creation failed returns false
    """
    status = False
    if pathExists(dirpath):
        print_info("Directory exists in provided path '{0}': ".format(dirpath))
        status = True
    elif not pathExists(dirpath):
        try:
            os.makedirs(dirpath)
        except Exception as exception:
            print_warning("Creating directory at '{0}' failed.".format(dirpath))
            print_exception(exception)
            status = False
        else:
            status = True
    return status
示例#3
0
def compare_xml(xml1, xml2, output_file=False, sorted_json=True,
                remove_namespaces=False, tag_list=[], attrib_list=[]):
    """
    This will compare two xml files or strings by converting to json
    and then sorting and by default giving the sorted files
    and writing the difference to a diff file.

    Arguments:
        1. xml1 : The first xml among the two xml's which
            needs to be compared
        2. xml2 : The second xml among the two xml's
            which needs to be compared
        3. output_file : It contains the difference between
            the two sorted json objects
        4. sorted_json : By default we are returning the sorted json
            files and if the user selects sorted_json as False,
            not returning the fileS
        5. remove_namespaces: If the user specifies remove_namespaces
            as True will remove namespaces and then compare xml's
        6. tag_list: If user specifies tag names in tag_list,
            will remove those tags and then compare xml's
        7. attrib_list: If user specifies attribute names in the
        attrib_list, will remove those attributes and then compare xml's

    Returns:
        Returns a tuple that contains
            comparison status
            two sorted json files or two None depends on sorted_json value
            output file path or diff_output depends on output_file value
    """
    try:
        from Framework.ClassUtils.json_utils_class import JsonUtils
        if remove_namespaces:
            xml1 = removenms(xml1)
            xml2 = removenms(xml2)

        xml1 = del_tags_from_xml(xml1, tag_list)
        xml2 = del_tags_from_xml(xml2, tag_list)

        xml1 = del_attributes_from_xml(xml1, attrib_list)
        xml2 = del_attributes_from_xml(xml2, attrib_list)

        output1 = json.loads(json.dumps(
                    (xmltodict.parse(xml1, xml_attribs=True))))
        output2 = json.loads(json.dumps(
                    (xmltodict.parse(xml2, xml_attribs=True))))

        sorted_json1 = JsonUtils().sort_json_object(output1)
        sorted_json2 = JsonUtils().sort_json_object(output2)

        json_obj1 = json.dumps(
            sorted_json1, indent=4, separators=(',', ':'), encoding="utf-8")
        json_obj2 = json.dumps(
            sorted_json2, indent=4, separators=(',', ':'), encoding="utf-8")

        if sorted_json:
            sorted_file1 = "sorted_file1.json"
            sorted_file2 = "sorted_file2.json"
            sorted_file1 = file_Utils.addTimeDate(sorted_file1)
            sorted_file2 = file_Utils.addTimeDate(sorted_file2)

            f = open(sorted_file1, 'w')
            f1 = open(sorted_file2, 'w')
            f.write(json_obj1)
            f1.write(json_obj2)
            f.close()
            f1.close()
        else:
            sorted_file1 = None
            sorted_file2 = None

        if output1 == output2:
            return True, sorted_file1, sorted_file2, None

        diff = ("\n".join(
            difflib.ndiff(json_obj1.splitlines(), json_obj2.splitlines())))

        if output_file:
            output_file = file_Utils.addTimeDate(output_file)
            te = open(output_file, 'w')
            te.write(diff)
            te.close()
        else:
            output_file = diff

        return False, sorted_file1, sorted_file2, output_file

    except Exception as exception:
        print_exception(exception)
        return False, None, None, output_file
示例#4
0
import file_Utils
import os.path

from xml.dom import minidom
from xml.etree import ElementTree
from xml.etree.ElementTree import tostring
from Framework.OSS import xmltodict
from print_Utils import print_debug, print_info, print_error, print_warning, print_exception
from collections import OrderedDict

try:
    from lxml import etree, objectify
except ImportError as err:
    print_error("Module lxml is not installed, Refer to the exception trace below for more details")
    print_exception(err)

def create_subelement(parent, tag, attrib):
    """Creates a subelement with given tag
    and attributes under the parent element """
    subelement = ElementTree.SubElement(parent, tag, attrib)
    return subelement

def getValuebyTag (filename, tag):
    """Get the value from the tag name from the xml file"""
    doc = minidom.parse(filename)
    itemlist = doc.getElementsByTagName(tag)[0].toxml()
    itemvalue = itemlist.replace('<' + tag + '>', '').replace('</' + tag + '>', '')
    return itemvalue

def getValuebyAttribute (filename, attribute, tag):