def validate_xml(data, dtd_directory=None): from subprocess import Popen, PIPE # Run xmllint to validate file dtd_file = DTD_FILE if dtd_directory is not None: dtd_file = '%s/%s' % (dtd_directory, DTD_FILE) p = Popen('xmllint --dtdvalid %s --noout -' % dtd_file, stdout=PIPE, stdin=PIPE, stderr=PIPE, shell=True) data = _ensure_bytes(data) p.stdin.write(data) p.stdin.close() for x in p.stdout.readlines(): sys.stdout.write(x) status = p.wait() if status != 0: return False return True
def validate_xml(data, dtd_directory=None): """Run xmllint to validate files in dtd_directory""" from subprocess import Popen, PIPE # Run xmllint to validate file dtd_file = DTD_FILE if dtd_directory is not None: dtd_file = '%s/%s' % (dtd_directory, DTD_FILE) p = Popen('xmllint --dtdvalid %s --noout -' % dtd_file, stdout=PIPE, stdin=PIPE, stderr=PIPE, shell=True) data = _ensure_bytes(data) p.stdin.write(data) p.stdin.close() for x in p.stdout.readlines(): sys.stdout.write(x) status = p.wait() if status != 0: return False return True
def validate_xml(data, dtd_directory=None, root_elem=None): """ Validate the provided XML instance data against a CIM-XML DTD, optionally requiring a particular XML root element. Arguments: * `data`: XML instance data to be validated. * `dtd_directory`: Directory with the DTD file (see `DTD_FILE` for name). * `root_elem`: Name of XML element that is expected as the root element in the XML instance data to be validated. None means no checking for a particular root element is performed. """ dtd_file = DTD_FILE if dtd_directory is not None: dtd_file = os.path.join(dtd_directory, DTD_FILE).replace('\\', '/') # Make sure the XML data requires the specified root element, if any if root_elem is not None: data = '<!DOCTYPE %s SYSTEM "%s">\n' % (root_elem, dtd_file) + data xmllint_cmd = 'xmllint --valid --noout -' else: xmllint_cmd = 'xmllint --dtdvalid %s --noout -' % dtd_file p = Popen(xmllint_cmd, stdout=PIPE, stderr=STDOUT, stdin=PIPE, shell=True) data = _ensure_bytes(data) p.stdin.write(data) p.stdin.close() first_time = True for x in p.stdout.readlines(): if first_time: first_time = False print("\nOutput from xmllint:") sys.stdout.write(x) status = p.wait() p.stdout.close() if status != 0: return False return True
def validate_xml(data, dtd_directory=None, root_elem=None): """ Validate the provided XML instance data against a CIM-XML DTD, optionally requiring a particular XML root element. Arguments: * `data`: XML instance data to be validated. * `dtd_directory`: Directory with the DTD file (see `DTD_FILE` for name). * `root_elem`: Name of XML element that is expected as the root element in the XML instance data to be validated. None means no checking for a particular root element is performed. """ dtd_file = DTD_FILE if dtd_directory is not None: dtd_file = os.path.join(dtd_directory, DTD_FILE).replace('\\', '/') # Make sure the XML data requires the specified root element, if any if root_elem is not None: data = '<!DOCTYPE %s SYSTEM "%s">\n' % (root_elem, dtd_file) + data xmllint_cmd = 'xmllint --valid --noout -' else: xmllint_cmd = 'xmllint --dtdvalid %s --noout -' % dtd_file p = Popen(xmllint_cmd, stdout=PIPE, stderr=STDOUT, stdin=PIPE, shell=True) data = _ensure_bytes(data) p.stdin.write(data) p.stdin.close() first_time = True for x in p.stdout.readlines(): if first_time: first_time = False print("\nOutput from xmllint:") sys.stdout.write(x) status = p.wait() if status != 0: return False return True