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 the XML document
2) The inherited code is somewhat 'brittle' in that some well-formed
XML documents are not correctly processed by the code; the brittleness
is caused by how the parser portion of the code handles whitespace.
3) I would like to change the code to make it less brittle. Whatever
changes I make must continue to produce the same data structure that is
currently being produced.
4) Rather than attempt to fix the parser portion of the code, I would
prefer to use ElementTree. ElementTree handles parsing XML documents
flawlessly, so the brittle portion of the code goes away. In addition,
the ElementTree model is very sweet to work with, so it is a relatively
easy task using the information in ElementTree to produce the same data
structure that is currently being produced.
5) The existing data structure--the structure that must be
maintained--that gets produced does NOT include any {xmlns=<whatever>}
information that may appear in the source XML document.
6) Based on a review of several posts in this group, I understand why
ElementTree hanldes xmlns=<whateverinformation the way it does. This
is an oversimplification, but one of the things it does is to
incorporate the {whatever} within the tag property of the element and
of any descendent elements.
7) One of the pieces of information in the data structure that gets
produced by this code is the tag...the tag in the data structure should
not have any xmlns=<whateverinformation.
So, given that the goal is to produce the same data structure and given
that I really want to use ElementTree, I need to find a way to remove
the xmlns=<whateverinformation. It seems like there are 2 general
methods for accomplishing this:
1) before feeding the string object to the ElementTree.XML() method,
remove the xmlns=<whateverinformation from the string.
2) keep the xmlns=<whateverinformation in the string that feeds
ElementTree.XML(), but when building the data structure, ensure that
the {whatever} information in the tag property of the element should
NOT be included in the data structure.
My requests for advice are:
a) What are the pros/cons of each of the 2 general methods described
above?
b) If I want to remove the xmlns information before feeding it to the
ElementTree.XML() method, and I don't want to be aware of what is to
the right of the equal sign, what is the best way to remove all the
substrings that are of the form xmlns=<whatever>? Would this require
learning the nuances of regular expressions?
c) If I want to leave the xmlns information in the string that gets fed
to ElementTree.XML, and I want to remove the {whatever} from the tag
before building the data structure, what is the best way to find
{whatever} from the tag property...is this another case where one
should be using regular expressions? 2 1944 mi************@yahoo.com wrote:
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 the XML document
2) The inherited code is somewhat 'brittle' in that some well-formed
XML documents are not correctly processed by the code; the brittleness
is caused by how the parser portion of the code handles whitespace.
3) I would like to change the code to make it less brittle. Whatever
changes I make must continue to produce the same data structure that is
currently being produced.
4) Rather than attempt to fix the parser portion of the code, I would
prefer to use ElementTree. ElementTree handles parsing XML documents
flawlessly, so the brittle portion of the code goes away. In addition,
the ElementTree model is very sweet to work with, so it is a relatively
easy task using the information in ElementTree to produce the same data
structure that is currently being produced.
5) The existing data structure--the structure that must be
maintained--that gets produced does NOT include any {xmlns=<whatever>}
information that may appear in the source XML document.
6) Based on a review of several posts in this group, I understand why
ElementTree hanldes xmlns=<whateverinformation the way it does. This
is an oversimplification, but one of the things it does is to
incorporate the {whatever} within the tag property of the element and
of any descendent elements.
7) One of the pieces of information in the data structure that gets
produced by this code is the tag...the tag in the data structure should
not have any xmlns=<whateverinformation.
So, given that the goal is to produce the same data structure and given
that I really want to use ElementTree, I need to find a way to remove
the xmlns=<whateverinformation. It seems like there are 2 general
methods for accomplishing this:
1) before feeding the string object to the ElementTree.XML() method,
remove the xmlns=<whateverinformation from the string.
2) keep the xmlns=<whateverinformation in the string that feeds
ElementTree.XML(), but when building the data structure, ensure that
the {whatever} information in the tag property of the element should
NOT be included in the data structure.
[snip]
maybe transform the document with XSLT before processing?
google: xslt remove namespaces
eg. http://www.tei-c.org/wiki/index.php/...Namespaces.xsl
eg. http://www.thescripts.com/forum/thread86057.html
hth
Gerard mi************@yahoo.com wrote:
c) If I want to leave the xmlns information in the string that gets fed
to ElementTree.XML, and I want to remove the {whatever} from the tag
before building the data structure, what is the best way to find
{whatever} from the tag property...is this another case where one
should be using regular expressions?
if the "whatever" in {whatever} is known in advance, you can use the
approach described here: http://effbot.org/zone/element-tidyl...-xhtml-to-html
if the "whatever" is not known, you can do e.g.
if elem.tag.startswith("{"):
elem.tag = elem.tag.split("}")[1]
</F> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: nmac |
last post by:
Hi all, hopefully someone can offer some sagely advice regarding
Production use of Jakarta's Tomcat.
First, some brief background.
My company have a servlet application that connects to a MySQL...
|
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"...
|
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:
...
|
by: mirandacascade |
last post by:
Verion of Python: 2.4
O/S: Windows XP
ElementTree resides in the c:\python24\lib\site-packages\elementtree\
folder
When a string that does not contain well-formed XML is passed as an
argument...
|
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...
|
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":...
|
by: Rick Muller |
last post by:
I'm a computational chemist who frequently dabbles in Python. A
collaborator sent me a huge XML file that at one point was evidently
modified by a now defunct java application. A sample of this...
|
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 =...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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,...
| |