sasdata.dataloader.readers.xml_reader module¶
Generic XML read and write utility
Usage: Either extend xml_reader or add as a class variable.
- class sasdata.dataloader.readers.xml_reader.XMLreader(xml=None, schema=None)¶
Bases:
FileReader
Generic XML read and write class. Mostly helper functions. Makes reading/writing XML a bit easier than calling lxml libraries directly.
- Dependencies:
This class requires lxml 2.3 or higher.
- append(element, tree)¶
Append an etree Element to an ElementTree.
- Parameters:
element – etree Element to append
tree – ElementTree object to append to
- break_processing_instructions(string, dic)¶
Method to break a processing instruction string apart and add to a dict
- Parameters:
string – A processing instruction as a string
dic – The dictionary to save the PIs to
- create_element(name, attrib=None, nsmap=None)¶
Create an XML element for writing to file
- Parameters:
name – The name of the element to be created
- create_element_from_string(xml_string)¶
Create an element from an XML string
- Parameters:
xml_string – A string of xml
- create_tree(root)¶
Create an element tree for processing from an etree element
- Parameters:
root – etree Element(s)
- ebuilder(parent, elementname, text=None, attrib=None)¶
Use lxml E builder class with arbitrary inputs.
- Parameters:
parnet – The parent element to append a child to
elementname – The name of the child in string form
text – The element text
attrib – A dictionary of attribute names to attribute values
- encoding = None¶
- find_invalid_xml()¶
Finds the first offending element that should not be present in XML file
- parse_schema_and_doc()¶
Creates a dictionary of the parsed schema and xml files.
- processing_instructions = None¶
- reader()¶
Read in an XML file into memory and return an lxml dictionary
- return_processing_instructions()¶
Get all processing instructions saved when loading the document
- Parameters:
tree – etree.ElementTree object to write PIs to
- schema = None¶
- schemadoc = None¶
- set_encoding(attr_str)¶
Find the encoding in the xml declaration and save it as a string
- Parameters:
attr_str – All attributes as a string e.g. “foo1=”bar1” foo2=”bar2” foo3=”bar3” … foo_n=”bar_n””
- set_processing_instructions()¶
Take out all processing instructions and create a dictionary from them If there is a default encoding, the value is also saved
- set_schema(schema)¶
Set the schema file and parse
- set_xml(xml: str) None ¶
Generic method that routes xml based on string contents :param xml: A string that can either be a filepath, URL, or XML string.
- to_string(elem, pretty_print=False, encoding=None)¶
Converts an etree element into a string
- validate_xml()¶
Checks to see if the XML file meets the schema
- write_attribute(elem, attr_name, attr_value)¶
Write attributes to an Element
- Parameters:
elem – etree.Element object
attr_name – attribute name to write
attr_value – attribute value to set
- write_text(elem, text)¶
Write text to an etree Element
- Parameters:
elem – etree.Element object
text – text to write to the element
- xml = None¶
- xmldoc = None¶
- xmlroot = None¶
- sasdata.dataloader.readers.xml_reader.uri_is_valid(x)¶
Check if a URI string is valid