[Tutor] XML parsing

Stefan Behnel stefan_ml at behnel.de
Fri Mar 30 05:21:42 EDT 2018


Asif Iqbal schrieb am 30.03.2018 um 03:40:
> On Thu, Mar 29, 2018 at 3:41 PM, Peter Otten wrote:
>> Asif Iqbal wrote:
>>> On Thu, Mar 29, 2018 at 3:56 AM, Peter Otten wrote:
>>>> Asif Iqbal wrote:
>>>>> Here is a sample xml file
>>>>>
>>>>> <collection xmlns:y="http://tail-f.com/ns/rest">
>>>>>   <template-metadata xmlns="http://networks.com/nms">
>>>>>     <template-name>ALLFLEX-BLOOMINGTON</template-name>
>>>>>     <type>post-staging</type>
>>>>>     <device-type>full-mesh</device-type>
>>>>>     <provider-tenant>ALLFLEX</provider-tenant>
>>>>>     <subscription xmlns="http://networks.com/nms">
>>>>>       <solution-tier>advanced-plus</solution-tier>
>>>>>       <bandwidth>1000</bandwidth>
>>>>>       <is-analytics-enabled>true</is-analytics-enabled>
>>>>>       <is-primary>true</is-primary>
>>>>>     </subscription>
>>>>> ....
>>>>> </collection>
>>>>>
>>>>> with open('/tmp/template-metadata') as f:
>>>>>     import xml.etree.ElementTree as ET
>>>>>     root = ET.fromstring(f.read())

Don't use fromstring() here, use "parse(f).getroot()". The first loads the
whole file step by step into a string in memory, then parses it, and then
throws the string away. The second directly loads and parses the file step
by step.


> I also want to extract the namespace and I see this gets me the namespace
> 
>       str(root[0]).split('{')[1].split('}')[0]
> 
> Is there a better way to extract the name space?

Yes: don't. ;)

Normally, you write code *for* concrete XML namespaces, not code that
extracts arbitrary namespaces from XML. I'm saying that because people
misunderstand this up all the time, and assume that they need to extract
information from namespaces (or even from namespace prefixes!). But without
knowing the namespace upfront, the XML data is actually meaningless. And if
you know the namespace anyway, why would you need to extract it from the data?

I admit that I'm being a bit strict here, there are certainly cases where
parsing the namespace from a tag is a sensible thing to do. I'm really just
saying that most of the time, when you feel the need to do that, it's worth
reconsidering (or asking) if you are doing the right thing.

Stefan



More information about the Tutor mailing list