I have following simple code that is parsing a XML file. The issue is if in XML file the name space contains ":" i get an error. Not having it, no issue. It happens when i have ":" between "Junos and Style" when i remove the ":" from XML it works perfectly. Please advise.
Fail with this:
<interface-information xmlns="http://xml.juniper.net/junos/12.1X47/junos-interface" **junos:style**="brief">
Works with This:
<interface-information xmlns="http://xml.juniper.net/junos/12.1X47/junos-interface" **junosstyle**="brief">
Python Script:
from xml.dom.minidom import parse
import xml.dom.minidom
DOMTree = xml.dom.minidom.parse("test.xml")
collection = DOMTree.documentElement
if collection.hasAttribute("xmlns"):
print "Root element : %s" % collection.getAttribute("xmlns")
Interfaces = collection.getElementsByTagName("logical-interface")
for rname in Interfaces:
print "*****Interface*****"
rtype = rname.getElementsByTagName('name')[0]
print "Type: %s" % rtype.childNodes[0].data
Here is the error:
Traceback (most recent call last):
File "test.py", line 48, in <module>
DOMTree = xml.dom.minidom.parse("test.xml")
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/minidom.py", line 1921, in parse
return expatbuilder.parse(file)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/expatbuilder.py", line 924, in parse
result = builder.parseFile(fp)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/expatbuilder.py", line 207, in parseFile
parser.Parse(buffer, 0)
xml.parsers.expat.ExpatError: unbound prefix: line 2, column 0
It works not having ":" in XML between "Junos and Style"
Here is complete XML:
<?xml version="1.0"?>
<interface-information xmlns="http://xml.juniper.net/junos/12.1X47/junos-interface" junos:style="brief">
<logical-interface>
<name>reth4.10</name>
<description>
Test description
</description>
<if-config-flags>
<iff-snmp-traps/>
<internal-flags>
0x0
</internal-flags>
</if-config-flags>
<encapsulation>
ENET2
</encapsulation>
<filter-information>
</filter-information>
<logical-interface-zone-name>
Test2
</logical-interface-zone-name>
<allowed-host-inbound-traffic>
<inbound-ping/>
</allowed-host-inbound-traffic>
<address-family>
<address-family-name>
inet
</address-family-name>
<interface-address>
</interface-address>
</address-family>
<address-family>
<address-family-name>
multiservice
</address-family-name>
</address-family>
</logical-interface>
</interface-information>