By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

ElementTree XML Namspace

P: n/a
We are reviewing a vendor who will output some data in an XML format.
I will then use python to convert the data to another format for
upload to another vendor. I'm having trouble with very basic steps
with the sample file they sent me.

My very simple test script is:

from xml.etree.ElementTree import parse
tree = parse("sample.xml")
print tree.findtext("invoice_batch/batch_id/")

When I apply this to a very concatenated sample.xml with namespace (as
they send it to me)
<invoice_batch_generic xmlns="http://tempuri.org/
invoice_batch_generic.xsd">
<invoice_batch>
<batch_id>1</batch_id>
</invoice_batch>
</invoice_batch_generic>
The result I get is "None".

When I apply this to the same file with the namespace removed
<invoice_batch_generic>
<invoice_batch>
<batch_id>1</batch_id>
</invoice_batch>
</invoice_batch_generic>
The result is "1" which is what I would expect.

This is obviously just the very first step of what will be a much
larger process. The actual XML file is several pages long. I'm using
Python 2.5.2 (activestate) on Windows Vista.

I'm new to Python and newer to XML. So what am I missing? What am I
doing wrong?

Thanks --Joshua
Nov 14 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Hunter" <hu******@gmail.comwrote in message
news:80**********************************@k36g2000 pri.googlegroups.com...
We are reviewing a vendor who will output some data in an XML format.
I will then use python to convert the data to another format for
upload to another vendor. I'm having trouble with very basic steps
with the sample file they sent me.

My very simple test script is:

from xml.etree.ElementTree import parse
tree = parse("sample.xml")
print tree.findtext("invoice_batch/batch_id/")

When I apply this to a very concatenated sample.xml with namespace (as
they send it to me)
<invoice_batch_generic xmlns="http://tempuri.org/
invoice_batch_generic.xsd">
<invoice_batch>
<batch_id>1</batch_id>
</invoice_batch>
</invoice_batch_generic>
The result I get is "None".

When I apply this to the same file with the namespace removed
<invoice_batch_generic>
<invoice_batch>
<batch_id>1</batch_id>
</invoice_batch>
</invoice_batch_generic>
The result is "1" which is what I would expect.

This is obviously just the very first step of what will be a much
larger process. The actual XML file is several pages long. I'm using
Python 2.5.2 (activestate) on Windows Vista.

I'm new to Python and newer to XML. So what am I missing? What am I
doing wrong?

Thanks --Joshua
You're missing the namespace[1]. Try:

print
tree.findtext("{http://tempuri.org/invoice_batch_generic.xsd}invoice_batch/{http://tempuri.org/invoice_batch_generic.xsd}batch_id/")

-Mark

[1]http://effbot.org/zone/element.htm#xml-namespaces

Nov 15 '08 #2

P: n/a
Hunter wrote:
We are reviewing a vendor who will output some data in an XML format.
I will then use python to convert the data to another format for
upload to another vendor.
Take a look at lxml.objectify, it has a nicer API, especially if you are new
to XML. It also handles loads of namespace issues under the hood, so that you
don't get bothered with them.

http://codespeak.net/lxml/objectify....ect-attributes
http://codespeak.net/lxml/objectify....space-handling

You said "reviewing". Does that indicate that you need validation?

http://codespeak.net/lxml/validation.html
http://codespeak.net/lxml/objectify....rting-a-schema

Stefan
Nov 15 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.