Splitting a DOM
Uche Ogbuji
uche at ogbuji.net
Fri Feb 13 10:25:23 EST 2004
brice.vissiere at costes-gestion.net (Brice Vissi?re) wrote in message news:<fa538331.0402120759.44f20301 at posting.google.com>...
> Hello,
>
> I would like to handle an XML file structured as following
> <ROOT>
> <STEP>
> ...
> </STEP>
> <STEP>
> ...
> </STEP>
> ...
> </ROOT>
>
> From this file, I want to build an XML file for each STEP block.
>
> Currently I'm doing something like:
>
> from xml.dom.ext.reader import Sax2
> from xml.dom.ext import PrettyPrint
>
> reader = Sax2.Reader()
> my_dom = reader.fromUri('steps.xml')
> steps = my_dom.getElementsByTagName('STEP')
>
> i=0
> for step in steps:
> tmp = file('step%s.xml' % i,'w')
> tmp.write('<?xml version="1.0" encoding="ISO-8859-1" ?>\n')
> PrettyPrint(step , tmp , encoding='ISO-8859-1')
> tmp.close()
> i+=1
>
> But I'm pretty sure that there's a better way to split the DOM ?
Here's an Anobind recipe:
--- % ---
#Boilerplate set-up
import anobind
from Ft.Xml import InputSource
from Ft.Lib import Uri
#Create an input source for the XML
isrc_factory = InputSource.DefaultFactory
#Create a URI from a filename the right way
file_uri = Uri.OsPathToUri('steps.xml', attemptAbsolute=1)
isrc = isrc_factory.fromUri(file_uri)
#Now bind from the XML given in the input source
binder = anobind.binder()
binding = binder.read_xml(isrc)
#File splitting task
import tempfile
#The direct approach
i = 0
for folder in binding.ROOT.STEP:
fout = open('step%s.xml', 'w')
folder.unbind(fout)
fout.close()
i += 1
--- % ---
To use XPath replace the line
for folder in binding.ROOT.STEP:
With
for folder in binding.xpath_query(u'ROOT/STEP'):
Anobind: http://uche.ogbuji.net/tech/4Suite/anobind/
--Uche
http://uche.ogbuji.net
More information about the Python-list
mailing list