Re: [xml] Parsing document fragments in Python

On Sun, 2003-12-21 at 15:51, Daniel Veillard wrote:
On Sun, Dec 21, 2003 at 04:21:48PM -0500, Phil Frost wrote:
I've been trying to find a way to parse a fragment of a document (balanced)
and insert it into an existing tree. I found this in the archives:

That seems to be just what I need; parse a balanced (but not complete) string
into a tree that can be inserted into another tree.

However, I'm having difficulty finding the equivalent functionality in the
Python bindings. Could someone please clarify this?

  I don't think there is a python binding for it.

As a work-around, you can read the balanced chunk into a second document
using parseMemory, grab that doc's root element, then insert that where
needed in your original doc. Here's a brief example:

#!/usr/bin/python -u
import libxml2

chunk = "<foo><bar>content</bar></foo>"
doc2 = libxml2.parseMemory(chunk, len(chunk))
root2 = doc2.getRootElement()

doc1 = libxml2.newDoc("1.0")
root1 = doc1.newChild(None, "doc", None)
print doc1.serialize(None,1)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]