Need help with basic DOM XML tree traversing
Barak, Ron
Ron.Barak at lsi.com
Sun Apr 25 11:06:01 EDT 2010
Hi,
This is my first try at XML with Python, and though I tried to read on the web, I'm unable to traverse a DOM tree, as my top element seems to be DOCUMENT_NODE and I cannot find a way to get to the nodes below it.
Below is a sample data, script and execution.
Could you point to where I'm doing wrong ?
Thanks,
Ron.
________________________________
$ python -u xml_parse.py
node: <xml.dom.minidom.DocumentType instance at 0x00DE25A8>
dom2.nodeType: 9
dom2.nodeName: #document
node is DOCUMENT_NODE: <xml.dom.minidom.DocumentType instance at 0x00DE25A8>
node: <DOM Element: database at 0xde2698>
dom2.nodeType: 9
dom2.nodeName: #document
node is DOCUMENT_NODE: <DOM Element: database at 0xde2698>
('dom2._get_childNodes():', [<xml.dom.minidom.DocumentType instance at 0x00DE25A8>, <DOM Element: database at 0xde2698>])
Traceback (most recent call last):
File "xml_parse.py", line 26, in <module>
print("child_nodes._get_childNodes():",child_nodes._get_childNodes())
AttributeError: 'NodeList' object has no attribute '_get_childNodes'
$ cat xml_parse.py
#!/usr/bin/env python
from xml.dom.minidom import parse, parseString
xml_file_path ='example.xml'
in_xml = open(xml_file_path)
tmp = in_xml.read()
doctype_loc = tmp.rfind("<!DOCTYPE")
open_squere_par_loc = tmp.find("[",doctype_loc)
first_part = tmp[:open_squere_par_loc + 1]
close_squere_par_loc = tmp.find("]>",doctype_loc)
last_part = tmp[close_squere_par_loc:]
xml_string = "%s%s" % (first_part, last_part)
dom2 = parseString(xml_string)
for node in dom2.childNodes:
print "node:",node
print "dom2.nodeType:",dom2.nodeType
print "dom2.nodeName:",dom2.nodeName
if dom2.nodeType == dom2.ELEMENT_NODE:
print "node is ELEMENT_NODE:",node
elif dom2.nodeType == dom2.DOCUMENT_NODE:
print "node is DOCUMENT_NODE:",node
child_nodes = dom2._get_childNodes()
print("dom2._get_childNodes():",dom2._get_childNodes())
print("child_nodes._get_childNodes():",child_nodes._get_childNodes())
dom2.unlink()
$ cat example.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE database [
<!ELEMENT database (DbPortCharacteristic*, DbDpmPersonality*, DbHostMode*, DbGlobalSettings*, DbSimulatedDisk*, DbUserPermission*, DbUser*, DbUserSession*, DbUserGroup*, DbLicenseKey*, DbObjectBase*, DbFolderQueryMetadata*, DbFolder*, DbVDevAccessPermissions*, DbApplicationType*, DbPReserve*, DbVDevAgentBroadcastMsg*, DbVDev*, DbConsistencyGroup*, DbVolume*, DbPit*, DbView*, DbStripeSegment*, DbPool*, DbStripe*, DbScsiAddress*, DbDisk*, DbHbaAccessPath*, DbHba*, DbSubnodeGroupInfo*, DbSubnodeGroup*, DbHbaGroup*, DbPrRequest*, DbDomain*, DbSvmInfo*, DbPermTemplate*, DbPersonality*, DbMessageGroup*, DbJobBase*, DbMmJob*, DbMmRJob*, DbMcJob*, DbMcRJob*, DbGroupBase*, DbMmGroup*, DbMmRGroup*, DbMcGroup*, DbMcRGroup*, DbLoadBalanceNode*, DbBrokenMmJobInfo*, DbSmGroup*, DbSmJob*, DbDestVolInfo*, DbJobDestPermission*, DbVSSSnapshotSet*, DbVSSSnapshot*, DbPoolAlertData*)>
<!ELEMENT DbPortCharacteristic (m_id, m_PortAddress, m_PortNumber, m_MtuActive, m_MtuSupported, m_SpeedCurrent, m_SpeedMax, m_ScsiRoleInfo, m_FCPortTypeInfo, m_InterfaceProtocolType, m_PortStatus, m_HbaGroup)>
<!ELEMENT m_id (#PCDATA)>
]>
<database>
<DbPortCharacteristic id="9429">
<m_id>2</m_id>
<m_PortAddress>"5001438102cfba20"</m_PortAddress>
<m_PortNumber>0</m_PortNumber>
<m_MtuActive>2048</m_MtuActive>
<m_MtuSupported>2048</m_MtuSupported>
<m_SpeedCurrent>0</m_SpeedCurrent>
<m_SpeedMax>4</m_SpeedMax>
<m_ScsiRoleInfo>1</m_ScsiRoleInfo>
<m_FCPortTypeInfo>1</m_FCPortTypeInfo>
<m_InterfaceProtocolType>3</m_InterfaceProtocolType>
<m_PortStatus>0</m_PortStatus>
<m_HbaGroup><ref id="8967"/></m_HbaGroup>
</DbPortCharacteristic>
</database>
$ python -V
Python 2.6.4
________________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100425/d332b778/attachment.html>
More information about the Python-list
mailing list