Python Minidom XML parsing dotted quad/nested children

krooz

New Member
I've got a gigantic list of varying objects I need to parse, and have multiple questions:[*]The string values within XML I'm able to parse quite easily (hostname, color,class_name etc), however anything numerical in nature (ip address/subnet mask etc) I'm not doing correctly. How do I get it to display the correct dotted quad?[*]What is the correct method (using minidom) to pull information out of deeper children? (see Group object - need 'name' under reference)[*]How can I sanitize (remove) the erroneous [] when a field does not contain a value (netmask for instance).XML looks like one of the two outputs(sanitized):a) Host object:\[code\]<network_object><Name>DB1</Name><Class_Name>host_plain</Class_Name><color><![CDATA[black]]></color><ipaddr><![CDATA[192.168.100.100]]></ipaddr>\[/code\]b) Group object (contains multiple members):\[code\] <network_object><Name>DB_Servers</Name><Class_Name>network_object_group</Class_Name><members> <reference> <Name>DB1</Name> <Table>network_objects</Table> </reference> <reference> <Name>DB2</Name> <Table>network_objects</Table> </reference></members><color><![CDATA[black]]></color>\[/code\]Current output of my code looks like this for a host object:\[code\]DB1 host_plain black [<DOM Element: ipaddr at 0x2d05a50>] []\[/code\]For a network object:\[code\]Net_192.168.100.0 network black [<DOM Element: ipaddr at 0x399add0>] [<DOM Element: netmask at 0x399af10>]\[/code\]For a group object:\[code\]DB_Servers network_object_group black [] []\[/code\]My code:\[code\]from xml.dom import minidomnet_xml = minidom.parse("network_objects.xml")NetworkObjectsTag = net_xml.getElementsByTagName("network_objects")[0]# Pull individual network objectsNetworkObjectTag = NetworkObjectsTag.getElementsByTagName("network_object")for network_object in NetworkObjectTag: name = network_object.getElementsByTagName("Name")[0].firstChild.data class_name = network_object.getElementsByTagName("Class_Name")[0].firstChild.data color = network_object.getElementsByTagName("color")[0].firstChild.data ipaddr = network_object.getElementsByTagName("ipaddr") netmask = network_object.getElementsByTagName("netmask") print(name,class_name,color,ipaddr,netmask)\[/code\]
 
Back
Top