473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

ElementTree XML Namspace

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
2 2340

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Stewart Midwinter | last post by:
I want to parse a file with ElementTree. My file has the following format: <!-- file population.xml --> <?xml version='1.0' encoding='utf-8'?> <population> <person><name="joe" sex="male"...
1
by: Greg Wilson | last post by:
I'm trying to convert from minidom to ElementTree for handling XML, and am having trouble with entities in DTDs. My Python script looks like this: ...
1
by: mirandacascade | last post by:
O/S: Windows 2K Vsn of Python: 2.4 Currently: 1) Folder structure: \workarea\ <- ElementTree files reside here \xml\ \dom\
15
by: Steven Bethard | last post by:
I'm having trouble using elementtree with an XML file that has some gbk-encoded text. (I can't read Chinese, so I'm taking their word for it that it's gbk-encoded.) I always have trouble with...
0
by: Greg Aumann | last post by:
I am trying to write some python code for a library that reads an XML-like language from a file into elementtree data structures. Then I want to be able to read and/or modify the structure and then...
2
by: mirandacascade | last post by:
Situation is this: 1) I have inherited some python code that accepts a string object, the contents of which is an XML document, and produces a data structure that represents some of the content of...
5
by: saif.shakeel | last post by:
#!/usr/bin/env python from elementtree import ElementTree as Element tree = et.parse("testxml.xml") for t in tree.getiterator("SERVICEPARAMETER"): if t.get("Semantics") == "localId":...
1
by: Mike Slinn | last post by:
The following short Python program parses a KML file and displays the names of all Marks and Routes: from elementtree.ElementTree import ElementTree tree = ElementTree(file='test.kml') kml =...
3
by: gray.bowman | last post by:
I'm messing around with trying to write an xml file using xml.etree.ElementTree. All the examples on the internet show the use of ElementTree.write(), although when I try to use it it's not...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.