473,394 Members | 1,781 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,394 software developers and data experts.

How to parse xml with ISO-8859-1 encoding using ElementTree andSimpleXMLTreeBuilder?

Zvi
Hi All,

I got trouble parsing xml returned by web service. XML data contains
characters above 128, so ET.fromstring throws an error. Error is
thrown from python's xmllib.py file, where it detects char above 128.
I am replacing utf-8 encoding string in returned xml with
'ISO-8859-1', and then I call .encode with ISO-8859-1 param. Still I
get the parsing error, illegal character.
What's interesting is that if I define a string const and assign it
the value returned from the service request it gets parsed. I.e. the
following gets parsed ok.
TEST_EVNVELOPE2 = """<?xml version="1.0" encoding="ISO-8859-1"?>
<soap:Envelope xmlns:soap="http://
schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/
XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body><GetResponse xmlns="http://
tempuri.org/">
<GetResult>&lt;?xml version="1.0"
encoding="ISO-8859-1"?&gt;&lt;Response&gt;&lt;Entity Name="Accounts"
Current="00300571BDF91DDCA7D1320EE5C78877"&gt;&lt; Field Name="Name"
Value="Bad und WA¤rmetechnik FA_hrwirt GmbH"/&gt;&lt;/Instance&gt;&lt;/
Entity&gt;&lt;/Response&gt;</GetResult></GetResponse>
</soap:Body></soap:Envelope>"""

CHARSET = 'ISO-8859-1'

ET.XMLTreeBuilder = SimpleXMLTreeBuilder.TreeBuilder
spEnv = TEST_EVNVELOPE2
spEnv = spEnv.replace('utf-16', CHARSET)
spEnv = spEnv.replace('utf-8', CHARSET)
dom=ET.fromstring(spEnv.encode(CHARSET))

however when spEnv is assigned response.content directly I get parsing
error in ET.fromstring, even though TEST_EVNVELOPE2 value is just
pasted from the browser's src code, that is I just dumped
response.content to rendered html and then copied it from html src.
Why it works as a string const and not as variable value?

So, what's the correct way to make parsing work?

Jun 27 '08 #1
0 1525

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: jrefactors | last post by:
When people say C programming language, they mean ISO C89? The latest C is ISO C99, but I heard this is not commonly used. What's the differences between ISO C89 and ISO C99? Please advise....
4
by: Matteo | last post by:
Hy everybody. I'm not a html writer, but a sysadmin who's trying to help a user able to compile an online form with IE but not with Mozilla (Moz1.6, Ns7.1, Firefox 0.8+) due to a javascript date...
2
by: Samuel | last post by:
Hello, I am trying to convert a local time into UTC ISO8601, then parse it back into local time. I tried the following: ---------------------- #!/usr/bin/python import time import datetime...
10
by: Reiner Merz | last post by:
Hi, I'm looking for advice on how to parse a timestamp string according to the ISO 8601 specification. For those unfamiliar with the standard, here's an example: 2003-09-09T23:00:00Z...
19
by: linzhenhua1205 | last post by:
I want to parse a string like C program parse the command line into argc & argv. I hope don't use the array the allocate a fix memory first, and don't use the memory allocate function like malloc....
22
by: pemo | last post by:
ISO C does not allow extra ';' outside of a function int n;; int main(void) { .... } Anyone care to enlighten me as to why ISO C does not allow this, but
0
by: Andy Fish | last post by:
Hello, I'm trying to parse dates in ISO 8601 format. I tried this: DateTime.ParseExact(inputValue, "s", CultureInfo.InvariantCulture) But this only works with one specific format. Is there...
34
by: priyanka | last post by:
Hi, I was wondering if we could parse or do something in the executable( whose source language was C). How can I use some scripting language like perl/python to find out the information about...
9
by: Alok | last post by:
Hi, I want to purchase the ISO C++ standard ISO/IEC 14882:2003 specification for reference. The ANSI store sells the PDF or CD-ROM of the document for $30...
7
by: k0mp | last post by:
Hi, Is there a way to retrieve a web page and before it is entirely downloaded, begin to test if a specific string is present and if yes stop the download ? I believe that urllib.openurl(url)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
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
Oralloy
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,...
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
tracyyun
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 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.