1

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>
1
  • Assuming you are trying to parse a response. Can you paste the complete xml ? I tried the following sample response and it worked fine pastebin.com/peJVU6Zj Commented Aug 5, 2016 at 2:49

2 Answers 2

1

Just found what the issue is.

I had to define xmlns:junos in XML.

Don't know why but somehow i had omitted this line in my XML. I think it happened while i was copy pasting the XML.

Thanks for reply.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah please go ahead.
0

Adding the definition for junos apparently resolves the parsing issue.

<rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.1X47/junos">

I have used the following working sample for reference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.