Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Read in XML without escaping CDATA block contents
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
spiffytech  
View profile  
 More options Sep 8 2012, 1:50 pm
From: spiffytech <spiffyt...@gmail.com>
Date: Sat, 8 Sep 2012 10:50:49 -0700 (PDT)
Local: Sat, Sep 8 2012 1:50 pm
Subject: Read in XML without escaping CDATA block contents

I'm reading in an XML file that contains CDATA blocks like this:

<bullet begin="2">                                                        

    <text><![CDATA[<P ALIGN="LEFT">The purposes of information security
policies.</P>]]></text>                                                    

</bullet>

When I print out the BeautifulSoup tag tree, I get this:

<bullet begin="2">
    <text>&lt;P ALIGN="LEFT"&gt;The purposes of information security
policies.&lt;/P&gt;</text>                                                

</bullet>

Is there a way to make BeautifulSoup preserve the CDATA markup instead of
escaping the block contents?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Leonard Richardson  
View profile  
 More options Sep 8 2012, 2:24 pm
From: Leonard Richardson <leona...@segfault.org>
Date: Sat, 8 Sep 2012 14:24:02 -0400
Local: Sat, Sep 8 2012 2:24 pm
Subject: Re: Read in XML without escaping CDATA block contents

> Is there a way to make BeautifulSoup preserve the CDATA markup instead of
> escaping the block contents?

You're seeing decisions made by lxml's XML parser.

"By default, lxml's parser will strip CDATA sections from the tree and
replace them by their plain text content. As real applications for
CDATA are rare, this is the best way to deal with this issue."

-- http://lxml.de/api.html#cdata

If you're using pure lxml, you can preserve the CDATA blocks by
passing strip_cdata=False into the XMLParser constructor. Beautiful
Soup's lxml tree builder passes strip_cdata=False, but Beautiful Soup
uses a different parser interface from the default, and
strip_cdata=False has no effect when using this interface. I believe
this is a bug in lxml.

In my tests, the only parser that preserved that CDATA block was
Python's built-in HTMLParser. If preserving the CDATA block is
essential, I recommend you either parse the data with HTMLParser, or
work directly with lxml--whichever is easier.

Leonard


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »