> I provided what I though was a minimal example of what I'm trying to
> accomplish, but I guess it's not close enough to what I'm actually doing :)
> My ultimate goal is this: I have two XML documents. I want to replace the
> contents of a certain tag in document 1 with the XML tree inside a certain
> tag in document 2. I've tried this:
> doc1.find(locale=lang).string = doc2.find(locale=lang)
> but I run into the entity escaping problem. I suppose I could loop over the
> whole XML tree I want from doc2, creating new tags, copying the attributes
> across for each, and inserting them in place in doc1, but that seems like a
> lot of work to get right for something that seems so conceptually simple.
> Is there an easier way to accomplish this with BeautifulSoup?
Whenever you set .string to a value, the value is treated as a string,
and that means entity escaping. To replace the contents of a tag with
another tag, you need to use the tree manipulation methods. The
closest equivalent to the code you wrote is probably this:
locale = doc1.find(locale=lang)
This will give you a tree that looks like this:
But it sounds like you want something more like this:
For that I recommend unwrap(), which replaces a tag with its contents.
So your complete code might look like this:
locale1 = doc1.find(locale=lang)
locale2 = doc2.find(locale=lang)