William O'Higgins Witteman wrote:
> On Mon, Sep 11, 2006 at 09:57:28AM -0700, Dave Kuhlman wrote:
>> On Mon, Sep 11, 2006 at 12:11:37PM -0400, William O'Higgins Witteman wrote:
>>> I have a large number of XML documents to add data to. They are
>>> currently skeletal documents, looking like this:
>>>
>>> <?xml version="1.0" ?>
>>> <!DOCTYPE rdf:RDF SYSTEM "local.dtd">
>>> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
>>> <rdf:Description rdf:about="local_file">
>>> <tagname></tagname>
>>> <anothertagname></anothertagname>
>>> ...
>>>
>>> What I want is to open each document and inject some data between
>>> specific sets of tags. I've been able to parse these documents, but I am
>>> not seeing how to inject data between tags so I can write it back to the
>>> file. Any pointers are appreciated. Thanks.
>
>> Here is a bit of code to give you the idea with ElementTree (or
>> lxml, which uses the same API as ElementTree):
>>
>> from elementtree import ElementTree as etree
>> doc = etree.parse('content.xml')
>> root = doc.getroot()
>> # Do something with the DOM tree here.
>> o
>
> This is the bit I'm missing - I can't seem to find an existing element
> and change it's value. When I do so I just get an additional element.
> Here's the code I'm using:
>
> main = etree.SubElement(root,"rdf:Description")
> title = etree.SubElement(main,"title")
> title.text = "Example Title"
That's what SubElement does - it creates a new element. You need to find
the existing element. The section on Searching should point you in the
right direction:
http://effbot.org/zone/element.htm#searching-for-subelements
Try something like
title =
root.find('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}Description/title')
Note that ET uses the URI of the namespace, not the short name.
You can explore a bit from the interactive interpreter to help get your
bearings, for example
print root
for sub in root:
print sub
will give you a good idea what the correct name of the Description element.
Kent
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor