473,790 Members | 3,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

lxml question

Hi,

I have to parse some text which pretends to be XML. lxml does not want
to parse it, because it lacks a root element.
I think that this situation is not unusual, so: is there a way to
force lxml to parse it ?

My work around is wrapping the text with "<root>...</root>" before
feeding lxmls parser.

Greetings, Uwe
Sep 26 '08 #1
3 1345
On Sep 26, 11:19*am, Uwe Schmitt <rocksportroc.. .@googlemail.co m>
wrote:
I have to parse some text which pretends to be XML. lxml does not want
to parse it, because it lacks a root element.
I think that this situation is not unusual, so: is there a way to
force lxml to parse it ?
By "pretends to be XML" you mean XML-like but not really XML?
My work around is wrapping the text with "<root>...</root>" before
feeding lxmls parser.
That's actually not a bad solution, if you know that the document is
otherwise well-formed. Another thing you can do is use libxml2's
"recover" mode which accommodates non-well-formed XML.

parser = etree.XMLParser (recover=True)
tree = etree.XML(your_ xml_string, parser)

You'll still need to use your wrapper root element, because recover
mode will ignore everything after the first root closes (and it won't
throw an error).

-- Mark.
Sep 26 '08 #2
On Sep 27, 1:19*am, Uwe Schmitt <rocksportroc.. .@googlemail.co m>
wrote:
I have to parse some text which pretends to be XML. lxml does not want
to parse it, because it lacks a root element.
Another option is BeautifulSoup, which handles badly formed XML really
well:

http://www.crummy.com/software/BeautifulSoup/
Sep 27 '08 #3
Uwe Schmitt wrote:
I have to parse some text which pretends to be XML. lxml does not want
to parse it, because it lacks a root element.
I think that this situation is not unusual, so: is there a way to
force lxml to parse it ?

My work around is wrapping the text with "<root>...</root>" before
feeding lxmls parser.
Yes, you can do that. To avoid creating an intermediate string, you can use
the feed parser and do something like this:

parser = etree.XMLParser ()
parser.feed("<r oot>")
parser.feed(you r_xml_tag_seque nce_data)
parser.feed("</root>")
root = parser.close()

Stefan
Oct 3 '08 #4

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

Similar topics

2
2498
by: Srijit Kumar Bhadra | last post by:
I am new to lxml. I am interested to know the equivalent code using lxml (http://cheeseshop.python.org/pypi/lxml/1.1alpha). The code is taken from http://effbot.org/zone/xml-writer.htm from elementtree.SimpleXMLWriter import XMLWriter import sys w = XMLWriter(sys.stdout) html = w.start("html")
5
8319
by: Jan Dries | last post by:
I'm trying to find Windows binaries for lxml. The cheeseshop is supposed to have such binaries, but I can't find them. Does anyone know where I might find such binaries? Thanks, Jan
30
4632
by: Chas Emerick | last post by:
I looked around for an ElementTree-specific mailing list, but found none -- my apologies if this is too broad a forum for this question. I've been using the lxml variant of the ElementTree API, which I understand works in much the same way (with some significant additions). In particular, it shares the use of a .tail attribute. I ran headlong into this aspect of the API while doing some DOM manipulations, and it's got me pretty...
1
3264
by: Maxim Sloyko | last post by:
Hi All! I have a little problem with XML namespaces. In my application I have two XML processors, that process the same document, one after the other. The first one looks for nodes in 'ns1' namespace, and substitutes them, according to some algorithm. After this processor is finished, it is guaranteed that there are no more 'ns1' nodes left in the tree. however 'ns1' namespace dclaration is still there, in the root node (well, I put...
0
1082
by: Stefan Behnel | last post by:
Hi everyone, I'm very happy to announce the official release of lxml 2.0! http://codespeak.net/lxml/ http://pypi.python.org/pypi/lxml/2.0 ** What is lxml? """
7
5271
by: Silfheed | last post by:
Heyas So first off I know that CDATA is generally hated and just shouldn't be done, but I'm simply required to parse it and spit it back out. Parsing is pretty easy with lxml, but it's the spitting back out that's giving me issues. The fact that lxml strips all the CDATA stuff off isnt really a big issue either, so long as I can create CDATA blocks later with <>&'s showing up instead of &lt;&gt;&amp; . I've scoured through the lxml docs, but...
1
6386
by: =?iso-8859-1?q?KLEIN_St=E9phane?= | last post by:
Hi, I'm on Ubuntu 8.04.1 I've installed lxml with easy_install lxml command. Now, when I load etree I've this error : $ python Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42)
0
1978
by: =?iso-8859-1?q?KLEIN_St=E9phane?= | last post by:
Le Mon, 25 Aug 2008 13:50:50 +0000, KLEIN Stéphane a écrit : I've this bug only with lxml2, lxml 1.3.3 work very well. Regards, Stephane
1
2010
by: Owen Zhang | last post by:
I am trying to build lxml package in SunOS 5.10. I got the following errors. Does anybody know why? $ python setup.py build Building lxml version 2.1. NOTE: Trying to build without Cython, pre-generated 'src/lxml/ lxml.etree.c' needs to be available. Using build configuration of libxslt 1.1.7 Building against libxml2/libxslt in the following directory: /usr/lib running build
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10201
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10147
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9987
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6770
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5424
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3709
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2910
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.