On Mon, Sep 11, 2006 at 02:38:46PM -0400, Kent Johnson wrote:
>>>On Mon, Sep 11, 2006 at 12:11:37PM -0400, William O'Higgins Witteman 
>>>wrote:
>>>>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. 

>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.

That's a huge help, thank you.  What would I do if there is no namespace
for the given documents?  I find that a great deal of "XML" content is
just well-formed ad-hoc-ery, lacking formal definitions and namespaces,
and so there is no URI to put in the find argument.  Do I have to find a
new module?  Thanks again.
-- 

yours,

William
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to