graphit/graph_io/io_xml_format.py
def read_xml(xml_file, graph=None):
    """
    Parse hierarchical XML data structure to a graph
    
    Uses the Python build-in etree cElementTree parser to parse the XML
    document and convert the elements into nodes.
    The XML element tag becomes the node key, XML text becomes the node
    value and XML attributes are added to the node as additional attributes.
    
    :param xml_file:       XML data to parse
    :type xml_file:        File, string, stream or URL
    :param graph:          Graph object to import dictionary data in
    :type graph:           :graphit:Graph
    
    :return:               GraphAxis object
    :rtype:                :graphit:GraphAxis
    """
    
    # User defined or default GraphAxis object
    if graph is None:
        graph = GraphAxis()
    if not isinstance(graph, GraphAxis):
        raise TypeError('Unsupported graph type {0}'.format(type(graph)))
    
    # Try parsing the string using default Python cElementTree parser
    xml_file = open_anything(xml_file)
    try:
        tree = et.fromstring(xml_file.read())
    except et.ParseError as error:
        logging.error('Unable to parse XML file. cElementTree error: {0}'.format(error))
        return
    
    def walk_element_tree(element, parent=None):
        
        for child in element:
            child_data = child.attrib
            if child.text and len(child.text.strip()):
                child_data[graph.value_tag] = child.text.strip()
            
            nid = graph.add_node(child.tag, **child_data)
            graph.add_edge(parent, nid)
            
            walk_element_tree(child, parent=nid)
    
    is_empty = graph.empty()
    
    # Add root element
    element_data = tree.attrib
    if tree.text and len(tree.text.strip()):
        element_data[graph.value_tag] = tree.text.strip()
    rid = graph.add_node(tree.tag, **element_data)
    
    if is_empty:
        graph.root = rid
    
    # Recursive add XML elements as nodes
    walk_element_tree(tree, parent=graph.root)
    
    return graph

    Parse hierarchical XML data structure to a graph
    
    Uses the Python build-in etree cElementTree parser to parse the XML
    document and convert the elements into nodes.
    The XML element tag becomes the node key, XML text becomes the node
    value and XML attributes are added to the node as additional attributes.
    
    :param xml_file:       XML data to parse
    :type xml_file:        File, string, stream or URL
    :param graph:          Graph object to import dictionary data in
    :type graph:           :graphit:Graph
    
    :return:               GraphAxis object
    :rtype:                :graphit:GraphAxis
    
Parse hierarchical XML data structure to a graph
Uses the Python build-in etree cElementTree parser to parse the XML
document and convert the elements into nodes.
The XML element tag becomes the node key, XML text becomes the node
value and XML attributes are added to the node as additional attributes.
Parse hierarchical XML data structure to a graph Uses the Python build - in etree cElementTree parser to parse the XML document and convert the elements into nodes . The XML element tag becomes the node key XML text becomes the node value and XML attributes are added to the node as additional attributes .
def read_xml(xml_file, graph=None):
    if graph is None:
        graph = GraphAxis()
    if not isinstance(graph, GraphAxis):
        raise TypeError('Unsupported graph type {0}'.format(type(graph)))
    xml_file = open_anything(xml_file)
    try:
        tree = et.fromstring(xml_file.read())
    except et.ParseError as error:
        logging.error('Unable to parse XML file. cElementTree error: {0}'.format(error))
        return
    def walk_element_tree(element, parent=None):
        for child in element:
            child_data = child.attrib
            if child.text and len(child.text.strip()):
                child_data[graph.value_tag] = child.text.strip()
            nid = graph.add_node(child.tag, **child_data)
            graph.add_edge(parent, nid)
            walk_element_tree(child, parent=nid)
    is_empty = graph.empty()
    element_data = tree.attrib
    if tree.text and len(tree.text.strip()):
        element_data[graph.value_tag] = tree.text.strip()
    rid = graph.add_node(tree.tag, **element_data)
    if is_empty:
        graph.root = rid
    walk_element_tree(tree, parent=graph.root)
    return graph
def read_xml ( xml_file , graph = None ) : if graph is None : graph = GraphAxis ( ) if not isinstance ( graph , GraphAxis ) : raise TypeError ( 'Unsupported graph type {0}' . format ( type ( graph ) ) ) xml_file = open_anything ( xml_file ) try : tree = et . fromstring ( xml_file . read ( ) ) except et . ParseError as error : logging . error ( 'Unable to parse XML file. cElementTree error: {0}' . format ( error ) ) return def walk_element_tree ( element , parent = None ) : for child in element : child_data = child . attrib if child . text and len ( child . text . strip ( ) ) : child_data [ graph . value_tag ] = child . text . strip ( ) nid = graph . add_node ( child . tag , ** child_data ) graph . add_edge ( parent , nid ) walk_element_tree ( child , parent = nid ) is_empty = graph . empty ( ) element_data = tree . attrib if tree . text and len ( tree . text . strip ( ) ) : element_data [ graph . value_tag ] = tree . text . strip ( ) rid = graph . add_node ( tree . tag , ** element_data ) if is_empty : graph . root = rid walk_element_tree ( tree , parent = graph . root ) return graph
Parse hierarchical XML data structure to a graph
Uses the Python build-in etree cElementTree parser to parse the XML
document and convert the elements into nodes.
Parse hierarchical XML data structure to a graph Uses the Python build - in etree cElementTree parser to parse the XML document and convert the elements into nodes .
"""
    Parse hierarchical XML data structure to a graph
    
    Uses the Python build-in etree cElementTree parser to parse the XML
    document and convert the elements into nodes.
    The XML element tag becomes the node key, XML text becomes the node
    value and XML attributes are added to the node as additional attributes.
    
    :param xml_file:       XML data to parse
    :type xml_file:        File, string, stream or URL
    :param graph:          Graph object to import dictionary data in
    :type graph:           :graphit:Graph
    
    :return:               GraphAxis object
    :rtype:                :graphit:GraphAxis
    """ # User defined or default GraphAxis object # Try parsing the string using default Python cElementTree parser # Add root element # Recursive add XML elements as nodes
None
:graphit:GraphAxis
xml_file
XML data to parse
XML data to parse
graph
Graph object to import dictionary data in
Graph object to import dictionary data in